Submission #1081646

# Submission time Handle Problem Language Result Execution time Memory
1081646 2024-08-30T08:45:08 Z TB_ Pipes (CEOI15_pipes) C++17
40 / 100
750 ms 65536 KB
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define fo(i, n) for(ll i = 0; i<(n); i++)
#define F first 
#define S second
#define pb push_back
#define deb(x) cout << #x << " = " << (x) << endl
#define deb2(x, y) cout << #x << " = " << (x)  << ",  " << #y << " = " << (y) << endl


typedef vector<int> vl;
typedef vector<vl> vvl;


struct UnionFind{
    int n;
    vl p;
    UnionFind(int n){
        p.resize(n);
        fo(i, n) p[i] = i;
    }

    int find(int x){
        if(p[x] == x) return x;
        return p[x] = find(p[x]);
    }

    bool same(int a, int b){
        return find(a) == find(b);
    }

    void unite(int a, int b){
        a = find(a);
        b = find(b);
        if(rand()%2) swap(a, b);
        p[b] = a;
    }
};


int main(){
    cin.tie(0)->sync_with_stdio(0);

    int n, m, from, to;
    cin >> n >> m;
    vl xorv(n, 0), amount(n, 0), hash(n, 0);
    UnionFind uf(n);
    mt19937 rng(42);
    fo(i, m){
        cin >> from >> to;
        from--; to--;
        if(uf.same(from, to)){
            ll val = rng();
            hash[from]^=val;
            hash[to]^=val;
        }else{
            amount[from]++;
            amount[to]++;
            xorv[from]^=to;
            xorv[to]^=from;
            uf.unite(from, to);
        }
    }

    stack<int> st;

    fo(i, n){
        if(amount[i] == 1) st.push(i);
    }

    while(!st.empty()){
        int pos = st.top();
        st.pop();
        if(!amount[pos]) continue;
        ll edge = xorv[pos];
        // deb2(pos+1, edge+1);
        if(!hash[pos]) cout << pos+1 << " " << edge+1 << "\n";
        amount[pos]--;
        amount[edge]--;
        xorv[pos]^=edge;
        xorv[edge]^=pos;
        hash[edge]^=hash[pos];
        if(amount[edge] == 1) st.push(edge);
    }


    return 0;
}


# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 604 KB Output is correct
2 Correct 2 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 5608 KB Output is correct
2 Correct 63 ms 5716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 104 ms 2624 KB Output is correct
2 Correct 121 ms 11856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 170 ms 16784 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 234 ms 22136 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 363 ms 34752 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 503 ms 45924 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 593 ms 57184 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 750 ms 65536 KB Memory limit exceeded
2 Halted 0 ms 0 KB -