標籤:
http://acm.hdu.edu.cn/showproblem.php?pid=5154
思路:有向圖判斷有沒有環,可以用floyd。。
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 const int inf=1<<20; 6 7 int g[200][200]; 8 int main() 9 {10 int n,m;11 while(scanf("%d%d",&n,&m)!=EOF)12 {13 memset(g,0,sizeof(g));14 for(int i=1; i<=n; i++)15 {16 for(int j=1; j<=n; j++)17 {18 g[i][j]=inf;19 }20 }21 for(int i=0; i<m; i++)22 {23 int a,b;24 scanf("%d%d",&a,&b);25 g[b][a]=1;26 }27 for(int k=1; k<=n; k++)28 {29 for(int i=1; i<=n; i++)30 {31 for(int j=1; j<=n; j++)32 {33 if(i==k||j==k) continue;34 g[i][j]=min(g[i][j],g[i][k]+g[k][j]);35 }36 }37 }38 bool flag=true;39 for(int i=1; i<=n; i++)40 {41 if(g[i][i]<inf)42 {43 flag=false;44 break;45 }46 }47 if(flag) printf("YES\n");48 else printf("NO\n");49 }50 return 0;51 }View Code
hdu 5154 Harry and Magical Computer