Submission #669015

# Submission time Handle Problem Language Result Execution time Memory
669015 2022-12-05T11:21:33 Z radinr85 Pipes (CEOI15_pipes) C++14
0 / 100
1029 ms 35480 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 + 10;
vector<int> ver[N];
vector<pii> ans;
vector<pii> e;
bool mark[N];
int par1[N];
int par2[N];
int par[N];
int sz1[N];
int sz2[N];
int cnt[N];
int h[N];
int n , m;

int get_par(int u , bool x) {
    if(!x)
        return par1[u] == u ? u : par1[u] = get_par(par1[u] , 0);
    else
        return par2[u] == u ? u : par2[u] = get_par(par2[u] , 1);
}
bool merge(int u , int v , bool x) {
    u = get_par(u , x);
    v = get_par(v , x);

    if(u == v)
        return false;
    
    if(!x) {
        if(sz1[u] > sz1[v])
            swap(u , v);
        
        sz1[v] += sz1[u];
        par1[u] = v;
    }
    else {
        if(sz2[u] > sz2[v])
            swap(u , v);
        
        sz2[v] += sz2[u];
        par2[u] = v;
    }
    return true;
}
int DFS(int u) {
    mark[u] = true;

    for(auto v : ver[u]) {
        if(!mark[v]) {
            h[v] = h[u] + 1;
            par[v] = u;
            cnt[u] += DFS(v);
        }
        else if(h[u] - h[v] > 1) {
            cnt[u] ++;
            cnt[v] --;
        }
    }
    if(cnt[u] == 0)
        ans.pb({u , par[u]});
    
    return cnt[u];
}

int main() {
	cin.tie(0)->sync_with_stdio(0);
	
    cin >> n >> m;
    for(int i = 1 ; i <= n ; i ++) {
        sz1[i] = 1 , sz2[i] = 1;
        par1[i] = i , par2[i] = i;
    }
    for(int i = 0 ; i < m ; i ++) {
        int u , v;
        cin >> u >> v;

        if(merge(u , v , 0) || merge(u , v , 1))
            e.pb({u , v});
    }
    for(auto x : e) {
        ver[x.F].pb(x.S);
        ver[x.S].pb(x.F);
    }
    for(int i = 1 ; i <= n ; i ++)
        if(!mark[i])
            DFS(i);

    for(auto x : ans)
        if(x.S != 0)
            cout << x.F << " " << x.S << endl;

	return 0;
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:108:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  108 |     for(auto x : ans)
      |     ^~~
pipes.cpp:112:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  112 |  return 0;
      |  ^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Incorrect 2 ms 2680 KB Wrong number of edges
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3284 KB Output is correct
2 Incorrect 5 ms 3156 KB Wrong number of edges
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 96 ms 8600 KB Output is correct
2 Incorrect 91 ms 8332 KB Wrong number of edges
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 165 ms 10620 KB Wrong number of edges
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 269 ms 12092 KB Output is correct
2 Runtime error 216 ms 19572 KB Memory limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 321 ms 32632 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 531 ms 33976 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 705 ms 27544 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 858 ms 35480 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1029 ms 32660 KB Memory limit exceeded
2 Halted 0 ms 0 KB -