Euler Circuit UVA - 10735(混合图输出路径)

时间:2024-05-06 09:06:32

就是求混合图是否存在欧拉回路

如果存在则输出一组路径

(我就说嘛 咱的代码怎么可能错。。。。。最后的输出格式竟然w了一天 我都没发现)

解析:

  对于无向边定向建边放到网络流图中add(u, v, 1);

  对于有向边放到另一个图中add2(u, v);

  然后就是混合边求是否有欧拉

  一边dinic后 遍历每一条边 如果不是反向边 且 起点不是s 终点不是t

  如果Node[i].c == 0 则 add2(Node[i].v, Node[i].u);

  else add2(Node[i].u, Node[i].v);

  然后用有向图的fleury输出边就好了

 #include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define pd(a) printf("%d\n", a);
#define plld(a) printf("%lld\n", a);
#define pc(a) printf("%c\n", a);
#define ps(a) printf("%s\n", a);
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 2e9;
int n, m, s, t, cnt;
int in[maxn], out[maxn], vis[maxn];
int d[maxn], head[maxn], cur[maxn];
set<int> ss;
int st[maxn],cnt3;
int cnt2, head2[maxn]; struct edge
{
int u, v, c, next, ff;
}Edge[maxn << ]; void add_(int u, int v, int c, int ff)
{
Edge[cnt].u = u;
Edge[cnt].v = v;
Edge[cnt].c = c;
Edge[cnt].ff = ff;
Edge[cnt].next = head[u];
head[u] = cnt++;
} void add(int u, int v, int c)
{
add_(u, v, c, );
add_(v, u, , );
} bool bfs()
{
queue<int> Q;
mem(d, );
Q.push(s);
d[s] = ;
while(!Q.empty())
{
int u = Q.front(); Q.pop();
for(int i = head[u]; i != -; i = Edge[i].next)
{
edge e = Edge[i];
if(!d[e.v] && e.c > )
{
d[e.v] = d[e.u] + ;
Q.push(e.v);
if(e.v == t) return ;
}
}
}
return d[t] != ;
} int dfs(int u, int cap)
{
int ret = ;
if(u == t || cap == )
return cap;
for(int &i = cur[u]; i != -; i = Edge[i].next)
{
edge e = Edge[i];
if(d[e.v] == d[u] + && e.c > )
{
int V = dfs(e.v, min(cap, e.c));
Edge[i].c -= V;
Edge[i^].c += V;
ret += V;
cap -= V;
if(cap == ) break;
}
}
if(cap > ) d[u] = -;
return ret;
} int Dinic(int u)
{
int ans = ;
while(bfs())
{
memcpy(cur, head, sizeof(head));
ans += dfs(u, INF);
}
return ans;
} struct node
{
int u, v, flag, next;
}Node[maxn << ]; void add2(int u, int v)
{
Node[cnt2].u = u;
Node[cnt2].v = v;
Node[cnt2].next = head2[u];
Node[cnt2].flag = ;
head2[u] = cnt2++;
}
int used[maxn];
void fleury(int u)
{
for(int i = head2[u]; i != -; i = Node[i].next)
{
if(!used[i])
{
used[i] = ;
fleury(Node[i].v);
} }
st[cnt3++] = u;
} void init()
{
mem(in, );
mem(head, -);
mem(out, );
mem(st, );
cnt = ;
cnt2 = ;
cnt3 = ;
mem(head2, -);
mem(used, );
ss.clear(); } char str[]; int main()
{
int T;
cin >> T;
while(T--)
{
int u, v, w;
cin >> n >> m;
init();
s = , t = maxn - ;
for(int i = ; i <= m; i++)
{
scanf("%d%d%s", &u, &v, str);
in[v]++, out[u]++;
if(str[] == 'U') add(u, v, );
else if(str[] == 'D') add2(u, v);
}
int flag = , m_sum = ;
for(int i = ; i <= n; i++)
{
if(abs(out[i] - in[i]) & )
{
flag = ;
break;
}
if(out[i] > in[i]) add(s, i, (out[i] - in[i]) / ), m_sum += (out[i] - in[i]) / ;
else if(in[i] > out[i]) add(i, t, (in[i] - out[i]) / ); }
if(!flag && m_sum == Dinic(s))
{
for(int i = ; i < cnt; i++)
{
if(!Edge[i].ff || Edge[i].u == s || Edge[i].v == t) continue;
if(Edge[i].c == ) add2(Edge[i].v, Edge[i].u);
else add2(Edge[i].u, Edge[i].v);
}
fleury();
for(int i = cnt3 - ; i >= ; i--)
{
if(i != cnt3 - ) printf(" ");
printf("%d", st[i]);
} printf("\n"); }
else
cout << "No euler circuit exist" << endl;
if(T) printf("\n"); } return ;
}