Submission #46454

# Submission time Handle Problem Language Result Execution time Memory
46454 2018-04-20T22:09:39 Z alex99 Pipes (CEOI15_pipes) C++14
100 / 100
3842 ms 7980 KB
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <ctime>
using namespace std;
int N, M;
int TT[100005], R[100005];
long long Sum[100005];
int F[100005];
vector <int> G[100005];
bool Use[100005];
void Unite(int x, int y)
{
    if(x == y)
        return;
    if(R[x] < R[y])
    {
        TT[x] = y;
    }
    else
        TT[y] = x;
    if(R[x] == R[y])
        R[x]++;
}

int Father(int x)
{
    int init = x;
    while(x != TT[x])
        x = TT[x];
    while(init != x)
    {
        int next = TT[init];
        TT[init] = x;
        init = next;
    }
    return x;
}

void DFS(int node)
{
    Use[node] = 1;
    for(int i = 0; i < G[node].size(); i++)
    {
        int neighb = G[node][i];
        if(neighb == F[node])
            continue;
        F[neighb] = node;
        DFS(neighb);
        Sum[node] += Sum[neighb];
    }
    if(Sum[node] == 0 && F[node] != 0)
    {
        cout << node << " " << F[node] << "\n";
    }
}

void Solve()
{
    cin >> N >> M;
    for(int i = 1; i <= N; i++)
        TT[i] = i;
    for(int i = 1; i <= M; i++)
    {
        int x, y;
        cin >> x >> y;
        if(Father(x) == Father(y))
        {
            int r = rand();
            Sum[x] += r;
            Sum[y] -= r;
        }
        else
        {
            G[x].push_back(y);
            G[y].push_back(x);
            Unite(Father(x), Father(y));
        }
    }
    for(int i = 1; i <= N; i++)
        if(Use[i] == 0)
            DFS(i);
}
int main()
{
    srand(time(NULL));
    Solve();
    return 0;
}

Compilation message

pipes.cpp: In function 'void DFS(int)':
pipes.cpp:44:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0; i < G[node].size(); i++)
                    ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2660 KB Output is correct
2 Correct 9 ms 2752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 3044 KB Output is correct
2 Correct 10 ms 2944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 297 ms 2892 KB Output is correct
2 Correct 289 ms 2892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 515 ms 3176 KB Output is correct
2 Correct 609 ms 3168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 936 ms 3836 KB Output is correct
2 Correct 726 ms 4220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1150 ms 6276 KB Output is correct
2 Correct 1006 ms 6264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1876 ms 6896 KB Output is correct
2 Correct 1786 ms 6960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2464 ms 7780 KB Output is correct
2 Correct 2393 ms 7800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3242 ms 7784 KB Output is correct
2 Correct 3065 ms 7800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3842 ms 7544 KB Output is correct
2 Correct 3699 ms 7980 KB Output is correct