2013年省赛I题 Thrall’s Dream
2013年省赛I题
判断单向联通,用bfs
剪枝:从小到大跑,如果遇到之前跑过的点(也就是编号小于当前点的点),就o(n)传递关系。
bfs
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<algorithm> 5 #include<cmath> 6 #include<ctime> 7 #include<set> 8 #include<map> 9 #include<stack> 10 #include<cstring> 11 #define inf 2147483647 12 #define ls rt<<1 13 #define rs rt<<1|1 14 #define lson ls,nl,mid,l,r 15 #define rson rs,mid+1,nr,l,r 16 #define N 100010 17 #define For(i,a,b) for(int i=a;i<=b;i++) 18 #define p(a) putchar(a) 19 #define g() getchar() 20 21 using namespace std; 22 int T; 23 int n,m,cnt; 24 int x,y; 25 bool b[2010][2010]; 26 struct node{ 27 int n; 28 node *next; 29 }*e[40010]; 30 31 queue<int>q; 32 33 void in(int &x){ 34 int y=1; 35 char c=g();x=0; 36 while(c<'0'||c>'9'){ 37 if(c=='-')y=-1; 38 c=g(); 39 } 40 while(c<='9'&&c>='0'){ 41 x=(x<<1)+(x<<3)+c-'0';c=g(); 42 } 43 x*=y; 44 } 45 void o(int x){ 46 if(x<0){ 47 p('-'); 48 x=-x; 49 } 50 if(x>9)o(x/10); 51 p(x%10+'0'); 52 } 53 54 void push(int x,int y){ 55 node *p; 56 p=new node(); 57 p->n=y; 58 if(!e[x]) 59 e[x]=p; 60 else{ 61 p->next=e[x]->next; 62 e[x]->next=p; 63 } 64 } 65 66 void bfs(int x){ 67 q.push(x); 68 while(!q.empty()){ 69 int t=q.front(); 70 q.pop(); 71 if(t<x){ 72 For(i,1,n) 73 if(b[t][i]) 74 b[x][i]=1; 75 } 76 else{ 77 for(node *i=e[t];i;i=i->next) 78 if(!b[x][i->n]){ 79 b[x][i->n]=1; 80 q.push(i->n); 81 } 82 } 83 } 84 } 85 86 bool judge(){ 87 For(i,1,n) 88 For(j,i+1,n) 89 if(!b[i][j]&&!b[j][i]) 90 return 0; 91 return 1; 92 } 93 94 void clear(){ 95 memset(b,0,sizeof(b)); 96 For(i,1,2000) 97 e[i]=0; 98 } 99 100 int main(){ 101 in(T); 102 while(T--){ 103 clear(); 104 in(n);in(m); 105 For(i,1,m){ 106 in(x);in(y); 107 push(x,y); 108 } 109 For(i,1,n) 110 bfs(i); 111 printf("Case %d: ",++cnt); 112 if(judge()) 113 printf("Kalimdor is just ahead\n"); 114 else 115 printf("The Burning Shadow consume us all\n"); 116 } 117 return 0; 118 }
dfs更好写,队友比赛的时候不知道为啥T了
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<algorithm> 5 #include<cmath> 6 #include<ctime> 7 #include<set> 8 #include<map> 9 #include<stack> 10 #include<cstring> 11 #define inf 2147483647 12 #define ls rt<<1 13 #define rs rt<<1|1 14 #define lson ls,nl,mid,l,r 15 #define rson rs,mid+1,nr,l,r 16 #define N 100010 17 #define For(i,a,b) for(int i=a;i<=b;i++) 18 #define p(a) putchar(a) 19 #define g() getchar() 20 21 using namespace std; 22 int T; 23 int n,m,cnt; 24 int x,y; 25 bool b[2010][2010]; 26 struct node{ 27 int n; 28 node *next; 29 }*e[40010]; 30 31 queue<int>q; 32 33 void in(int &x){ 34 int y=1; 35 char c=g();x=0; 36 while(c<'0'||c>'9'){ 37 if(c=='-')y=-1; 38 c=g(); 39 } 40 while(c<='9'&&c>='0'){ 41 x=(x<<1)+(x<<3)+c-'0';c=g(); 42 } 43 x*=y; 44 } 45 void o(int x){ 46 if(x<0){ 47 p('-'); 48 x=-x; 49 } 50 if(x>9)o(x/10); 51 p(x%10+'0'); 52 } 53 54 void push(int x,int y){ 55 node *p; 56 p=new node(); 57 p->n=y; 58 if(e[x]==NULL) 59 e[x]=p; 60 else{ 61 p->next=e[x]->next; 62 e[x]->next=p; 63 } 64 } 65 66 // void bfs(int x){ 67 // q.push(x); 68 // while(!q.empty()){ 69 // int t=q.front(); 70 // q.pop(); 71 // if(t<x){ 72 // For(i,1,n) 73 // if(b[t][i]) 74 // b[x][i]=1; 75 // } 76 // else{ 77 // for(node *i=e[t];i;i=i->next) 78 // if(!b[x][i->n]){ 79 // b[x][i->n]=1; 80 // q.push(i->n); 81 // } 82 // } 83 // } 84 // } 85 86 void dfs(int x,int f){ 87 for(node *i=e[x];i;i=i->next) 88 if(!b[f][i->n]){ 89 b[f][i->n]=1; 90 dfs(i->n,f); 91 } 92 } 93 94 bool judge(){ 95 For(i,1,n) 96 For(j,i+1,n) 97 if(!b[i][j]&&!b[j][i]) 98 return 0; 99 return 1; 100 } 101 102 void clear(){ 103 memset(b,0,sizeof(b)); 104 For(i,1,2000) 105 e[i]=0; 106 } 107 108 int main(){ 109 in(T); 110 while(T--){ 111 clear(); 112 in(n);in(m); 113 For(i,1,m){ 114 in(x);in(y); 115 push(x,y); 116 } 117 For(i,1,n) 118 dfs(i,i); 119 printf("Case %d: ",++cnt); 120 if(judge()) 121 printf("Kalimdor is just ahead\n"); 122 else 123 printf("The Burning Shadow consume us all\n"); 124 } 125 return 0; 126 }