Codeforces Round #731 (Div. 3) G. How Many Paths? 强连通分量缩点+拓扑

Posted kaka0010

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #731 (Div. 3) G. How Many Paths? 强连通分量缩点+拓扑相关的知识,希望对你有一定的参考价值。

原题链接:https://codeforces.ml/contest/1547/problem/G

题意

给你一个n个点,m条边的有向图,可能存在自环。定义了四种状态:

  1. 无法从1到v,输出-1
  2. 有且仅有一条路径到v,输出1
  3. 有不只一条路径到v,输出2
  4. 有无数条路径到v,输出-1

输出 [ 1 , n ] [1,n] [1,n]所有点的状态

分析

遇到有环的图,最常见的做法就是缩点然后在DAG图上跑拓扑。本题的状态转移也是相当明显的,基本上没有什么思维量,看到tag应该就可以写出来了,只需要讨论一下四种状态之间的转移就可以。

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int ul;
typedef pair<ll, ll> PII;
const ll inf = 1e18;
const int N = 4e5 + 10;
const int M = 1e6 + 10;
const ll mod = 1e9 + 7;
const double eps = 1e-8;

#define lowbit(i) (i & -i)
#define Debug(x) cout << (x) << endl
#define fi first
#define se second
#define mem memset
#define endl '\\n'

namespace StandardIO {
    template<typename T>
    inline void read(T &x) {
        x = 0; T f = 1;
        char c = getchar();
        for (; c < '0' || c > '9'; c = getchar()) if (c == '-') f = -1;
        for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
        x *= f;
    }

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

int in[N], f[N], ff[N], cir[N];
int dfn[N], low[N], idx, tp, in_stk[N], vis[N], sd[N], scc;
vector<int> g[N], G[N], bel[N];

void tarjan(int x) {
    low[x] = dfn[x] = ++idx;
    vis[x] = 1;
    in_stk[++tp] = x;
    for (auto v : g[x]) {
        if (!dfn[v]) {
            tarjan(v);
            low[x] = min(low[x], low[v]);
        }
        else if (vis[v]) {
            low[x] = min(low[x], dfn[v]);
        }
    }
    if (low[x] == dfn[x]) {
        int y;
        ++scc;
        while(y = in_stk[tp--]) {
            sd[y] = scc;
            bel[scc].push_back(y);
            vis[y] = 0;
            if (x == y) break;
        }
    }
}
void tupo() {
    queue<int> que;
    for (int i = 1; i <= scc; i++) {
        if (!in[i]) que.push(i);
    }
    f[sd[1]] = 1;
    while (que.size()) {
        int now = que.front();
        que.pop();
        if (cir[now] && f[now] != 0) f[now] = -1;
        for (auto v : G[now]) {
            if (f[now] == 1) {
                if (!f[v]) {
                    f[v] = 1;
                } else if (f[v] == 1) {
                    f[v] = 2;
                }
            } else if (f[now] == 2) {
                if (!f[v] || f[v] == 1) {
                    f[v] = 2;
                }
            } else if (f[now] == -1) {
                f[v] = -1;
            }
            if (--in[v] == 0) {
                que.push(v);
            }
        }
    }
}
inline void solve() {
    int T; cin >> T; while (T--) {
        int n, m; cin >> n >> m;
        for (int i = 1; i <= n; i++) {
            g[i].clear(), G[i].clear(), bel[i].clear();
            in[i] = f[i] = vis[i] = dfn[i] = low[i] = cir[i] = ff[i] = sd[i] = 0;
        }
        tp = idx = scc = 0;
        for (int i = 1; i <= m; i++) {
            int u, v; cin >> u >> v;
            g[u].push_back(v);
        }
        for (int i = 1; i <= n; i++) if (!dfn[i]) tarjan(i);
        for (int i = 1; i <= scc; i++) {
            for (auto u : bel[i]) {
                for (auto v : g[u]) {
                    if (sd[v] != sd[u]) {
                        G[sd[u]].push_back(sd[v]);
                        in[sd[v]]++;
                    } else {
                        cir[i] = 1;
                    }
                }
            }
        }
        tupo();
        for (int i = 1; i <= scc; i++) {
            for (auto it : bel[i]) ff[it] = f[i];
        }
        for (int i = 1; i <= n; i++) cout << ff[i] << ' ';
        cout << endl;
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifdef ACM_LOCAL
    freopen("input", "r", stdin);
    freopen("output", "w", stdout);
    signed test_index_for_debug = 1;
    char acm_local_for_debug = 0;
    do {
        if (acm_local_for_debug == '$') exit(0);
        if (test_index_for_debug > 20)
            throw runtime_error("Check the stdin!!!");
        auto start_clock_for_debug = clock();
        solve();
        auto end_clock_for_debug = clock();
        cout << "Test " << test_index_for_debug << " successful" << endl;
        cerr << "Test " << test_index_for_debug++ << " Run Time: "
             << double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
        cout << "--------------------------------------------------" << endl;
    } while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
    solve();
#endif
    return 0;
}

以上是关于Codeforces Round #731 (Div. 3) G. How Many Paths? 强连通分量缩点+拓扑的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #731 (Div. 3)

Codeforces Round #731 (Div. 3) E题解

Codeforces Round #731 (Div. 3) B. Alphabetical Strings

Codeforces Round #731 (Div. 3) E. Air Conditioners

Codeforces Round #731 (Div. 3) F. Array Stabilization (GCD version)

Codeforces Round #731 (Div. 3) F. Array Stabilization (GCD version)