Submission #671895

# Submission time Handle Problem Language Result Execution time Memory
671895 2022-12-14T08:25:27 Z radinr85 Pipes (CEOI15_pipes) C++14
100 / 100
1195 ms 13392 KB
//radinr85
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef deque<int> dq;
typedef long double ld;
typedef pair<int , int> pii;
typedef priority_queue<int> pq;
 
const int maxn = 3e6;
const ll mod = 1e9+7;
 
#define F first
#define S second
#define endl "\n"
#define pb push_back
#define ms(x , y) memset(x , y , sizeof x)
ll pw(ll a, ll b, ll md = mod){ll res = 1;while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);}
 
const int N = 1e5 + 1;
vector<int> ver[N];
bitset<N> mark;
int par[N][2];
int sz[N][2];
int cnt[N];
int n , m;
 
int get_par(int u , int x) {
    while(par[u][x] != u) {
        u = par[u][x];
    }
    return u;
    // par[u][x] == u ? u : par[u][x] = get_par(par[u][x] , x);
}
void merge(int u , int v) {
    int u1 = get_par(u , 0);
    int u2 = get_par(v , 0);
 
    if(u1 == u2) {
        u1 = get_par(u , 1);
        u2 = get_par(v , 1);
 
        if(u1 != u2) {
            if(sz[u1][1] > sz[u2][1])
                swap(u1 , u2);
            par[u1][1] = u2;
            sz[u2][1] += sz[u1][1];

            ver[u].pb(v);
            ver[v].pb(u);
        }
    }
    else {
        if(sz[u1][0] > sz[u2][0])
            swap(u1 , u2);
        par[u1][0] = u2;
        sz[u2][0] += sz[u1][0];

        ver[u].pb(v);
        ver[v].pb(u);
    }
}
void DFS(int u) {
    mark[u] = 1;
 
    int multi = 0;
    for(auto v : ver[u]) {
        if(!mark[v]) {
            par[v][0] = u;
            par[v][1] = par[u][1] + 1;
 
            DFS(v);
            cnt[u] += cnt[v];
        }
        else if(par[u][0] == v)
            multi ++;
        else if(par[v][1] < par[u][1]) {
            cnt[u] ++;
            cnt[v] --; 
        }
    }
    if(multi == 1 && !cnt[u])
        cout << u << " " << par[u][0] << endl;
}
 
int main() {
	cin.tie(0)->sync_with_stdio(0);
    
    cin >> n >> m;
    for(int i = 1 ; i <= n ; i ++)
        par[i][0] = par[i][1] = i , sz[i][0] = sz[i][1] = 1;
    
    for(int i = 0 ; i < m ; i ++) {
        int u , v;
        cin >> u >> v;
 
        merge(u , v);
    }
    ms(par , 0);
    for(int i = 1 ; i <= n ; i ++) {
        if(!mark[i]) {
            DFS(i);
        }
    }
 
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 2 ms 3412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 3924 KB Output is correct
2 Correct 5 ms 3616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 3760 KB Output is correct
2 Correct 96 ms 3532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 167 ms 4360 KB Output is correct
2 Correct 210 ms 3960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 276 ms 5796 KB Output is correct
2 Correct 269 ms 5452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 376 ms 10408 KB Output is correct
2 Correct 330 ms 7464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 666 ms 11524 KB Output is correct
2 Correct 571 ms 9000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 789 ms 13392 KB Output is correct
2 Correct 737 ms 9264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 971 ms 13328 KB Output is correct
2 Correct 924 ms 9364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1195 ms 12796 KB Output is correct
2 Correct 1120 ms 10384 KB Output is correct