Submission #671890

# Submission time Handle Problem Language Result Execution time Memory
671890 2022-12-14T08:07:31 Z radinr85 Pipes (CEOI15_pipes) C++17
40 / 100
925 ms 61804 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;

const int INF = INT_MAX;
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];
int par[N][2];
int sz[N][2];
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 U = get_par(u , 0);
    int V = get_par(v , 0);

    if(U == V) {
        U = get_par(u , 1);
        V = get_par(v , 1);

        if(U != V) {
            if(sz[U][1] > sz[V][1])
                swap(U , V);
            
            par[U][1] = V;
            sz[V][1] += sz[U][1];

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

        ver[u].pb(v);
        ver[v].pb(u);
    }
}
int DFS(int u) {
    sz[u][0] = 1;

    int cnt = 0;
    for(auto v : ver[u]) {
        if(!sz[v][0]) {
            par[v][1] = par[u][1] + 1;
            par[v][0] = u;
            sz[u][1] += DFS(v);
        }
        else if(v == par[u][0])
            cnt ++;
        else if(par[u][1] > par[v][1])
            sz[u][1] ++ , sz[v][1] --;
    }
    if(par[u][0] != 0 && cnt < 2 && sz[u][1] == 0)
        cout << u << " " << par[u][0] << endl;
    
    return sz[u][1];
}

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

    cin >> n >> m;
    for(int i = 1 ; i <= n ; i ++)
        par[i][0] = i , par[i][1] = i , sz[i][0] = 1 , sz[i][1] = 1;
    
    for(int i = 0 ; i < m ; i ++) {
        int u , v;
        cin >> u >> v;

        merge(u , v);
    }
    ms(sz , 0);
    ms(par , 0);

    for(int i = 1 ; i <= n ; i ++) {
        if(!sz[i][0])
            DFS(i);
    }

    
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4180 KB Output is correct
2 Correct 2 ms 4180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4564 KB Output is correct
2 Correct 4 ms 4436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 4540 KB Output is correct
2 Correct 79 ms 4264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 133 ms 5068 KB Output is correct
2 Correct 156 ms 4604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 235 ms 6296 KB Output is correct
2 Runtime error 203 ms 19824 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 302 ms 10416 KB Output is correct
2 Runtime error 292 ms 25672 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 460 ms 11256 KB Output is correct
2 Runtime error 476 ms 42216 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 616 ms 12940 KB Output is correct
2 Runtime error 621 ms 50340 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 813 ms 15820 KB Output is correct
2 Runtime error 768 ms 61804 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 925 ms 28484 KB Memory limit exceeded
2 Halted 0 ms 0 KB -