Thrall’s Dream

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?

 

 

Input

 

 

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.

 

 

Output

 

 

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.

 

 

Sample Input

2
3 2
1 2
1 3
3 2
1 2
2 3

Sample Output

Case 1: The Burning Shadow consume us all
Case 2: Kalimdor is just ahead

题意:n个点,m条单项的道路,判断是否每两个点都有道路相连
题解:tarjan算法求强连通分量缩点然后判断缩点后能否构成一条链即可,一条链的条件为存在一个点入度为1,一个点出度为1,其余的点的入度=出度=1;
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stack>
using namespace std;
struct lmx{
 int u;
 int v;
 int next;
}a[10005];
stack<int> st;
bool mm[2005][2005];
int dfn[2005],low[2005],head[2005],instack[2005];
int n,m,ret,ans,ti;
int flag[2005];
int du[2005][2];
void add(int u,int v)
{
 a[ret].u=u;
 a[ret].v=v;
 a[ret].next=head[u];
 head[u]=ret++;
}
void init()
{
memset(dfn,0,sizeof(dfn));
  memset(instack,0,sizeof(instack));
  memset(head,-1,sizeof(head));
}
void tarjan(int s)
{
 dfn[s]=low[s]=++ti;
 instack[s]=1;
 st.push(s);
 int k;
 for(int i=head[s];i!=-1;i=a[i].next)
 {
  int k=a[i].v;
  if(!dfn[k])
  {
   tarjan(k);
   if(low[s]>low[k]) low[s]=low[k];
  }
  else if(instack[k]&&low[s]>dfn[k]) low[s]=dfn[k];
 }
 if(dfn[s]==low[s])
 {
  ans++;
  do{
   k=st.top();
   st.pop();
   instack[k]=0;
   flag[k]=ans;
  }while(k!=s);
 }
}
bool is()
{
 if(ans==1) return true;
 int i;
 memset(du,0,sizeof(du));
 for(i=0;i<m;i++)
 {
  int u=flag[a[i].u],v=flag[a[i].v];
  if(u==v) continue;
  du[u][0]++;
  du[v][1]++;
 }
 int c1=0,c2=0,c3=0;
 for(i=1;i<=ans;i++)
 {
        if(du[i][0]==1&&du[i][1]==0) c1++;
  if(du[i][0]==0&&du[i][1]==1) c2++;
  if(du[i][0]==1&&du[i][1]==1) c3++;
 }
 return (c1==1&&c2==1&&(ans-2==c3));
}
int main()
{
    int test,i,j,ca=0;
 scanf("%d",&test);
 while(test--)
 {
  ca++;
  scanf("%d%d",&n,&m);
     init();
  memset(mm,false,sizeof(mm));
  ret=0;ans=0;ti=0;
  for(i=0;i<m;i++)
  {
   int a,b;
   scanf("%d%d",&a,&b);
   mm[a][b]=true;
  }
  for(i=1;i<=n;i++)
  {
   for(j=1;j<=n;j++)
   {
    if(mm[i][j]) add(i,j);
   }
  }
        for(i=1;i<=n;i++)
  {
   if(!dfn[i]) tarjan(i);
  }
  bool fla=is();
  printf("Case %d: ",ca);
  if(fla) puts("Kalimdor is just ahead");
  else puts("The Burning Shadow consume us all");
 }
 return 0;
}
posted @ 2013-10-11 03:03  forevermemory  阅读(560)  评论(0编辑  收藏  举报