[Codevs1506] And codevs1506
Description[Problem description]The interest group members came from various schools. To increase friendship, a messaging game was held at the party. If a knew B, a received a message and sent it to B, and all the people a knows. If a knows B, B doesn't necessarily know. The owner starts from 1 to n and gives all the "Cognition" relationships. If I publishes a new message, will the message be sent back to I after several conversations, 1 <= I <= n.[Input File]In the input file message. in, the first line is two numbers n (n <1000) and m (m <10000). There is a space between the two numbers, indicating the number of people and the number of cognitive relationships. The next m row contains two numbers a and B, indicating that a knows B. 1 <= a, B <= n. The relationship may be given again, but the two numbers in a row are not the same.[Output file]There are n lines in the output file message. out, with one character T or F in each line. Line I, if it is T, indicates that I will send a new message back to I; if it is F, it means that I will not send a new message back to I.[Input example]4 61 22 34 13 11 32 3[Output sample]TTTF1. the topological sorting is 60 points, and all situations cannot be handled.
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int r[1001],c[1001],a[1001][1001],ans[10001],top=1,s=0; 7 int n,m,x,y,t,tot=0; 8 int main(){ 9 ios::sync_with_stdio(false);10 cin>>n>>m;11 for(int i=1;i<=m;i++){12 int x,y;13 cin>>x>>y;14 c[x]++;r[y]++;15 a[x][c[x]]=y;16 }17 for(int i=1;i<=n;i++) if(r[i]==0) ans[top++]=i;18 while(top&&s<=n){19 t=ans[top];20 top--;21 for(int i=1;i<=c[t];i++){22 s++;23 r[a[t][i]]--;24 if(r[a[t][i]]==0)25 ans[top++]=a[t][i];26 }27 c[t]=0;28 }29 for(int i=1;i<=n;i++)30 if(c[i]==0) cout<<"F"<<endl;31 else cout<<"T"<<endl;32 return 0;33 }
2. dfs search for loops in the graph
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 using namespace std; 6 int l[1001][10001]; 7 int n,m,a,b,t; 8 bool v[1001],huan[1001]; 9 void dfs(int x){10 if(huan[t]) return ;11 v[x]=1;12 if(x==t){13 huan[t]=1;14 return ;15 }16 for(int i=1;i<=l[x][0];i++)17 if(!v[l[x][i]]) dfs(l[x][i]);18 }19 int main(){20 ios::sync_with_stdio(false);21 cin>>n>>m;22 for(int i=1;i<=m;i++){23 cin>>a>>b;24 l[a][0]++;25 l[a][l[a][0]]=b;26 }27 for(t=1;t<=n;t++){28 memset(v,0,sizeof(v));29 for(int i=1;i<=l[t][0];i++)30 dfs(l[t][i]);31 }32 for(int i=1;i<=n;i++)33 if(huan[i]) cout<<"T"<<endl;34 else cout<<"F"<<endl;35 return 0;36 }