The city executive board in Lund wants to construct a sightseeing tour by bus in Lund, so that tourists can see every corner of the beautiful city. They want to construct the tour so that every street in the city is visited exactly once. The bus should also start and end at the same junction. As in any city, the streets are either one-way or two-way, traffic rules that must be obeyed by the tour bus. Help the executive board and determine if it’s possible to construct a sightseeing tour under these constraints.
On the first line of the input is a single positive integer n, telling the number of test scenarios to follow. Each scenario begins with a line containing two positive integers m and s, 1 <= m <= 200,1 <= s <= 1000 being the number of junctions and streets, respectively. The following s lines contain the streets. Each street is described with three integers, xi, yi, and di, 1 <= xi,yi <= m, 0 <= di <= 1, where xi and yi are the junctions connected by a street. If di=1, then the street is a one-way street (going from xi to yi), otherwise it’s a two-way street. You may assume that there exists a junction from where all other junctions can be reached.
For each scenario, output one line containing the text “possible” or “impossible”, whether or not it’s possible to construct a sightseeing tour.
4
5 8
2 1 0
1 3 0
4 1 1
1 5 0
5 4 1
3 4 0
4 2 1
2 2 0
4 4
1 2 1
2 3 0
3 4 0
1 4 1
3 3
1 2 0
2 3 0
3 2 0
3 4
1 2 0
2 3 1
1 2 0
3 2 0
possible
impossible
impossible
possible
Northwestern Europe 2003
过了编译就A了 开心~
无向图随意定向,统计出入度。
若 |indeg[u]−outdeg[u]| 为奇数,显然不可能存在欧拉回路,因为欧拉回路要求出度等于入度。
现在出入度之差是偶数了。
因为我们把无向边随意定向了,所以 |indeg[u]−outdeg[u]|/2 条边需要改变方向。
构建模型:因为我们需要给无向边定向,所以有向边没用,删掉。
定义 x=|indeg[u]−outdeg[u]|/2 。
若 x>0 ,则u的入度大于出度,则u向e连边,容量为x,表示u点有x条边流向汇点。
若 x<0 ,则u的入度小于出度,则s向u连边,容量为x,表示源点可以给u提供x条边。
若 x=0 ,则u的出入度本身就平衡,不用管。
然后对于随意定向的无向边,怎么定的向就怎么建图,容量为1表示u给v提供1条边。
然后跑最大流,若满流表示这些边都可以分出去,然后对于满流边反向就得到了一个欧拉回路。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int INF = 1000000010;
const int SZ = 10010;
int n,m;
int head[SZ],nxt[SZ],tot = 1,s,e;
struct edge{
int t,d;
}l[SZ];
void build(int f,int t,int d)
{
l[++ tot].t = t;
l[tot].d = d;
nxt[tot] = head[f];
head[f] = tot;
}
void insert(int f,int t,int d)
{
build(f,t,d); build(t,f,0);
}
int deep[SZ];
queue<int> q;
bool bfs()
{
memset(deep,0,sizeof(deep));
while(q.size()) q.pop();
deep[s] = 1;
q.push(s);
while(q.size())
{
int f = q.front(); q.pop();
for(int i = head[f];i;i = nxt[i])
{
int v = l[i].t;
if(!deep[v] && l[i].d)
{
deep[v] = deep[f] + 1;
q.push(v);
if(v == e) return true;
}
}
}
return false;
}
int dfs(int u,int flow)
{
if(u == e || flow == 0) return flow;
int rest = flow;
for(int i = head[u];i;i = nxt[i])
{
int v = l[i].t;
if(l[i].d && deep[v] == deep[u] + 1)
{
int f = dfs(v,min(rest,l[i].d));
if(f > 0)
{
l[i].d -= f;
l[i ^ 1].d += f;
rest -= f;
if(rest == 0) break;
}
else deep[v] = 0;
}
}
if(flow - rest == 0) deep[u] = 0;
return flow - rest;
}
int dinic()
{
int ans = 0;
while(bfs())
{
int tmp = dfs(s,INF);
if(!tmp) break;
ans += tmp;
}
return ans;
}
int indeg[SZ],outdeg[SZ];
int ff[SZ],tt[SZ],dd[SZ];
bool check()
{
s = n + 1,e = n + 2;
for(int i = 1;i <= m;i ++)
indeg[tt[i]] ++,outdeg[ff[i]] ++;
int sum = 0;
for(int i = 1;i <= n;i ++)
{
if((indeg[i] + outdeg[i]) & 1) return false;
int tmp = (indeg[i] - outdeg[i]) >> 1;
if(tmp == 0) continue;
if(tmp < 0)
insert(s,i,-tmp);
else
insert(i,e,tmp);
sum += tmp < 0 ? -tmp : tmp;
}
for(int i = 1;i <= m;i ++)
if(dd[i] == 0)
insert(ff[i],tt[i],1);
if(dinic() == (sum >> 1)) return true;
return false;
}
void init()
{
memset(head,0,sizeof(head));
memset(outdeg,0,sizeof(outdeg));
memset(indeg,0,sizeof(indeg));
tot = 1;
}
int main()
{
int T;
scanf("%d",&T);
while(T --)
{
init();
scanf("%d%d",&n,&m);
for(int i = 1;i <= m;i ++)
{
scanf("%d%d%d",&ff[i],&tt[i],&dd[i]);
}
if(check()) puts("possible");
else puts("impossible");
}
return 0;
}