HDU 1269 - 迷宫城堡 (强连通)

思路

强联通模板题。

代码

#include <cstdio>
#include <stack>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <ctime>
#include <string>
#include <map>
#include <iomanip>
#include <cmath>
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define F first
#define S second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
#define BitCount(x) __builtin_popcount(x)
#define BitCountll(x) __builtin_popcountll(x)
#define LeftPos(x) 32 - __builtin_clz(x) - 1
#define LeftPosll(x) 64 - __builtin_clzll(x) - 1
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
using namespace std;
const double eps = 1e-6;
const int MAXN = 10000 + 10;
const int MOD = 1000007;
const int dir[][2] = { {1, 0}, {-1, 0}, {0, -1}, {0, 1} };
 
int SCCCnt, DFSClock, pre[MAXN], lowlink[MAXN], sccno[MAXN];
stack<int> st;
vector<int> G[MAXN];
 
void DFS(int from)
{
    pre[from] = lowlink[from] = ++DFSClock;
    st.push(from);
    for (int i = 0; i < SZ(G[from]); i++)
    {
        int to = G[from][i];
        if (!pre[to])
        {
            DFS(to);
            lowlink[from] = min(lowlink[from], lowlink[to]);
        }
        else if (!sccno[to]) lowlink[from] = min(lowlink[from], pre[to]);
    }
    if (lowlink[from] == pre[from])
    {
        SCCCnt++;
        while (1)
        {
            int x = st.top(); st.pop();
            sccno[x] = SCCCnt;
            if (x == from) break;
        }
    }
}
 
void FindSCC(int n)
{
    DFSClock = SCCCnt = 0;
    MS(sccno, 0); MS(pre, 0);
    for (int i = 1; i <= n; i++)
        if (!pre[i]) DFS(i);
}
 
int main()
{
    //ROP;
    int n, m, i, j;
    while (scanf("%d%d", &n, &m), n + m)
    {
        for (int i = 0; i <= n; i++) G[i].clear();
        for (i = 0; i < m; i++)
        {
            int from, to;
            scanf("%d%d", &from, &to);
            G[from].PB(to);
        }
        FindSCC(n);
        int tmp = sccno[1];
        for (i = 2; i <= n; i++)
            if (sccno[i] != tmp) break;
        printf("%s\n", i == n + 1 ? "Yes" : "No");
    }
    return 0;
}

Powered by Jekyll and Theme by solid