noi模板-HDU1269-强连通分量

来源:互联网 发布:linux informix 安装 编辑:程序博客网 时间:2024/06/05 09:47

HDU1269

#include <bits/stdc++.h>using namespace std;#define rep(i, l, r) for (int i = l; i <= r; i++)#define REP(i, l, r) for (int i = l; i >= r; i--)#define MAXN 100010int n, m, first[MAXN], nxt[MAXN], N = -1, dfn[MAXN], low[MAXN], belong[MAXN], cnt, Time;stack<int> s;bool vis[MAXN];struct edge {int x, y;} a[MAXN];inline int read() {    int x = 0, f = 1; char ch = getchar();    while (ch<'0' || ch>'9') {if (ch == '-') f = -1; ch = getchar();}    while (ch>='0' && ch<='9') x = x*10 + ch - '0', ch = getchar();    return x*f;}inline void add(int x, int y) {a[++N].x = x, a[N].y = y, nxt[N] = first[x], first[x] = N;}inline void tarjan(int x) {    dfn[x] = low[x] = ++Time;    s.push(x); vis[x] = 1;    for (int i = first[x], y = a[i].y; ~i; i = nxt[i])        if (!dfn[y]) tarjan(y), low[x] = min(low[x], low[y]);        else if (vis[y]) low[x] = min(low[x], dfn[y]);    if (dfn[x] == low[x]) {        cnt++;        int y;        do {            y = s.top(); s.pop();            vis[y] = 0;            belong[y] = cnt;        }while (x != y);    }}inline bool equiv(int *a, int n) {    int t = a[1];    rep(i, 2, n) if (a[i] != t) return 0;    return 1;}int main() {    while (n = read()) {        m = read();        N = -1; memset(first, -1, sizeof(first));        rep(i, 1, m) {            int tx = read(), ty = read();            add(tx, ty);        }        memset(dfn, 0, sizeof(dfn));        memset(vis, 0, sizeof(vis));        Time = cnt = 0;        rep(i, 1, n) if (!dfn[i]) tarjan(i);        puts(equiv(belong, n) ? "Yes" : "No");    }        return 0;}


0 0
原创粉丝点击