Submission #208229

# Submission time Handle Problem Language Result Execution time Memory
208229 2020-03-10T10:56:10 Z alexandra_udristoiu Pipes (CEOI15_pipes) C++14
90 / 100
5000 ms 7948 KB
#include<iostream>
#include<vector>
#define DIM 100005
using namespace std;
int n, m, i, x, y, r1, r2, nr;
int r[DIM], ok[DIM], t[DIM], nxt[DIM], niv[DIM];
vector<int> v[DIM];
int rad(int x){
    while(r[x] > 0){
        x = r[x];
    }
    return x;
}
int urm(int x){
    int y, aux;
    y = nxt[x];
    while(ok[y] == 1){
        y = nxt[y];
    }
    while(x != y){
        aux = nxt[x];
        nxt[x] = y;
        x = aux;
    }
    return y;
}
void dfs(int nod, int tt){
    niv[nod] = 1 + niv[tt];
    for(int i = 0; i < v[nod].size(); i++){
        if(v[nod][i] != tt){
            dfs(v[nod][i], nod);
        }
    }
    if(t[nod] != tt){
        ok[ t[nod] ] = ok[nod];
        t[nod] = tt;
        nxt[nod] = t[nod];
    }
}
int main(){
    cin>> n >> m;
    for(i = 1; i <= n; i++){
        r[i] = -1;
    }
    for(; m; m--){
        cin>> x >> y;
        r1 = rad(x);
        r2 = rad(y);
        if(r1 != r2){
           /*nr++;
            if(nr == n){
                break;
            }*/
            v[x].push_back(y);
            v[y].push_back(x);
            if(r[r1] < r[r2]){
                r[r1] += r[r2];
                r[r2] = r1;
                dfs(y, x);
                ok[y] = 0;
            }
            else{
                r[r2] += r[r1];
                r[r1] = r2;
                dfs(x, y);
                ok[x] = 0;
            }
        }
        else{
             while(x != y){
                if(niv[x] > niv[y]){
                    ok[x] = 1;
                    x = urm(x);
                }
                else{
                    ok[y] = 1;
                    y = urm(y);
                }
            }
        }
    }
    for(i = 1; i <= n; i++){
        if(ok[i] == 0 && t[i] != 0){
            cout<< i <<" "<< t[i] <<"\n";
        }
    }
}

Compilation message

pipes.cpp: In function 'void dfs(int, int)':
pipes.cpp:29:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < v[nod].size(); i++){
                    ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2680 KB Output is correct
2 Correct 6 ms 2680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 3068 KB Output is correct
2 Correct 17 ms 3064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 456 ms 3192 KB Output is correct
2 Correct 440 ms 3192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 783 ms 3576 KB Output is correct
2 Correct 922 ms 3704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1262 ms 4216 KB Output is correct
2 Correct 1075 ms 4624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1628 ms 6476 KB Output is correct
2 Correct 1436 ms 6392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2582 ms 6776 KB Output is correct
2 Correct 2606 ms 7048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3449 ms 7928 KB Output is correct
2 Correct 3244 ms 7948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4364 ms 7804 KB Output is correct
2 Correct 4172 ms 7932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5067 ms 7672 KB Time limit exceeded
2 Halted 0 ms 0 KB -