FZU-1924+判断环/DFS/BFS
dfs:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 dfs 3 */ 4 #include<stdio.h> 5 #include<string.h> 6 #include<stdlib.h> 7 #include<algorithm> 8 #include<iostream> 9 #include<queue> 10 #include<map> 11 #include<stack> 12 #include<set> 13 #include<math.h> 14 using namespace std; 15 typedef long long int64; 16 //typedef __int64 int64; 17 typedef pair<int64,int64> PII; 18 #define MP(a,b) make_pair((a),(b)) 19 const int maxn = 1005; 20 const int inf = 0x7fffffff; 21 const double pi=acos(-1.0); 22 const double eps = 1e-8; 23 24 int vis[ maxn ]; 25 struct Edge{ 26 int u,next; 27 }edge[ maxn<<4 ]; 28 int cnt,head[ maxn ]; 29 void init(){ 30 memset( vis,-1,sizeof( vis ) ); 31 cnt = 0; 32 memset( head,-1,sizeof( head ) ); 33 } 34 void addedge( int a,int b ){ 35 edge[ cnt ].u = b; 36 edge[ cnt ].next = head[ a ]; 37 head[ a ] = cnt++; 38 } 39 40 bool flag; 41 int ans; 42 void dfs( int cur ){ 43 if( flag ) return ; 44 vis[ cur ] = 1; 45 for( int i=head[ cur ];i!=-1;i=edge[i].next ){ 46 int nxt = edge[i].u; 47 if(nxt == ans){ 48 flag = true; 49 return; 50 } 51 if(vis[ nxt ] == 1)continue; 52 if( flag )return; 53 dfs( nxt ); 54 } 55 } 56 57 58 int main(){ 59 int T; 60 scanf("%d",&T); 61 int Case = 1; 62 while( T-- ){ 63 int n,m; 64 scanf("%d%d",&n,&m); 65 int edge_a,edge_b; 66 scanf("%d%d",&edge_a,&edge_b); 67 int x,y; 68 init(); 69 while( edge_a-- ){ 70 scanf("%d%d",&x,&y); 71 x++,y++; 72 y += n; 73 addedge( x,y ); 74 } 75 while( edge_b-- ){ 76 scanf("%d%d",&x,&y); 77 x++,y++; 78 x += n; 79 addedge( x,y ); 80 } 81 flag = false; 82 83 for( int i=1;i<=n;i++ ){ 84 memset(vis,0,sizeof(vis)); 85 ans = i; 86 dfs(i); 87 if(flag)break; 88 /* 89 if( flag==false&&vis[i]==-1 ){ 90 dfs( i,-1 ); 91 } 92 */ 93 } 94 printf("Case %d: ",Case ++ ); 95 if( flag ) printf("Possible\n"); 96 else printf("Impossible\n"); 97 } 98 return 0; 99 }
keep moving...