Sightseeing tour
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 9693 Accepted: 4077
Description
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.
Input
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.
Output
For each scenario, output one line containing the text “possible” or “impossible”, whether or not it’s possible to construct a sightseeing tour.
Sample Input
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
Sample Output
possible
impossible
impossible
possible
题意就是给一些点一些边,让你判断是否存在欧拉回路。
1代表有向边,0代表无向边。
具体做法:
欧拉回路
这个网络流的构图确实特别巧妙。
【我觉得当时写的还是挺详细的】
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 100010;
const int INF = 0x73f3f3f;
int s,t;
int T;
int n,m;
int ans=0;
int q1[N],q2[N];
//q1统计出度,q2统计入度
struct node{
int pre,v,f;
}edge[N];
int num=1;
int head[N];
void addedge(int from,int to,int f){
num++;
edge[num].pre=head[from];
edge[num].v=to;
edge[num].f=f;
head[from]=num;
num++;
edge[num].pre=head[to];
edge[num].v=from;
edge[num].f=0;
head[to]=num;
}
int Abs(int x){
return x<0?-x:x;
}
bool check(){
for(int i=1;i<=n;i++)
if(Abs(q1[i]-q2[i])&1) return false;
return true;
}
int state[N],dis[N];
bool vis[N];
bool bfs(){
int h=0,tail=1;
state[1]=s,vis[s]=true;
do{
h++;
int u=state[h];
for(int i=head[u];i;i=edge[i].pre){
int v=edge[i].v;
if(!vis[v]&&edge[i].f){
dis[v]=dis[u]+1;
vis[v]=true;
tail++;
state[tail]=v;
}
}
}while(h<tail);
if(vis[t]) return true;
return false;
}
inline int Min(int a,int b){
return a<b?a:b;
}
int dfs(int u,int delta){
if(u==t||delta==0) return delta;
int ans=0;
for(int i=head[u];i&δi=edge[i].pre){
int v=edge[i].v;
if(dis[v]==dis[u]+1&&edge[i].f){
int dd=dfs(v,Min(delta,edge[i].f));
edge[i].f-=dd;
edge[i^1].f+=dd;
ans+=dd;
delta-=dd;
}
}
if(!ans) dis[u]=-1;
return ans;
}
#define ms(x,y) memset(x,y,sizeof(x))
void zero(){
ms(dis,0);ms(vis,0);ms(state,0);
}
int maxflow(){
int ans=0;
while(1){
zero();
if(!bfs()) break;
ans+=dfs(s,INF);
}
return ans;
}
void update(){
ms(q1,0);ms(q2,0);ms(edge,0);ms(head,0);
num=1;ans=0;
}
int main(){
scanf("%d",&T);
while(T--){
update();
scanf("%d%d",&n,&m);
s=0,t=n+1;
while(m--){
int u,v,x;
scanf("%d%d%d",&u,&v,&x);
q1[u]++,q2[v]++;
if(x==0) addedge(u,v,1);
}
//printf("%d ",num);
/*for(int i=1;i<=n;i++)
printf("%d %d %d \n",i,q1[i],q2[i]);*/
if(!check()) printf("impossible\n");
else{
for(int i=1;i<=n;i++){
if(q1[i]>q2[i]){
int x=q1[i]-q2[i]>>1;
addedge(s,i,x);
}
else if(q1[i]<q2[i]){
int x=q2[i]-q1[i]>>1;
addedge(i,t,x);
ans+=x;
}
}
//for(int i=head[s];i;i=edge[i].pre)
// printf("%d ",edge[i].v);
//printf("%d",num);
if(maxflow()==ans) printf("possible\n");
else printf("impossible\n");
}
}
return 0;
}