• 第四届 山东省ACM B Thrall’s Dream(连通图 OR BFS)


    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. 

    示例输入

    23 21 21 33 21 22 3

    示例输出

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

     

    题意为给定n个点和m条有向边,问能不能任意给出两个点,至少有一条路径可以从a到b。

    has[i][j],表示可以从i到达j, 对于每个点s,进行广搜,记录下has[s][ ] .

    然后只要判断一下对于任意两个点都满足 has[i][j]==1||has[j][i]==1,就可以了。

    #include <iostream>
    #include <stdio.h>
    #include <algorithm>
    #include <string.h>
    #include <stdlib.h>
    #include <cmath>
    #include <iomanip>
    #include <vector>
    #include <set>
    #include <map>
    #include <stack>
    #include <queue>
    #include <cctype>
    #define rd(x) scanf("%d",&x)
    #define rd2(x,y)  scanf("%d%d",&x,&y)
    #define rd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
    using namespace std;
    const int maxn=2010;
    vector<int>g[maxn];
    bool has[maxn][maxn];
    bool vis[maxn];
    int n,m;
    bool ok;
     
    void bfs(int s)
    {
        memset(vis,0,sizeof(vis));
        vis[s]=1;
        queue<int>q;
        q.push(s);
        while(!q.empty())
        {
            int first=q.front();
           // cout<<"fist"<<first<<endl;
            q.pop();
            int len=g[first].size();
            for(int i=0;i<len;i++)
            {
                int v=g[first][i];
                if(vis[v])
                    continue;
                has[s][v]=1;
                q.push(v);
                vis[v]=1;
            }
        }
    }
     
     
    int main()
    {
        int t,c=1;
        rd(t);
        while(t--)
        {
            ok=1;
            rd2(n,m);
            memset(has,0,sizeof(has));
            for(int i=0;i<maxn;i++)
                g[i].clear();
            int u,v;
            while(m--)
            {
                rd2(u,v);
                g[u].push_back(v);
            }
            for(int i=1;i<=n;i++)
            {
                bfs(i);
            }/*
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                    cout<<has[i][j]<<" ";
                cout<<endl;
            }*/
            for(int i=1;i<=n;i++)
            {
                for(int j=i+1;j<=n;j++)
                {
                    if(!has[i][j]&&!has[j][i])
                    {
                        ok=0;
                        break;
                    }
                }
                if(ok==0)
                    break;
            }
            if(ok)
                printf("Case %d: Kalimdor is just ahead
    ",c++);
            else
                printf("Case %d: The Burning Shadow consume us all
    ",c++);
        }
        return 0;
    }

    参考网址:http://blog.csdn.net/sr_19930829/article/details/44925325







  • 相关阅读:
    2016年 河南工业大学校赛 D题.rqy的键盘
    2016年 河南工业大学校赛 C题.魔法宝石
    jqueryMobile导航
    jqueryMobile列表
    jqueryMobile
    停止css3动画
    导航条
    移动端前面必须加的两行代码
    标签页
    file上传图片预览(此方法支持app)
  • 原文地址:https://www.cnblogs.com/zswbky/p/6717896.html
Copyright © 2020-2023  润新知