sdutoj 2604 Thrall’s Dream
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2604
Thrall’s Dream
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
We never paid any heed to the ancient prophecies, like fools we clung to the old hatreds, and fought as we had for generations. Until one day the sky rained fire, and a new enemy came upon us. We stand now upon the brink of destruction, for the Reign of Chaos has come at last.
Thrall, the warchief of the Orcish Horde, all along, he led his tribe live in the fringe of Lordaeron under the human control. In a downpour night, Thrall falls into sleep in a Orc hall at Arathi Highlands, at this moment he heard a voice:
“The sands of time have run out, son of Durotan. The cries of war echo upon the winds, the remnants of the past scar the land which is besieged once again by conflict. Heroes arise to challenge fate, and lead their brethren to battle. As mortal armies rush blindly towards their doom, The Burning Shadow comes to consume us all. You must rally the Horde, and lead your people to their destiny.
I will answer all of your questions in time, young warchief. For now, rally your warriors and prepare to leave this land, cross the sea to the distant land of Kalimdor. We will speak again. ”
Thrall believes the prophesy of Blood Raven Medivh. Three days later, He and Grom Hellscream's Warsong Clan meet in the Lordaeron coast to the distant lands of Kalimdor. But the Goblin Zeppelins they take encountered storms in the middle. Thrall and Grom falling to the islands, they want to find each other and then to Kalimdor.
For the sake of simplicity, we assume that Thrall and Grom may fall into any islands x and y, only by Thrall to find Grom or by Grom to find Thrall. Give you the map of this island, please judge that Thrall and Gtom can meet?
输入
输出
示例输入
2 3 2 1 2 1 3 3 2 1 2 2 3
示例输出
Case 1: The Burning Shadow consume us all Case 2: Kalimdor is just ahead
提示
来源
示例程序
AC代码:
1 #include<stdio.h> 2 #include<iostream> 3 #include<string.h> 4 using namespace std; 5 int a[2005],b[2005],c[2005]; 6 int main() 7 { 8 int T,cnt=1; 9 scanf("%d",&T); 10 while(T--) 11 { 12 int n,m; 13 scanf("%d %d",&n,&m); 14 memset(a,0,sizeof(a)); 15 memset(b,0,sizeof(b)); 16 memset(c,0,sizeof(c)); 17 if(n<2) 18 { 19 printf("Case %d: Kalimdor is just ahead\n",cnt++); 20 } 21 else 22 { 23 int x,y; 24 while(m--) 25 { 26 scanf("%d %d",&x,&y); 27 a[x]--; 28 a[y]++; 29 c[x]=1; 30 c[y]=1; 31 } 32 int flag=1,t=0; 33 for(int i=1;i<=n;i++) 34 { 35 if(!c[i]) 36 { 37 flag=0; 38 break; 39 } 40 if(a[i]!=0) 41 t++; 42 } 43 if(!flag) 44 printf("Case %d: The Burning Shadow consume us all\n",cnt++); 45 else 46 { 47 if(t<=2) 48 printf("Case %d: Kalimdor is just ahead\n",cnt++); 49 else 50 printf("Case %d: The Burning Shadow consume us all\n",cnt++); 51 } 52 } 53 } 54 return 0; 55 }
官方代码:
1 #include <cstdio> 2 #include <cstring> 3 4 const int maxn = 2005; 5 const int maxm = 10005; 6 7 struct TOPO 8 { 9 int net[maxn], in[maxn], ans[maxn]; 10 int nv, size; 11 struct edge 12 { 13 int v, next; 14 edge(){} 15 edge(int a, int b){v = a; next = b;} 16 }E[maxm]; 17 inline void init(int n) 18 { 19 memset(in, 0, sizeof(in)); 20 memset(net, -1, sizeof(net)); 21 nv = n; 22 size = 0; 23 } 24 inline void add_edge(int u, int v) 25 { 26 E[size] = edge(v, net[u]); 27 net[u] = size++; 28 in[v]++; 29 } 30 bool toposort() 31 { 32 int num; 33 34 for(int i = 1; i <= nv; i++) 35 { 36 int j = 1; 37 num = 0; 38 for(int k = 1; k <= nv; k++) 39 if(!in[k]) 40 num++; 41 if(num > 1) 42 return false; 43 while(in[j] != 0) 44 j++; 45 in[j] = -1; 46 ans[i] = j; 47 for(int k = net[j]; k != -1; k = E[k].next) 48 in[E[k].v]--; 49 } 50 return true; 51 } 52 }T; 53 54 struct SCC 55 { 56 int net[maxn], dfn[maxn], low[maxn], s[maxn], belong[maxn]; 57 int count, ans, top, size, nv; 58 bool ins[maxn]; 59 struct edge 60 { 61 int v, next; 62 edge(){} 63 edge(int a, int b){v = a; next = b;} 64 }E[maxm]; 65 inline void init(int n) 66 { 67 memset(dfn, -1, sizeof(dfn)); 68 memset(net, -1, sizeof(net)); 69 memset(ins, false, sizeof(ins)); 70 count = ans = size = 0; 71 top = -1; 72 nv = n; 73 } 74 inline void add_edge(int u, int v) 75 { 76 E[size] = edge(v, net[u]); 77 net[u] = size++; 78 } 79 void dfs(int u) 80 { 81 int v; 82 83 dfn[u] = low[u] = ++count; 84 ins[u] = true; 85 s[++top] = u; 86 for(int i = net[u]; i != -1; i = E[i].next) 87 { 88 v = E[i].v; 89 if(dfn[v] == -1) 90 { 91 dfs(v); 92 if(low[v] < low[u]) 93 low[u] = low[v]; 94 } 95 else if(ins[v] && dfn[v] < low[u]) 96 low[u] = dfn[v]; 97 } 98 if(dfn[u] == low[u]) 99 { 100 ans++; 101 do 102 { 103 v = s[top--]; 104 belong[v] = ans; 105 ins[v] = false; 106 }while(u != v); 107 } 108 } 109 bool tarjan() 110 { 111 for(int i = 1; i <= nv; i++) 112 if(dfn[i] == -1) 113 dfs(i); 114 T.init(ans); 115 for(int i = 1; i <= nv; i++) 116 for(int j = net[i]; j != -1; j = E[j].next) 117 { 118 int v = E[j].v; 119 if(belong[i] != belong[v]) 120 T.add_edge(belong[i], belong[v]); 121 } 122 return T.toposort(); 123 } 124 }S; 125 126 int main() 127 { 128 int t, n, m, a, b, cnt = 0; 129 130 scanf("%d", &t); 131 while(t--) 132 { 133 scanf("%d %d", &n, &m); 134 S.init(n); 135 while(m--) 136 { 137 scanf("%d %d", &a, &b); 138 S.add_edge(a, b); 139 } 140 if(S.tarjan()) 141 printf("Case %d: Kalimdor is just ahead\n", ++cnt); 142 else 143 printf("Case %d: The Burning Shadow consume us all\n", ++cnt); 144 } 145 return 0; 146 }
暴力搜索:
1 #include<iostream> 2 #include<algorithm> 3 #include<vector> 4 #include<cstring> 5 #include<cstdio> 6 using namespace std; 7 const int N = 2010; 8 int m,n; 9 bool vis[N][N],vis1[N]; 10 vector< int >mm[N]; 11 void dfs(int m,int x) 12 { 13 if(mm[x].size()==0) return; 14 for(int i=0;i<mm[x].size();i++) 15 { 16 if(vis[m][mm[x][i]] == false) 17 { 18 vis[m][mm[x][i]]=true; 19 int t = mm[x][i]; 20 dfs(m,t); 21 } 22 } 23 } 24 int main() 25 { 26 int T,cas =1,x,y; 27 scanf("%d",&T); 28 while(T--) 29 { 30 scanf("%d %d",&m,&n); 31 for(int i=1;i<=m;i++) 32 mm[i].clear(); 33 for(int i=0; i<n; i++) 34 { 35 scanf("%d %d",&x,&y); 36 mm[x].push_back(y); 37 } 38 memset(vis,false,sizeof(vis)); 39 for(int i=1;i<=m;i++) 40 vis[i][i] = true; 41 for(int i =1;i<=m;i++) dfs(i,i); 42 bool flag = true; 43 for(int i=1;i<=m;i++) 44 { 45 for(int j=1;j<=m;j++) 46 { 47 if(vis[i][j] == false && vis[j][i] == false) 48 { 49 flag = false; 50 goto end; 51 } 52 } 53 } 54 end: if (flag) printf("Case %d: Kalimdor is just ahead\n",cas++); 55 else printf("Case %d: The Burning Shadow consume us all\n",cas++); 56 } 57 return 0; 58 }