Submission #540695

# Submission time Handle Problem Language Result Execution time Memory
540695 2022-03-21T12:35:44 Z Vladth11 Pipes (CEOI15_pipes) C++14
100 / 100
1058 ms 14284 KB
#include <iostream>
#include <map>
#include <vector>

using namespace std;

const int NMAX = 100001;

vector <int> v[NMAX];
int root;
int lvl[NMAX];
int parent[NMAX];
int parentB[NMAX];

int findR(int a){
    if(parent[a] == a)
        return a;
    return parent[a] = findR(parent[a]);
}

int findRB(int a){
    if(parentB[a] == a)
        return a;
    return parentB[a] = findRB(parentB[a]);
}

void unite(int a, int b){
    a = findR(a);
    b = findR(b);
    parent[a] = b;
}

void uniteB(int a, int b){
    a = findRB(a);
    b = findRB(b);
    parentB[a] = b;
}

map <int, int> dp;

void DFS(int node, int p){
    lvl[node] = lvl[p] + 1;
    for(auto x : v[node]){
        if(lvl[x] == 0){
            DFS(x, node);
        }else if(lvl[x] > lvl[node]){
            dp[node]--;
        }else{
            dp[node]++;
        }
    }
    if(node != root){
        dp[node]--;
    }
    dp[p] += dp[node];
    if(dp[node] == 0 && node != root){
        cout << node << " " << p << "\n";
    }
  dp.erase(node);

}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m, i;
    cin >> n >> m;
    for(i = 1; i <= n; i++){
        parent[i] = i;
        parentB[i] = i;
    }
    for(i = 1; i <= m; i++){
        int a, b;
        cin >> a >> b;
        if(findR(a) != findR(b)){
            v[a].push_back(b);
            v[b].push_back(a);
            unite(a, b);
        }else if(findRB(a) != findRB(b)){
            v[a].push_back(b);
            v[b].push_back(a);
            uniteB(a, b);
        }
    }
    for(i = 1; i <= n; i++){
        if(!lvl[i]){
            root = i;
            DFS(root, 0);
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 3196 KB Output is correct
2 Correct 7 ms 2900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 85 ms 2960 KB Output is correct
2 Correct 84 ms 2820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 160 ms 3760 KB Output is correct
2 Correct 174 ms 3244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 268 ms 5336 KB Output is correct
2 Correct 229 ms 4876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 387 ms 10748 KB Output is correct
2 Correct 327 ms 6860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 586 ms 11912 KB Output is correct
2 Correct 530 ms 8800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 768 ms 14284 KB Output is correct
2 Correct 680 ms 8816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 894 ms 14132 KB Output is correct
2 Correct 832 ms 8804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1058 ms 13536 KB Output is correct
2 Correct 971 ms 10480 KB Output is correct