#HDU 1269 Maze Castle (tarjan)

Problem Description

In order to train small Greek sense of direction, Gardon built a large castle, inside the room there are N (N <= 10000) and M channels (M <= 100000), each channel is unidirectional, that if called a passage communicating the room a and room B, a room B can be reached only by the described rooms a through this channel, but does not indicate the room can reach a B room through it. Gardon need to ask you to write a program to confirm whether any two rooms are interconnected, namely: for any i and j, there is at least one path from room to room j i, j may also exist a path from room to the room i.

 

 

Input

The first line comprises a plurality of sets of input data, there are two input numbers: N and M, the next M lines each with two numbers, a and B, may represent a channel from A to B room room. File and end with two zeroes.

 

 

Output

For each set of input data, if any two rooms are connected with each other, and outputs "Yes", and otherwise outputs "No".

 

 

Sample Input

 

3 3 1 2 2 3 3 1 3 3 1 2 2 3 3 2 0 0

 

 

Sample Output

 

Yes No

Title effect Needless to say, given a, it is determined whether any two communication to FIG.

Ideas: first saw this question that is Floyed, after reading the data gave up the idea, then you can think about is not the first point and then floyed shrink it? Then ah knock knock, we found that, in fact, not so much trouble. . . . Are able to communicate at any point, there is only one case, that he himself is a ring, if not a ring, somehow out of or into is 0, then he will not be able to reach other points or other points of arrival. So the direct reduction point of view is not only a new point Jiuhaola! Just learning floyed think it is consequently now see floyedQAQ

AC Code:

#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
const int maxn = 1e5 + 5;

struct node
{
    int v, next;
}e[maxn];
int dfn[maxn], low[maxn], n, m, cnt, tot;
int head[maxn], in[maxn], suo[maxn], scnt;
bool vis[maxn];
stack <int> st;
void init() {
    memset(head, -1, sizeof(head));
    memset(e, 0, sizeof(e));
    memset(suo, 0, sizeof(suo));
    memset(dfn, 0, sizeof(dfn));
    memset(low, 0, sizeof(low));
    memset(vis, 0, sizeof(vis));
    memset(in, 0, sizeof(in));
    cnt = tot = scnt = 0;
}
void add (int from, int to) {
    e[++cnt].v = to;
    e[cnt].next = head[from];
    head[from] = cnt;
}
void tarjan(int x) {
    dfn[x] = low[x] = ++tot;
    vis[x] = 1;
    st.push(x);
    for (int i = head[x]; i != -1; i = e[i].next) {
        if (!dfn[e[i].v]) {
            tarjan(e[i].v);
            low[x] = min (low[x], low[e[i].v]);
        }
        else if (vis[e[i].v]) low[x] = min (low[x], dfn[e[i].v]);
    }
    if (dfn[x] == low[x]) {
        scnt++;
        int k;
        do {
            k = st.top();
            st.pop();
            suo[k] = scnt;
            vis[k] = 0;
        }
        while (k != x);
    }
}

int main()
{
    while (cin >> n >> m && n + m) {
        init();
        for (int i = 0; i < m; i++) {
            int ui, vi;
            cin >> ui >> vi;
            add (ui, vi);
        }
        for (int i = 1; i <= n; i++) {
            if (!dfn[i]) tarjan(i);
        }
        if (scnt == 1) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_43851525/article/details/91363033