HRBUST2048 Thrall's Dream solution to a problem report

Topic Portal

[Title] effect

Desperate long English decent ...... However, in practice meaning of the questions is very simple

N-given $ $ $ m $ points and directed edge section, it is determined whether communication with the FIG., The communication is defined for any two points $ x, y $, $ satisfy the path from there to the X $ $ $ Y or path from $ y $ to $ x $.

[Analysis] ideas

 This question is very simple matter, even after the good side from every point of $ dfs $ again, if connectivity to record, final judgment on the line, complexity $ O (N) $.

【Code】

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #define g() getchar()
 7 #define rg register
 8 #define go(i,a,b) for(rg int i=a;i<=b;i++)
 9 #define back(i,a,b) for(rg int i=a;i>=b;i--)
10 #define db double
11 #define ll long long
12 #define il inline
13 #define pf printf
14 #define mem(a,b) memset(a,b,sizeof(a))
15 using namespace std;
16 int fr(){
17     int w=0,q=1;
18     char ch=g();
19     while(ch<'0'||ch>'9'){
20         if(ch=='-') q=-1;
21         ch=g();
22     }
23     while(ch>='0'&&ch<='9') w=(w<<1)+(w<<3)+ch-'0',ch=g();
24     return w*q;
25 }
26 const int N=2002,M=10002;
27 int T,n,m,head[N];
28 bool d[N][N];
29 struct edge{
30     int to,next;
31 }e[M];
32 il void work(rg int from,rg int now){
33     d[from][now]=1;
34     for(rg int i=head[now];i;i=e[i].next)
35         if(!d[from][e[i].to]) work(from,e[i].to);
36     return;
37 }
38 int main(){
39     //freopen("","r",stdin);
40     //freopen("","w",stdout);
41     T=fr();
42     go(t,1,T){
43         bool ans=1;
44         n=fr();m=fr();
45         mem(head,0);mem(d,0);
46         go(i,1,m){
47             rg int u=fr(),v=fr();
48             e[i].next=head[u];
49             e[i].to=v;
50             head[u]=i;
51         }
52         go(i,1,n) work(i,i);
53         go(i,1,n) go(j,1,n) if(!d[i][j]&&!d[j][i]) {ans=0;break;}
54         if(ans) pf("Case %d: Kalimdor is just ahead\n",t);
55         else pf("Case %d: The Burning Shadow consume us all\n",t);
56     }
57     return 0;
58 }
Code poke here

Guess you like

Origin www.cnblogs.com/THWZF/p/11422895.html