Hrbust 2048 Thrall’s Dream【强连通+拓扑排序+思维】好题!

Thrall’s Dream
Time Limit: 1000 MSMemory Limit: 32768 K
Total Submit: 70(21 users)Total Accepted: 18(13 users)Rating: Special Judge: No
Description

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
Source
HCPC2014校赛训练赛 2

题目大意:

问这个有向图是不是一个单向联通图。

单向联通图:任意两点,要么有一条路径从u到v,要么有一条路径从v到u;


思路:


1、对于一个DAG图(邮箱无环图),我们分析两种情况:

对于这样一个图,如果没有黄色边,那么此图的拓扑排序过程中有拆一个点得到两个点的情况,那么对应这种情况显然就是不行的。

那么如果有这条黄色边,那么此图在拓扑排序过程中就只有拆一个点得到一个点的情况。

那么我们不难得到结论:

①对于一个DAG图,如果其开始的时候度为0的节点只有一个。

②并且在拓扑排序过程中拆一个点最多得到一个点的话。

③那么其就满足单向联通图的条件。否则就不满足。


2、那么给出的有向图肯定是有可能包含环的存在的。

对于一个环,其一定属于一个强连通分量,那么其中所有的点,肯定满足单向联通子图的条件,那么我们只要将所有强连通分量找到,并且进行缩点染色,那么我们就能得到一个DAG图。


3、那么整个题就解决了,对于图论题,坑点肯定要想是否有重边存在,对于这个题,如果有重边存在肯定影响一个缩了点之后的点的度数有影响,那么一定要考虑进去。

然而这个题并没有设这个坑点,考虑到了肯定更好(而且图论题不想重边怎么行);


Ac代码:

#include<stdio.h>
#include<queue>
#include<string.h>
#include<vector>
using namespace std;
struct node
{
    int from;
    int to;
    int next;
} e[2000000];
vector<int >mp[20500];
int map[2050][2050];
int vis[20500];
int dfn[20500];
int low[20500];
int stack[20500];
int color[20500];
int head[20500];
int degree[20650];
int cont;
int n,m,tot,cnt,sig;
void init()
{
    sig=0;
    cnt=1;
    tot=-1;
    cont=0;
    memset(degree,0,sizeof(degree));
    memset(head,-1,sizeof(head));
    memset(color,0,sizeof(color));
    memset(stack,0,sizeof(stack));
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(vis,0,sizeof(vis));
}
void add(int from,int to)
{
    e[cont].to=to;
    e[cont].next=head[from];
    head[from]=cont++;
}
void Tarjan(int u)
{
    vis[u]=1;
    dfn[u]=low[u]=cnt++;
    stack[++tot]=u;
    for(int i=0; i<mp[u].size(); i++)
    {
        int v=mp[u][i];
        if(vis[v]==0)Tarjan(v);
        if(vis[v]==1)low[u]=min(low[u],low[v]);
    }
    if(dfn[u]==low[u])
    {
        sig++;
        do
        {
            color[stack[tot]]=sig;
            vis[stack[tot]]=-1;
        }
        while(stack[tot--]!=u);
    }
}
int top()
{
    queue<int >s;
    for(int i=1; i<=sig; i++)
    {
        if(degree[i]==0)s.push(i);
    }
    if(s.size()==1)
    {
        while(!s.empty())
        {
            int cnt=0;
            int u=s.front();
            s.pop();
            for(int i=head[u]; i!=-1; i=e[i].next)
            {
                int v=e[i].to;
                degree[v]--;
                if(degree[v]==0)
                {
                    cnt++;
                    s.push(v);
                }
            }
            if(cnt>1)return 0;
        }
        return 1;
    }
    else return 0;
}
int Slove()
{
    init();
    for(int i=1; i<=n; i++)
    {
        if(vis[i]==0)Tarjan(i);
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=0; j<mp[i].size(); j++)
        {
            int v=mp[i][j];
            if(color[i]!=color[v])
            {
                add(color[i],color[v]);
                degree[color[v]]++;
            }
        }
    }
    if(top()==1)return 1;
    else return 0;
}
int main()
{
    int kase=0;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(map,0,sizeof(map));
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++)mp[i].clear();
        for(int i=0; i<m; i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            if(map[x][y]==0)
                mp[x].push_back(y);
            map[x][y]=1;
        }
        int tmp=Slove();
        if(tmp==1)printf("Case %d: Kalimdor is just ahead\n",++kase);
        if(tmp==0)printf("Case %d: The Burning Shadow consume us all\n",++kase);
    }
}









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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值