洛谷 P3225 [HNOI2012]矿场搭建

传送门

AcWing 396 矿场搭建

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using p = pair<int, int>;
const double pi(acos(-1));
const int inf(0x3f3f3f3f);
const int mod(1e9 + 7);
const int maxn(1e3 + 10);
const int maxm(1e3 + 10);
int ecnt, head[maxn];
int root, dcnt, tim, dfn[maxn], low[maxn];
vector<int> ver[maxn];
bool cut[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)
{
    edges[ecnt].to = v;
    edges[ecnt].nxt = head[u];
    head[u] = ecnt++;
}

void tarjan(int u)
{
    dfn[u] = low[u] = ++tim;
    st.push(u);
    if (u == root and head[u] == -1) {
        ++dcnt;
        ver[dcnt].push_back(u);
        return;
    }
    int cnt = 0;
    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]);
            if (dfn[u] <= low[v]) {
                ++cnt;
                if (u not_eq root or cnt > 1) {
                    cut[u] = true;
                }
                ++dcnt;
                int x = -1;
                do {
                    x = st.top();
                    st.pop();
                    ver[dcnt].push_back(x);
                } while (x not_eq v);
                ver[dcnt].push_back(u);
            }
        } else {
            low[u] = min(low[u], dfn[v]);
        }
    }
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("input.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    int cas = 0;
    while (true) {
        for (int i = 1; i <= dcnt; ++i) {
            ver[i].clear();
        }
        ecnt = dcnt = tim = 0;
        memset(head, -1, sizeof head);
        memset(dfn, 0, sizeof dfn);
        memset(cut, false, sizeof cut);
        int n = 0, m = read();
        if (not m) {
            break;
        }
        while (m--) {
            int u = read(), v = read();
            n = max(max(n, u), v);
            addEdge(u, v);
            addEdge(v, u);
        }
        for (root = 1; root <= n; ++root) {
            if (not dfn[root]) {
                tarjan(root);
            }
        }
        int res = 0;
        ull num = 1;
        for (int i = 1; i <= dcnt; ++i) {
            int cnt = 0;
            for (int j = 0; j < (int)ver[i].size(); ++j) {
                if (cut[ver[i][j]]) {
                    ++cnt;
                }
            }
            if (not cnt) {
                if (ver[i].size() > 1) {
                    res += 2;
                    num *= ver[i].size() * (ver[i].size() - 1) / 2;
                } else {
                    res += 1;
                }
            } else if (cnt == 1) {
                res += 1;
                num *= ver[i].size() - 1;
            }
        }
        printf("Case %d: %d %llu
", ++cas, res, num);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/singularity2u/p/13926455.html