码迷,mamicode.com
首页 > 其他好文 > 详细

sdutoj Thrall’s Dream

时间:2015-04-28 22:47:38      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

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?

输入

    There are multiple test case in the input file, first line is a case number T. Each test case will begin with two integers N (0 <= N < 2001) and M (0 <= M < 10001), where N is the number of islands and M is number of portal. Next M lines each line contains two integers a and b, indicated there is a portal in island a that people can go from a to b by this portal. The island numbered from 1 to N.

输出

    For each test case, your output should be in one line with “Kalimdor is just ahead” (without quotes, hereinafter the same) if Thrall and Grom can meet or “The Burning Shadow consume us all” otherwise as indicated in the sample output. 

示例输入

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

提示

 

来源

2013年山东省第四届ACM大学生程序设计竞赛

示例程序

 

 

 

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 }
View Code

 

 

官方代码:

 

技术分享
  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 }
View Code

 

sdutoj Thrall’s Dream

标签:

原文地址:http://www.cnblogs.com/jeff-wgc/p/4464181.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!