`
Simone_chou
  • 浏览: 183891 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Thrall's Dream(强连通分量 + Tarjan)

 
阅读更多

Problem B: Thrall's Dream

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 17  Solved: 3
[Submit][Status][Web Board]

Description

Problem 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

 

      题意:

      有 T 个 case,每个 case 都给出 N(0 ~ 2001),M(0 ~ 10001)代表有 N 个节点 M 条单向边。问是否任意两点间 a 和 b ,要么存在 a 到达 b,要么存在 b 到达 a。如果满足条件则输出 Kalimdor is just ahead,不能则输出  The Burning Shadow consume us all。

 

      思路:

      强连通分量 + 拓扑排序。要么 a 能到达 b,要么 b 能达 a,即存在一条直路从起点到终点,这条直路是没有分岔的,如果有分岔的话就不能满足题意了。除此之外,这个图还可能有环,有环则可以用强连通分量缩点表示成一个点,这个点可以存在在拓扑序列中。强连通分量缩点后,再对强连通分量建图,最后判断是否存在唯一确定的拓扑序即可。

 

      AC:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>

using namespace std;

const int NMAX = 2005;
const int EMAX = 10005;

int n, m, ind;
int v[EMAX], fir[NMAX], Next[EMAX];
int scc_cnt, dfs_clock;
int pre[NMAX], low[NMAX], cmp[NMAX];
int deg[NMAX], nscc[NMAX][NMAX];

stack<int> s;

void add_edge(int f, int t) {
        v[ind] = t;
        ++deg[t];
        Next[ind] = fir[f];
        fir[f] = ind;
        ++ind;
}

void dfs(int u) {
        low[u] = pre[u] = ++dfs_clock;
        s.push(u);

        for (int e = fir[u]; e != -1; e = Next[e]) {
                if (!pre[ v[e] ]) {
                        dfs ( v[e] );
                        low[u] = min(low[u], low[ v[e] ]);
                } else if (!cmp[ v[e] ]) {
                        low[u] = min(low[u], pre[ v[e] ]);
                }
        }

        if (low[u] == pre[u]) {
                ++scc_cnt;
                for (;;) {
                        int x = s.top(); s.pop();
                        cmp[x] = scc_cnt;
                        if (x == u) break;
                }
        }
}

void scc() {
        dfs_clock = scc_cnt = 0;
        memset(low, 0, sizeof(low));
        memset(pre, 0, sizeof(pre));
        memset(cmp, 0, sizeof(cmp));

        for (int u = 1; u <= n; ++u) {
                if (!pre[u]) dfs(u);
        }
}

void make_scc() {
        memset(deg, 0, sizeof(deg));
        memset(nscc, 0, sizeof(nscc));

        for (int u = 1; u <= n; ++u) {
                int f = u;
                for (int e = fir[u]; e != -1; e = Next[e]) {
                        int t = v[e];
                        if (cmp[f] != cmp[t] && !nscc[ cmp[f] ][ cmp[t] ]) {
                                nscc[ cmp[f] ][ cmp[t] ] = 1;
                                ++deg[ cmp[t] ];
                        }
                }
        }
}

bool topo() {

        for (int i = 1; i <= scc_cnt; ++i) {
                int ans = 0;

                for (int j = 1; j <= scc_cnt; ++j) {
                        if (!deg[j]) {
                                ++ans;
                                deg[j] = 1;
                                for (int t = 1; t <= scc_cnt; ++t) {
                                        if (nscc[j][t]) --deg[t];
                                }
                        }
                }

                if (ans > 1) return false;
        }

        return true;
}

int main() {
        int t;
        scanf("%d", &t);
        for (int tt = 1; tt <= t; ++tt) {

                ind = 0;
                memset(fir, -1, sizeof(fir));

                scanf("%d%d", &n, &m);
                while (m--) {
                        int f, t;
                        scanf("%d%d", &f, &t);
                        add_edge(f, t);
                }

                scc();

                make_scc();

                printf("Case %d: ", tt);
                if( topo() ) printf("Kalimdor is just ahead\n");
                else printf("The Burning Shadow consume us all\n");
        }
        return 0;
}

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics