Submission #906613

# Submission time Handle Problem Language Result Execution time Memory
906613 2024-01-14T14:57:40 Z codefox Pipes (CEOI15_pipes) C++14
80 / 100
828 ms 65536 KB
#include <bits/stdc++.h>

using namespace std;

#define pii pair<int, int>

vector<vector<int>> graph;
vector<int> rep;
vector<int> comp;
vector<int> low;
vector<int> num;
vector<int> complow;
vector<vector<int>> lift;
vector<int> depth;
int c = 0;

int finde(int i)
{
    if (i != rep[i]) rep[i] = finde(rep[i]);
    return rep[i];
}

int finde2(int i)
{
    if (i != comp[i]) comp[i] = finde2(comp[i]);
    return comp[i];
}

void dfs(int i, int d)
{
    num[i] = ++c;
    depth[i] = d;
    for (int ele:graph[i])
    {
        if (num[ele]) continue;
        lift[0][ele] = i;
        dfs(ele, d+1);
    }
}

void dfs2(int i)
{
    num[i] = low[i] = ++c;
    low[i] = complow[finde2(i)];
    for (int ele:graph[i])
    {
        if (num[ele]) continue;
        dfs2(ele);
        if (low[ele]==num[ele])  
        {
            cout << i+1 << " " << ele+1 << "\n";
        }
        low[i] = min(low[i], low[ele]);
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int n, m;
    cin >> n >> m;
    num.assign(n, 0);
    low.assign(n, 0);
    rep.assign(n, 0);
    comp.assign(n, 0);
    complow.assign(n, 1e9);
    graph.assign(n, vector<int>());
    iota(rep.begin(), rep.end(), 0);
    iota(comp.begin(), comp.end(), 0);
    for (int i = 0; i < m; i++)
    {
        int a, b;
        cin >> a >> b;
        a--; b--;
        if (finde(a)!= finde(b))
        {
            rep[finde(a)] = finde(b);
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        else
        {
            if (finde2(a) != finde2(b))
            {
                comp[finde2(a)] = finde2(b);
            }
        }
    }
    rep.clear();
    lift = vector<vector<int>>(18, vector<int>(n, -1));
    depth.assign(n, 0);

    for (int i = 0; i < n; i++)
    {
        c = 0;
        if (!num[i]) dfs(i, 0);
    }

    for (int i = 1; i < 18; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (lift[i-1][j]==-1) continue;
            lift[i][j] = lift[i-1][lift[i-1][j]];
        }
    }

    for (int i =0; i < n; i++)
    {
        if (complow[finde2(i)]==1e9) complow[finde2(i)]=i;
        else
        {
            int a = complow[finde2(i)];
            int b = i;
            if (depth[b]>depth[a])swap(a, b);
            int diff = depth[a]-depth[b];
            for (int i = 17; i >= 0; i--)
            {
                if (diff < (1<<i)) continue;
                diff -= 1<<i;
                a = lift[i][a];
            }
            if (a ==b)
            {
                complow[finde2(i)] = a;
                continue;
            }
            for (int i = 17; i >= 0; i--)
            {
                if (lift[i][a]==lift[i][b]) continue;
                a = lift[i][a];
                b = lift[i][b];
            }
            a = lift[0][a];
            complow[finde2(i)] = a;
        }
    }
    for (int i = 0; i < n; i++)
    {
        if (complow[i]==1e9) continue;
        complow[i] = num[complow[i]];
    }
    num.assign(n, 0);
    for (int i =0; i < n; i++)
    {
        c = 0;
        if (!num[i]) dfs2(i);
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1116 KB Output is correct
2 Correct 3 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 712 KB Output is correct
2 Correct 65 ms 688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 112 ms 1872 KB Output is correct
2 Correct 138 ms 1652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 193 ms 3924 KB Output is correct
2 Correct 163 ms 4688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 272 ms 10828 KB Output is correct
2 Correct 237 ms 10764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 444 ms 12368 KB Output is correct
2 Correct 418 ms 12316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 565 ms 15188 KB Output is correct
2 Correct 520 ms 15148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 692 ms 15312 KB Output is correct
2 Runtime error 684 ms 65536 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 828 ms 14564 KB Output is correct
2 Runtime error 809 ms 65536 KB Memory limit exceeded
3 Halted 0 ms 0 KB -