Submission #671889

# Submission time Handle Problem Language Result Execution time Memory
671889 2022-12-14T08:06:46 Z radinr85 Pipes (CEOI15_pipes) C++17
40 / 100
1020 ms 65536 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;
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:98:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   98 |     for(int i = 1 ; i <= n ; i ++)
      |     ^~~
pipes.cpp:103:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  103 |  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 4732 KB Output is correct
2 Correct 5 ms 4428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 89 ms 9904 KB Output is correct
2 Correct 82 ms 9628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 149 ms 14712 KB Output is correct
2 Correct 165 ms 15948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 242 ms 22412 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 309 ms 31052 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 531 ms 44396 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 647 ms 56840 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 797 ms 65536 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1020 ms 65536 KB Memory limit exceeded
2 Halted 0 ms 0 KB -