题目描述
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
两个人任意落到岛上,岛之间用有向路连接,求只两个人是否能相遇。只要一个人能走到另一个人那就算相遇
思路:用bfs遍历图,求出能互相到达的小岛。再枚举所有小岛
#include <iostream>
#include <vector>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=2005;
typedef long long LL;
vector<short> g[maxn];
bool can[maxn][maxn];
int n,m;
bool judge()
{
for(int i=1; i<=n; ++i)
for(int j=i+1; j<=n; ++j)
if(!can[i][j]&&!can[j][i])
return false;
return true;
}
void bfs(int x)
{
queue<short> que;
que.push(x);
while(!que.empty())
{
short q=que.front();
que.pop();
if(q<x)
{
for(int i=1; i<=n; ++i)
if(can[q][i])
can[x][i]=true;
}
else
{
for(int i=0; i<g[q].size(); ++i)
{
int y=g[q][i];
if(!can[x][y])
{
can[x][y]=true;
que.push(y);
}
}
}
}
}
int main()
{
int T,kase=0;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=1; i<=n; ++i)
g[i].clear();
memset(can,0,sizeof(can));
for(int i=1; i<=m; ++i)
{
int x,y;
scanf("%d%d",&x,&y);
g[x].push_back(y);
}
for(int i=1; i<=n; ++i)
bfs(i);
if(judge())
printf("Case %d: Kalimdor is just ahead\n",++kase);
else
printf("Case %d: The Burning Shadow consume us all\n",++kase);
}
return 0;
}