Topic Link Click Open link
A give a message to B, B to C transmission message ... Ask if the message from a will eventually return to a.
Train of thought: judge whether the topological arrangement asks if it is ring.
DFS detonation, a known all is stored in the adjacency table of A. Enumerate the people that each a knows, Dfs.
AC Code:
#include <iostream> #include <string> #include <cstring> #include <cstdio> #include <cmath > #include <iomanip> #include <map> #include <queue> #include <set> #define PI ACOs (-1.0) #
Define EPS 1e-8 typedef long LL;
using namespace Std;
int Vis[1505],head[1505],cnt,flag; struct node {int u,v,next;}
E[5505];
void Init () {memset (vis,0,sizeof (VIS));
Memset (head) (head,-1,sizeof);
cnt=0;
} void Add_edge (int u,int v) {e[cnt].u=u;
E[cnt].v=v;
E[cnt].next=head[u];
head[u]=cnt++;
} void Dfs (int sta,int x) {if (flag) return;
if (sta==x) {flag=1;
Return
} vis[x]=1;
for (int i=head[x]; I!=-1 i=e[i].next) {int tmp=e[i].v;
if (!vis[tmp] &&!flag) DFS (STA,TMP);
if (flag) return;
} return;
int main () {init ();
int n,m,i,j,u,v;
scanf ("%d%d", &n,&m); For (i=0 i<m; i++) {scanf ("%d%d", &U,&V);
Add_edge (U,V);
for (I=1; i<=n; i++) {flag=0;
memset (Vis) (vis,0,sizeof);
for (int j=head[i]; j!=-1 j=e[j].next) {dfs (I,E[J].V);
if (flag) break;
} if (flag) printf ("t\n");
else printf ("f\n");
return 0;
}
The first handwritten adjacency table, formerly all registration, on the adjacency table understanding is not in place. Through this, also deepened the understanding of the adjacency table. Will not appear after the edge of the serial number as the vertex number of such a tease behavior ....