Tarjan 算法

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using p = pair<int, int>;
const double pi(acos(-1));
const int inf(0x3f3f3f3f);
const int mod(1e9 + 7);
const int maxn(2e4 + 10);
const int maxm(2e5 + 10);
int ecnt, head[maxn];
int tim, dfn[maxn], low[maxn];
int ccnt;
bool cut[maxn];

struct edge {
    int to, nxt;
} edges[maxm];

template<typename T = int>
inline const T read()
{
    T 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 << 3) + (x << 1) + ch - '0';
        ch = getchar();
    }
    return x * f;
}

template<typename T>
inline void write(T x, bool ln)
{
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10, false);
    putchar(x % 10 + '0');
    if (ln) putchar(10);
}

void addEdge(int u, int v)
{
    edges[ecnt].to = v;
    edges[ecnt].nxt = head[u];
    head[u] = ecnt++;
}

void tarjan(int cur, int pre)
{
    int child = 0;
    dfn[cur] = low[cur] = ++tim;
    for (int i = head[cur]; compl i; i = edges[i].nxt) {
        int nxt = edges[i].to;
        if (not dfn[nxt]) {
            ++child;
            tarjan(nxt, cur);
            low[cur] = min(low[cur], low[nxt]);
            if (pre not_eq cur and low[nxt] >= dfn[cur] and not cut[cur]) {
                ++ccnt;
                cut[cur] = true;
            }
        } else if (nxt not_eq pre) {
            low[cur] = min(low[cur], dfn[nxt]);
        }
    }
    if (pre == cur and child >= 2) {
        ++ccnt;
        cut[cur] = true;
    }
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("input.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    memset(head, -1, sizeof head);
    int n = read(), m = read();
    while (m--) {
        int u = read(), v = read();
        addEdge(u, v);
        addEdge(v, u);
    }
    for (int i = 1; i <= n; ++i) {
        if (not dfn[i]) {
            tarjan(i, i);
        }
    }
    write(ccnt, true);
    for (int i = 1; i <= n; ++i) {
        if (cut[i]) {
            write(i, false);
            putchar(32);
        }
    }
    return 0;
}
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using p = pair<int, int>;
const double pi(acos(-1));
const int inf(0x3f3f3f3f);
const int mod(1e9 + 7);
const int maxn(1e4 + 10);
const int maxm(2e5 + 10);
int ecnt, head[maxn], shead[maxn];
int tim, dfn[maxn], low[maxn];
int scnt, scc[maxn], siz[maxn];
int w[maxn], sum[maxn], deg[maxn];
bool vis[maxn];
stack<int> st;

struct edge {
    int to, nxt;
} edges[maxm];

template<typename T = int>
inline const T read()
{
    T 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 << 3) + (x << 1) + ch - '0';
        ch = getchar();
    }
    return x * f;
}

template<typename T>
inline void write(T x, bool ln)
{
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10, false);
    putchar(x % 10 + '0');
    if (ln) putchar(10);
}

void addEdge(int u, int v, bool s = false)
{
    edges[ecnt].to = v;
    if (s) {
        edges[ecnt].nxt = shead[u];
        shead[u] = ecnt++;
    } else {
        edges[ecnt].nxt = head[u];
        head[u] = ecnt++;
    }
}

void tarjan(int u)
{
    dfn[u] = low[u] = ++tim;
    st.push(u);
    vis[u] = true;
    for (int i = head[u]; compl i; i = edges[i].nxt) {
        int v = edges[i].to;
        if (not dfn[v]) {
            tarjan(v);
            low[u] = min(low[u], low[v]);
        } else if (vis[v]) {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if (dfn[u] == low[u]) {
        ++scnt;
        int v = 0;
        do {
            v = st.top();
            st.pop();
            vis[v] = false;
            scc[v] = scnt;
            ++siz[scnt];
            sum[scnt] += w[v];
        } while (v not_eq u);
    }
}

int dfs(int u)
{
    int mx = 0;
    for (int i = shead[u]; compl i; i = edges[i].nxt) {
        int v = edges[i].to;
        mx = max(mx, dfs(v));
    }
    return sum[u] + mx;
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("input.txt", "r", stdin);
#endif
    memset(head, -1, sizeof head);
    memset(shead, -1, sizeof shead);
    int n = read(), m = read();
    for (int i = 1; i <= n; ++i) {
        w[i] = read();
    }
    while (m--) {
        int u = read(), v = read();
        addEdge(u, v);
    }
    for (int i = 1; i <= n; ++i) {
        if (not dfn[i]) {
            tarjan(i);
        }
    }
    for (int u = 1; u <= n; ++u) {
        for (int i = head[u]; compl i; i = edges[i].nxt) {
            int v = edges[i].to;
            int a = scc[u], b = scc[v];
            if (a not_eq b) {
                addEdge(a, b, true);
                ++deg[b];
            }
        }
    }
    int res = 0;
    for (int i = 1; i <= scnt; ++i) {
        if (not deg[i]) {
            res = max(res, dfs(i));
        }
    }
    write(res, true);
    return 0;
}
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using p = pair<int, int>;
const double pi(acos(-1));
const int inf(0x3f3f3f3f);
const int mod(1e9 + 7);
const int maxn(2e6 + 10);
const int maxm(2e6 + 10);
int ecnt, head[maxn];
int tim, dfn[maxn], low[maxn];
int scnt, id[maxn];
bool vis[maxn];
stack<int> st;

struct edge {
    int to, nxt;
} edges[maxm];

template<typename T = int>
inline const T read()
{
    T 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 << 3) + (x << 1) + ch - '0';
        ch = getchar();
    }
    return x * f;
}

template<typename T>
inline void write(T x, char c = false)
{
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10, false);
    putchar(x % 10 + '0');
    if (c) putchar(c);
}

void addEdge(int u, int v)
{
    edges[ecnt].to = v;
    edges[ecnt].nxt = head[u];
    head[u] = ecnt++;
}

void tarjan(int u)
{
    dfn[u] = low[u] = ++tim;
    st.push(u);
    vis[u] = true;
    for (int i = head[u]; compl i; i = edges[i].nxt) {
        int v = edges[i].to;
        if (not dfn[v]) {
            tarjan(v);
            low[u] = min(low[u], low[v]);
        } else if (vis[v]) {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if (dfn[u] == low[u]) {
        int v = -1;
        ++scnt;
        do {
            v = st.top();
            st.pop();
            vis[v] = false;
            id[v] = scnt;
        } while (u not_eq v);
    }
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("input.txt", "r", stdin);
#endif
    int n = read(), m = read();
    memset(head, -1, sizeof head);
    while (m--) {
        int u = read() - 1, a = read(), v = read() - 1, b = read();
        addEdge(u * 2 + not a, v * 2 + b);
        addEdge(v * 2 + not b, u * 2 + a);
    }
    for (int i = 0; i < n * 2; ++i) {
        if (not dfn[i]) {
            tarjan(i);
        }
    }
    bool flag = false;
    for (int i = 0; i < n; ++i) {
        if (id[i * 2] == id[i * 2 + 1]) {
            flag = true;
            break;
        }
    }
    if (flag) {
        puts("IMPOSSIBLE");
    } else {
        puts("POSSIBLE");
        for (int i = 0; i < n; ++i) {
            if (id[i * 2] < id[i * 2 + 1]) {
                write(0, ' ');
            } else {
                write(1, ' ');
            }
        }
    }
    return 0;
}

最后更新于