Submission #669595

# Submission time Handle Problem Language Result Execution time Memory
669595 2022-12-06T19:55:48 Z radinr85 Pipes (CEOI15_pipes) C++17
50 / 100
1228 ms 29148 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 cnt[N];
int n , m;

int get_par(int u , int x) {
    return 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) {
            ver[u].pb(v);
            ver[v].pb(u);
            par[u][1] = v;
        }
    }
    else {
        ver[u].pb(v);
        ver[v].pb(u);
        par[u][0] = v;
    }
}
int 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;

            cnt[u] += DFS(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;

    return cnt[u];
}

int main() {
	cin.tie(0)->sync_with_stdio(0);
	
    for(int i = 0 ; i < N ; i ++)
        par[i][0] = par[i][1] = i;
    
    cin >> n >> m;
    for(int i = 0 ; i < m ; i ++) {
        int u , v;
        cin >> u >> v;

        merge(u , v);
    }
    for(int i = 1 ; i <= n ; i ++) {
        if(!mark[i]) {
            par[i][0] = 0;
            par[i][1] = 0;

            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 5 ms 3924 KB Output is correct
2 Correct 5 ms 3668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 4108 KB Output is correct
2 Correct 87 ms 3896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 162 ms 5680 KB Output is correct
2 Correct 198 ms 4876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 278 ms 9072 KB Output is correct
2 Correct 230 ms 7224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 518 ms 23500 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 762 ms 24032 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 907 ms 29148 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1104 ms 29120 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1228 ms 28496 KB Memory limit exceeded
2 Halted 0 ms 0 KB -