Thrall’s Dream BFS 实现图之间的访问性



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


#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <stack>
#define LL long long
#define INF 0x7fffffff
#define MAX 200010
#define PI 3.1415926535897932
#define E 2.718281828459045
using namespace std;
int n,m,t;
int vis[2010];
int has[2010][2010];
vector <int> edge[2010];
void BFS(int s)
{
    memset(vis,0,sizeof(vis));              //用vis数组标记访问情况 避免图中有环形成死循环
    vis[s]=1;
    queue<int> q;
    q.push(s);
    while(!q.empty())
    {
        int first=q.front();
        q.pop();
        for(int i=0; i<edge[first].size(); i++)
        {
            int next=edge[first][i];
            if(vis[next]) continue;
            has[s][next]=1;
            vis[next]=1;
            q.push(next);
        }
    }

}
int main()
{
    scanf("%d",&t);
    int cases=1;
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++)
            edge[i].clear();
        memset(has,0,sizeof(has));
        for(int i=1; i<=m; i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            edge[a].push_back(b);   //用容器将这些边存起来
        }
        for(int i=1; i<=n; i++)
        {
            BFS(i);      //把每个点进行连接
        }
        int flag=1;
        for(int i=1; i<=n; i++)
        {
            for(int j=i+1; j<=n; j++)
            {
                if(!has[i][j] && !has[j][i])
                {
                    flag=0;
                    break;
                }
            }
            if (flag==0) break;
        }

        if(flag) printf("Case %d: Kalimdor is just ahead\n",cases++);
        else printf("Case %d: The Burning Shadow consume us all\n",cases++);
    }
}

/*题意:
给t个案列 有n个岛屿 m个连接岛屿的边
问任意两个岛,可不可以从a到b 或者 b到a  */

/*
思路:
把每个点的 与这个点s相连的点 用has[s][next]=1记录 表示这两个岛屿之间可以访问
然后将于与s相连的点放入队列 再将这些点相连的点 也用has[s][next->next]=1记录(例如 1->2  2->3  那么1->3也相通)*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值