POJ 1637 混合图欧拉回路判定
来源:互联网 发布:览物之情得无异乎的乎 编辑:程序博客网 时间:2024/06/06 04:54
Sightseeing tour
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 7070 Accepted: 2941
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
45 82 1 01 3 04 1 11 5 05 4 13 4 04 2 12 2 04 41 2 12 3 03 4 01 4 13 31 2 02 3 03 2 03 41 2 02 3 11 2 03 2 0
Sample Output
possibleimpossibleimpossiblepossible
Source
只需要判断出度入度,然后就是建图跑最大流判断是否满流。
代码:
/* ***********************************************Author :rabbitCreated Time :2014/3/26 10:45:50File Name :12.cpp************************************************ */#pragma comment(linker, "/STACK:102400000,102400000")#include <stdio.h>#include <iostream>#include <algorithm>#include <sstream>#include <stdlib.h>#include <string.h>#include <limits.h>#include <string>#include <time.h>#include <math.h>#include <queue>#include <stack>#include <set>#include <map>using namespace std;#define INF 0x3f3f3f3f#define eps 1e-8#define pi acos(-1.0)typedef long long ll;const int maxn=20010;const int maxm=1000000;struct Edge{int next,to,cap;Edge(){};Edge(int _next,int _to,int _cap){next=_next;to=_to;cap=_cap;}}edge[maxm];int head[maxn],tol,dep[maxn],gap[maxn];void addedge(int u,int v,int flow){ edge[tol]=Edge(head[u],v,flow);head[u]=tol++; edge[tol]=Edge(head[v],u,0);head[v]=tol++;}void bfs(int start,int end){ memset(dep,-1,sizeof(dep)); memset(gap,0,sizeof(gap)); gap[0]++;int front=0,rear=0,Q[maxn]; dep[end]=0;Q[rear++]=end; while(front!=rear){ int u=Q[front++]; for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].to;if(dep[v]==-1) Q[rear++]=v,dep[v]=dep[u]+1,gap[dep[v]]++; } }}int sap(int s,int t,int N){int res=0;bfs(s,t);int cur[maxn],S[maxn],top=0,u=s,i;memcpy(cur,head,sizeof(head));while(dep[s]<N){if(u==t){int temp=INF,id; for( i=0;i<top;i++) if(temp>edge[S[i]].cap) temp=edge[S[i]].cap,id=i; for( i=0;i<top;i++) edge[S[i]].cap-=temp,edge[S[i]^1].cap+=temp; res+=temp;top=id;u=edge[S[top]^1].to;}if(u!=t&&gap[dep[u]-1]==0)break;for( i=cur[u];i!=-1;i=edge[i].next)if(edge[i].cap&&dep[u]==dep[edge[i].to]+1)break;if(i!=-1)cur[u]=i,S[top++]=i,u=edge[i].to;else{int MIN=N;for( i=head[u];i!=-1;i=edge[i].next)if(edge[i].cap&&MIN>dep[edge[i].to])MIN=dep[edge[i].to],cur[u]=i;--gap[dep[u]];++gap[dep[u]=MIN+1];if(u!=s)u=edge[S[--top]^1].to;}}return res;}int in[maxn],out[maxn];int main(){ //freopen("data.in","r",stdin); //freopen("data.out","w",stdout); int T,t; scanf("%d",&T); for(t=1;t<=T;t++){ int n,m; scanf("%d%d",&n,&m); memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); memset(head,-1,sizeof(head));tol=0; int i,j,k; while(m--){scanf("%d%d%d",&i,&j,&k); out[i]++;in[j]++; if(k==0)addedge(i,j,1); } int flag=1;int sum=0;for(int i=1;i<=n;i++){int ret=out[i]-in[i];if(ret>0)addedge(0,i,ret/2),sum+=ret/2;else if(ret<0)addedge(i,n+1,-ret/2);if((in[i]+out[i])%2){flag=0;break;}}if(!flag){puts("impossible");continue;}int ans=sap(0,n+1,n+10);if(ans==sum)puts("possible");else puts("impossible"); } return 0;}
0 0
- POJ 1637 混合图欧拉回路的判定
- POJ 1637 混合图欧拉回路判定
- poj 1637判定混合图欧拉回路 uva10735 混合欧拉回路+路径输出
- poj 1637(混合图欧拉回路)
- 【混合图欧拉回路】POJ 1637
- POJ 1637 混合图欧拉回路
- 混合图欧拉回路 POJ 1637
- HDU 3472 混合图欧拉回路的判定
- POJ1637 Sightseeing tour(混合图欧拉回路判定)
- 判断混合图欧拉回路 poj 1637
- POJ 1637 Sightseeing tour (混合图欧拉回路)
- poj 1637 混合图欧拉回路,最大流
- poj 1637 Sightseeing tour(混合图欧拉回路)
- poj -- 1637 Sightseeing tour(混合图欧拉回路)
- [POJ 1637]Sightseeing tour[混合图欧拉回路]
- POJ 1637:Sightseeing tour 混合图欧拉回路
- Poj 1637 Sightseeing tour (混合图的欧拉回路判定)
- POJ-1637 Sightseeing tour(通过网络流判定混合图的欧拉回路)
- poj1159(回文串+LCS+滚动数组)
- 英语学习笔记3:Pets
- 强大的java编程实现邮件发送功能,支持内容,上传多个图片和多个附件
- Error: [Microsoft][ODBC 驱动程序管理器] 在指定的 DSN 中,驱动程序和应用程序之间的体系结构不匹配
- LCD基础知识
- POJ 1637 混合图欧拉回路判定
- andriod 笔记__clipboardManager
- Android下屏保程序的 开发须知
- oracle (+) 左右关联
- 编译器报错: error LNK2001: unresolved external symbol "struct _ServiceDescriptorTable * KeServiceDescript
- 查询条件为ROWNUM=2没有结果
- 十款最常见的Linux发行版及目标用户
- Memcached源码解析之cas属性
- 如何科学晾衣有利健康