http://acm.hdu.edu.cn/showproblem.php?pid=5154
Idea: There is a direction to figure out whether there is a ring, can be used Floyd.
1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 Const intinf=1<< -;6 7 intg[ $][ $];8 intMain ()9 {Ten intn,m; One while(SCANF ("%d%d", &n,&m)! =EOF) A { -Memset (G,0,sizeof(g)); - for(intI=1; i<=n; i++) the { - for(intj=1; j<=n; J + +) - { -g[i][j]=inf; + } - } + for(intI=0; i<m; i++) A { at intb; -scanf"%d%d",&a,&b); -g[b][a]=1; - } - for(intk=1; k<=n; k++) - { in for(intI=1; i<=n; i++) - { to for(intj=1; j<=n; J + +) + { - if(i==k| | J==K)Continue; theG[i][j]=min (g[i][j],g[i][k]+g[k][j]); * } $ }Panax Notoginseng } - BOOLflag=true; the for(intI=1; i<=n; i++) + { A if(g[i][i]<inf) the { +flag=false; - Break; $ } $ } - if(flag) printf ("yes\n"); - Elseprintf"no\n"); the } - return 0;Wuyi}
View Code
Hdu 5154 Harry and magical computer