zoukankan      html  css  js  c++  java
  • 洛谷 P2272 [ZJOI2007]最大半连通子图

    传送门

    AcWing 1175 最大半连通子图

    #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 maxn(1e5 + 10);
    const int maxm(2e6 + 10);
    int ecnt, head[maxn], shead[maxn];
    int tim, dfn[maxn], low[maxn];
    int scnt, scc[maxn], siz[maxn];
    int f[maxn], g[maxn];
    bool vis[maxn];
    stack<int> st;
    unordered_set<ll> uset;
    
    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)
    {
        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];
            } while (v not_eq u);
        }
    }
    
    int main()
    {
    #ifdef ONLINE_JUDGE
    #else
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
        ios::sync_with_stdio(false);
        memset(head, -1, sizeof head);
        memset(shead, -1, sizeof shead);
        int n = read(), m = read(), mod = read();
        while (m--) {
            int u = read(), v = read();
            addEdge(u, v, false);
        }
        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];
                ll hs = 1000000ll * a + b;
                if (a not_eq b and not uset.count(hs)) {
                    addEdge(a, b, true);
                    uset.insert(hs);
                }
            }
        }
        for (int u = scnt; u; --u) {
            if (not f[u]) {
                f[u] = siz[u];
                g[u] = 1;
            }
            for (int i = shead[u]; compl i; i = edges[i].nxt) {
                int v = edges[i].to;
                if (f[v] < f[u] + siz[v]) {
                    f[v] = f[u] + siz[v];
                    g[v] = g[u];
                } else if (f[v] == f[u] + siz[v]) {
                    g[v] = (g[v] + g[u]) % mod;
                }
            }
        }
        int mx = 0, sum = 0;
        for (int i = 1; i <= scnt; ++i) {
            if (f[i] > mx) {
                mx = f[i];
                sum = g[i];
            } else if (f[i] == mx) {
                sum = (sum + g[i]) % mod;
            }
        }
        write(mx, true);
        write(sum, true);
        return 0;
    }
    
  • 相关阅读:
    扫雷游戏:C++生成一个扫雷底板
    为《信息学奥赛一本通》平反(除了OJ很差和代码用宋体)
    关于最大公约数欧几里得算法辗转相除及最小公倍数算法拓展C++学习记录
    【Python】如何用较快速度用Python程序增加博客访问量——CSDN、博客园、知乎(应该包括简书)
    STL学习笔记之next_permutation函数
    C++集合容器STL结构Set
    11111111111
    express + mysql实践
    express实践
    ws:一个 Node.js WebSocket 库
  • 原文地址:https://www.cnblogs.com/singularity2u/p/13908387.html
Copyright © 2011-2022 走看看