Topic Description Description
A network of friends, if a knows B, then if a gets a message for the first time, it passes the message to B, and to everyone who knows it.
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 description Input Description
The first line is N and M, representing 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 Description Description
Altogether n rows, 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 For example
4 6
1 2
2 3
4 1
3 1
1 3
2 3
Sample output Sample
T
T
T
F
Range of data and hints of the size & Hint
n<=1000
1<=a, B<=n
This is obviously a BFS.
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <
Queue> using namespace std;
const int maxn=5000+5;
int first[maxn],nxt[maxn],tot=0;
int A[MAXN];
BOOL USED[MAXN];
int n,m; struct Edge {int f,t;}
L[MAXN];
void build (int f,int t) {l[++tot]= (edge) {f,t};
NXT[TOT]=FIRST[F];
First[f]=tot;
Return
BOOL BFS (int s) {queue<int>q;
memset (used,0,sizeof (used));
while (!q.empty ()) Q.pop ();
Q.push (s);
Used[s]=1;
while (!q.empty ()) {int F=q.front ();
Q.pop ();
for (int i=first[f];i;i=nxt[i]) {int w=l[i].t;
if (w==s) return 1;
else if (!used[w]) Q.push (w), used[w]=1;
} return 0;
int main () {int f,t;
scanf ("%d%d", &n,&m);
for (int i=1;i<=m;i++) {scanf ("%d%d", &f,&t);
Build (F,t); for (int i=1;i<=n;i++) {if (BFS (i)) putS ("t\n");
Else puts ("f\n");
return 0; }
Since the practice of Dfs is not, then learn.
A Dfs learned from a blog
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=1000+10;
BOOL S[maxn][maxn],vis[maxn][maxn],flag;
int n,m;
void Dfs (int x,int y)
{
if (flag==1) return;
if (S[x][y])
{
flag=1;
return;
}
for (int i=1;i<=n;i++)
{
if (S[x][i]&&!vis[x][i])
{
vis[x][i]=true;
DFS (i,y);
}}} int main ()
{
int a,b;
scanf ("%d%d", &n,&m);
for (int i=1;i<=m;i++)
{
scanf ("%d%d", &a,&b);
s[a][b]=1;
}
for (int i=1;i<=n;i++)
{
flag=0;
memset (Vis) (vis,0,sizeof);
DFS (i,i);
if (flag) puts ("t\n");
Else puts ("f\n");
}
return 0;
}
And just know that this is the idea of transitive closures.
Authentic transitive closures are as follows:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1000+1;
int n,m;
BOOL A[MAXN][MAXN];
void Floyd ()
{for (int k=1;k<=n;k++) for (int
i=1;i<=n;i++) for
(int j=1;j<=n;j++)
{
if (a[i][k]&&a[k][j])
a[i][j]=1
}
int main ()
{
int x,y;
scanf ("%d%d", &n,&m);
for (int i=1;i<=m;i++)
{
scanf ("%d%d", &x,&y);
a[x][y]=1;
}
Floyd ();
for (int i=1;i<=n;i++)
{
if (A[i][i]) puts ("t\n");
Else puts ("f\n");
}
return 0;
}
The principle is Floyd. The average complexity is slower than DFS, and the idea is similar.
But what is a transitive closure.