답안 #716988

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
716988 2023-03-31T17:09:47 Z stevancv Pipes (CEOI15_pipes) C++14
40 / 100
2517 ms 40472 KB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e5 + 2;
const int inf = 1e9;
struct Dsu {
    int link[N], sz[N];
    void Reset(int n) {
        for (int i = 1; i <= n; i++) {
            link[i] = i;
            sz[i] = 1;
        }
    }
    int Find(int x) {
        if (x == link[x]) return x;
        return link[x] = Find(link[x]);
    }
    int GetSz(int x) {
        return sz[Find(x)];
    }
    void Unite(int u, int v) {
        u = Find(u); v = Find(v);
        if (u == v) return;
        if (sz[u] < sz[v]) swap(u, v);
        link[v] = u;
        sz[u] += sz[v];
    }
}dsu;
vector<int> g[N];
int par[N][17], dep[N], su[N];
void Dfs(int s, int e) {
    par[s][0] = e;
    dep[s] = dep[e] + 1;
    for (int i = 1; i < 17; i++) par[s][i] = par[par[s][i - 1]][i - 1];
    for (int u : g[s]) {
        if (u == e) continue;
        Dfs(u, s);
    }
}
int Lca(int u, int v) {
    if (dep[u] < dep[v]) swap(u, v);
    int r = dep[u] - dep[v];
    for (int i = 0; i < 17; i++) {
        if ((1 << i) & r) u = par[u][i];
    }
    if (u == v) return u;
    for (int i = 16; i >= 0; i--) {
        if (par[u][i] != par[v][i]) {
            u = par[u][i];
            v = par[v][i];
        }
    }
    return par[u][0];
}
int Solve(int s, int e) {
    for (int u : g[s]) {
        if (u == e) continue;
        int x = Solve(u, s);
        if (x == 0) cout << s << sp << u << en;
        su[s] += x;
    }
    return su[s];
}
vector<pair<int, int>> gg[102];
vector<int> ans;
int Dfs1(int s, int e) {
    int d = dep[s];
    for (auto u : gg[s]) {
        if (u.second == e) continue;
        int v = u.first;
        if (dep[v] == inf) {
            dep[v] = dep[s] + 1;
            int x = Dfs1(v, u.second);
            if (x == dep[v]) ans.push_back(u.second);
            else smin(d, x);
        }
        else smin(d, dep[v]);
    }
    return d;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    if (n <= 100 && m <= 200) {
        vector<int> u(m), v(m);
        for (int i = 0; i < m; i++) {
            cin >> u[i] >> v[i];
            gg[u[i]].push_back({v[i], i});
            gg[v[i]].push_back({u[i], i});
        }
        for (int i = 1; i <= n; i++) dep[i] = inf;
        for (int i = 1; i <= n; i++) {
            if (dep[i] == inf) {
                dep[i] = 0;
                Dfs1(i, -1);
            }
        }
        for (int i : ans) cout << u[i] << sp << v[i] << en;
        return 0;
    }
    dsu.Reset(n);
    for (int i = 1; i <= m; i++) {
        int u, v;
        cin >> u >> v;
        if (dsu.Find(u) == dsu.Find(v)) {
            su[u] += 1;
            su[v] += 1;
            su[Lca(u, v)] -= 2;
            continue;
        }
        if (dsu.GetSz(u) < dsu.GetSz(v)) swap(u, v);
        dsu.Unite(u, v);
        Dfs(v, u);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for (int i = 1; i <= n; i++) {
        if (dep[i] == 0) Solve(i, 0);
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 3156 KB Output is correct
2 Correct 8 ms 3156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 144 ms 3088 KB Output is correct
2 Correct 129 ms 3028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 272 ms 3776 KB Output is correct
2 Correct 302 ms 3748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 463 ms 5332 KB Output is correct
2 Runtime error 393 ms 19860 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 802 ms 10680 KB Output is correct
2 Runtime error 684 ms 28744 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1192 ms 11852 KB Output is correct
2 Runtime error 1133 ms 24484 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1646 ms 13900 KB Output is correct
2 Runtime error 1744 ms 30060 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1949 ms 13900 KB Output is correct
2 Runtime error 2433 ms 35112 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2517 ms 13468 KB Output is correct
2 Runtime error 2347 ms 40472 KB Memory limit exceeded
3 Halted 0 ms 0 KB -