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代码:
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int a[],b[],c[];
int main()
{
int T,cnt=;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d %d",&n,&m);
memset(a,,sizeof(a));
memset(b,,sizeof(b));
memset(c,,sizeof(c));
if(n<)
{
printf("Case %d: Kalimdor is just ahead\n",cnt++);
}
else
{
int x,y;
while(m--)
{
scanf("%d %d",&x,&y);
a[x]--;
a[y]++;
c[x]=;
c[y]=;
}
int flag=,t=;
for(int i=;i<=n;i++)
{
if(!c[i])
{
flag=;
break;
}
if(a[i]!=)
t++;
}
if(!flag)
printf("Case %d: The Burning Shadow consume us all\n",cnt++);
else
{
if(t<=)
printf("Case %d: Kalimdor is just ahead\n",cnt++);
else
printf("Case %d: The Burning Shadow consume us all\n",cnt++);
}
}
}
return ;
}
官方代码:
#include <cstdio>
#include <cstring> const int maxn = ;
const int maxm = ; struct TOPO
{
int net[maxn], in[maxn], ans[maxn];
int nv, size;
struct edge
{
int v, next;
edge(){}
edge(int a, int b){v = a; next = b;}
}E[maxm];
inline void init(int n)
{
memset(in, , sizeof(in));
memset(net, -, sizeof(net));
nv = n;
size = ;
}
inline void add_edge(int u, int v)
{
E[size] = edge(v, net[u]);
net[u] = size++;
in[v]++;
}
bool toposort()
{
int num; for(int i = ; i <= nv; i++)
{
int j = ;
num = ;
for(int k = ; k <= nv; k++)
if(!in[k])
num++;
if(num > )
return false;
while(in[j] != )
j++;
in[j] = -;
ans[i] = j;
for(int k = net[j]; k != -; k = E[k].next)
in[E[k].v]--;
}
return true;
}
}T; struct SCC
{
int net[maxn], dfn[maxn], low[maxn], s[maxn], belong[maxn];
int count, ans, top, size, nv;
bool ins[maxn];
struct edge
{
int v, next;
edge(){}
edge(int a, int b){v = a; next = b;}
}E[maxm];
inline void init(int n)
{
memset(dfn, -, sizeof(dfn));
memset(net, -, sizeof(net));
memset(ins, false, sizeof(ins));
count = ans = size = ;
top = -;
nv = n;
}
inline void add_edge(int u, int v)
{
E[size] = edge(v, net[u]);
net[u] = size++;
}
void dfs(int u)
{
int v; dfn[u] = low[u] = ++count;
ins[u] = true;
s[++top] = u;
for(int i = net[u]; i != -; i = E[i].next)
{
v = E[i].v;
if(dfn[v] == -)
{
dfs(v);
if(low[v] < low[u])
low[u] = low[v];
}
else if(ins[v] && dfn[v] < low[u])
low[u] = dfn[v];
}
if(dfn[u] == low[u])
{
ans++;
do
{
v = s[top--];
belong[v] = ans;
ins[v] = false;
}while(u != v);
}
}
bool tarjan()
{
for(int i = ; i <= nv; i++)
if(dfn[i] == -)
dfs(i);
T.init(ans);
for(int i = ; i <= nv; i++)
for(int j = net[i]; j != -; j = E[j].next)
{
int v = E[j].v;
if(belong[i] != belong[v])
T.add_edge(belong[i], belong[v]);
}
return T.toposort();
}
}S; int main()
{
int t, n, m, a, b, cnt = ; scanf("%d", &t);
while(t--)
{
scanf("%d %d", &n, &m);
S.init(n);
while(m--)
{
scanf("%d %d", &a, &b);
S.add_edge(a, b);
}
if(S.tarjan())
printf("Case %d: Kalimdor is just ahead\n", ++cnt);
else
printf("Case %d: The Burning Shadow consume us all\n", ++cnt);
}
return ;
}
暴力搜索:
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#include<cstdio>
using namespace std;
const int N = ;
int m,n;
bool vis[N][N],vis1[N];
vector< int >mm[N];
void dfs(int m,int x)
{
if(mm[x].size()==) return;
for(int i=;i<mm[x].size();i++)
{
if(vis[m][mm[x][i]] == false)
{
vis[m][mm[x][i]]=true;
int t = mm[x][i];
dfs(m,t);
}
}
}
int main()
{
int T,cas =,x,y;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&m,&n);
for(int i=;i<=m;i++)
mm[i].clear();
for(int i=; i<n; i++)
{
scanf("%d %d",&x,&y);
mm[x].push_back(y);
}
memset(vis,false,sizeof(vis));
for(int i=;i<=m;i++)
vis[i][i] = true;
for(int i =;i<=m;i++) dfs(i,i);
bool flag = true;
for(int i=;i<=m;i++)
{
for(int j=;j<=m;j++)
{
if(vis[i][j] == false && vis[j][i] == false)
{
flag = false;
goto end;
}
}
}
end: if (flag) printf("Case %d: Kalimdor is just ahead\n",cas++);
else printf("Case %d: The Burning Shadow consume us all\n",cas++);
}
return ;
}