Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2271 Accepted Submission(s): 946
2 5 00100 10000 01001 11101 11000 5 01111 00000 01000 01100 01110
Case #1: Yes Case #2: No
该图本质是拓扑排序题.如果该图可以拓扑排序,那么不存在3节点的环,否则存在3节点的环.
#include<cstdio> #include<iostream> #include<cstring> #include<queue> #include<algorithm> #include<vector> using namespace std; const int M = 2000 + 5; int n; int in[M]; char str[M]; int t; vector<int> map[M]; bool toposort() { int sum = 0; queue<int>Q; for(int i=0; i<n; i++) if( !in[i] ) Q.push( i ); while( !Q.empty() ) { int u = Q.front(); Q.pop(); sum++; for(int i=0; i<map[u].size(); i++) { int m = map[u][i]; if( --in[m] == 0 ) Q.push( m ); } } if( sum==n ) return true; else return false; } int main() { scanf( "%d", &t ); int cas; for( cas=1; cas<=t; cas++ ) { scanf( "%d", &n ); memset( in, 0, sizeof( in ) ); for( int i=0; i<n; i++ ) { map[i].clear(); scanf( "%s", str ); for( int j=0; j<n; j++ ) //for(int j=0; j<strlen(str); j++) //这么写会超时,复杂度会增加 { if( str[j]=='1' ) { map[i].push_back( j ); in[ j ]++; } } } printf("Case #%d: %s\n", cas, toposort()?"No":"Yes"); } return 0; }
HDU 4324:Triangle LOVE( 拓扑排序 ),布布扣,bubuko.com
HDU 4324:Triangle LOVE( 拓扑排序 )
原文:http://blog.csdn.net/u013487051/article/details/37914423