Topic description
The group of students from various schools, in order to increase friendship, the party has a word game, if a know B, then a received a message, it will pass this message to B, and all the people who know.
If a knows b,b not necessarily know a.
All the people from 1 to n numbers, give all "understanding" relationship, ask if I release a new message, then will pass a number of messages, this message back to I,1<=i<=n.
Input format
The first line in the input is two numbers n (n<1000) and M (m<10000), and a space between two numbers indicates the number of people and the number of cognitive relationships.
The next m line, two numbers a and b per line, represents a knowing B. 1<=a, B<=n. The cognitive relationship may be repeated, but a line of two numbers will not be the same.
Output format
A total of n rows in the output, one character T or F per line. Line I if it is T, I send a new message back to I; if it is F, I send a new message that does not pass back to I. Sample Input 4 6
1 2
2 3
4 1
3 1
1 3
2 3
Sample output
T
T
T
F
It's kind of like a ring-awarding problem.
But the data is small.
Direct critical table + DFS starting at each point
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int len=1111;
const int lim=11111;
int First[lim],next[lim];
int m,n;
struct Self{int x,y;} S[lim];
BOOL Flag[len];
BOOL Dfs (int i,int root)
{
int A;
BOOL P=false;
Flag[i]=1;
for (int e=first[i];e!=-1;e=next[e])
{
if (s[e].y==root) return true;
if (!flag[s[e].y])
{
P=dfs (s[e].y,root);
if (p) return p;
}
}
return p;
}
int a,b;
int main ()
{
memset (first,-1,sizeof (a));
memset (Next,-1,sizeof (next));
scanf ("%d%d", &m,&n);
for (a=1;a<=n;a++)
{
scanf ("%d%d", &s[a].x,&s[a].y);
Next[a]=first[s[a].x];
first[s[a].x]=a;
}
for (a=1;a<=m;a++)
{
memset (flag,0,sizeof (flag));
if (Dfs (a,a)) cout<< "T" << ' \ n ';
else cout<< ' F ' << ' \ n ';
}
return 0;
}