hdu 4324 Triangle LOVE <拓扑排序>

链接 http://acm.hdu.edu.cn/showproblem.php?pid=4324

 此题可以一遍拓扑排序判环求解 即只需要找到一个环,就必定存在三元环 证明如下: 假设存在一个n元环,因为a->b有边,b->a必定没边,反之也成立 所以假设有环上三个相邻的点a-> b-> c,那么如果c->a间有边,就已经形成了一个三元环,如果c->a没边,那么a->c肯定有边,这样就形成了一个n-1元环。。。。 所以只需证明n为4时一定有三元环即可,显然成立。

View Code
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <algorithm>
 5 using namespace std;
 6 char map[2500][2500];
 7 int in[2500];
 8 int main( )
 9 {
10     int T;
11     scanf("%d", &T );
12     for( int t=1; t<=T; ++t ){
13         printf( "Case #%d: ", t );
14         int N, flage=0; 
15         scanf( "%d", &N );
16         for( int i=0; i<N; ++ i ){
17             scanf( "%s", map[i] );
18             in[i]=0;
19         }
20             
21         for( int i=0; i<N; ++ i ){
22             
23             for( int j=0; j<N; ++ j ){
24                 if( map[i][j]=='1' ){
25                     in[j]++;
26                 }
27             }
28         }
29         for( int i=0, k; i<N; ++i ){
30             for(  k=0; k<N; ++k ){
31                 if( !in[k] )break;
32             }
33             if( k==N ){
34                 puts( "Yes" );
35                 flage=1;
36                 break;
37             }
38             else{
39             //    printf( "%d ", k );
40                 in[k]--;
41                 for( int j=0; j<N; ++j ){
42                     if( map[k][j]=='1'&&in[j] )
43                         in[j]--;
44                 }
45             }
46         }
47         if( !flage )puts( "No" );
48     } 
49     return 0;
50 }

 

原文地址:https://www.cnblogs.com/jian1573/p/2619372.html