답안 #44880

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
44880 2018-04-08T21:08:18 Z bogdan10bos Pipes (CEOI15_pipes) C++14
30 / 100
977 ms 65536 KB
#include <bits/stdc++.h>

using namespace std;

//#define FILE_IO

typedef pair<int, int> pii;

int N, M;
int K, vis[100005], lst[100005];
vector<int> nodes, edg[100005];
vector<pii> critical;

int q[100005];

class UnionFind
{
public:
    int N;
    int f[100005];

    void init(int _N)
    {
        N = _N;
        for(int i = 1; i <= N; i++) f[i] = i;
    }

    int F(int x)
    {
        if(f[x] == x)   return x;
        return f[x] = F(f[x]);
    }

    void unite(int x, int y)
    {
        if(F(x) == F(y))    return;
        f[F(y)] = F(x);
    }
};
UnionFind f[2];

int main()
{
    #ifdef FILE_IO
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
    #endif

    scanf("%d%d", &N, &M);
    f[0].init(N), f[1].init(N);

    int cnt = 0;
    for(int i = 1; i <= M; i++)
    {
        assert(critical.size() <= N);

        int x, y;
        scanf("%d%d", &x, &y);

        if(f[1].F(x) == f[1].F(y))  continue;

        cnt++;
        if(cnt > 2 * N - 2) exit(69);

        if(f[0].F(x) != f[0].F(y))
        {
            critical.push_back({x, y});
            f[0].unite(x, y);
            int fx = f[1].F(x);
            int fy = f[1].F(y);
            edg[fx].push_back(fy);
            edg[fy].push_back(fx);
            continue;
        }

        x = f[1].F(x);
        y = f[1].F(y);

        nodes.clear();
        nodes.push_back(x);
        nodes.push_back(y);

        K += 2;
        vis[x] = K; vis[y] = K + 1;

        int st = 1, dr = 2;
        q[1] = x;
        q[2] = y;

        while(st <= dr)
        {
            int nod = q[st];
            int id = vis[nod];
            st++;

            for(auto nxt: edg[nod])
            {
                nxt = f[1].F(nxt);
                if(vis[nxt] == id)  continue;
                if(vis[nxt] == (id ^ 1))
                {
                    int nd1 = nod;
                    int nd2 = nxt;
                    if(vis[nod] != K)   swap(nd1, nd2);
                    while(nd1 != x)
                    {
                        nodes.push_back(nd1);
                        nd1 = lst[nd1];
                    }
                    while(nd2 != y)
                    {
                        nodes.push_back(nd2);
                        nd2 = lst[nd2];
                    }
                    st = dr + 1;
                    break;
                }

                vis[nxt] = vis[nod];
                lst[nxt] = nod;
                q[++dr] = nxt;
            }
        }

        for(int i = 1; i < nodes.size(); i++)
            f[1].unite(nodes[0], nodes[i]);
        for(int i = 0; i < edg[ nodes[0] ].size(); i++)
        {
            if(f[1].F( nodes[0] ) == f[1].F( edg[ nodes[0] ][i]))
            {
                edg[ nodes[0] ].erase(edg[ nodes[0] ].begin() + i);
                i--;
            }
        }

        for(int i = 1; i < nodes.size(); i++)
        {
            for(auto x: edg[ nodes[i] ])
                if(f[1].F(x) != f[1].F(nodes[0]))
                    edg[ nodes[0] ].push_back(x);
            edg[ nodes[i] ].clear();
        }
    }

    for(auto e: critical)
        if(f[1].F(e.first) != f[1].F(e.second))
            printf("%d %d\n", e.first, e.second);

    return 0;
}

Compilation message

In file included from /usr/include/c++/7/cassert:44:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:33,
                 from pipes.cpp:1:
pipes.cpp: In function 'int main()':
pipes.cpp:55:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         assert(critical.size() <= N);
                ~~~~~~~~~~~~~~~~^~~~
pipes.cpp:125:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 1; i < nodes.size(); i++)
                        ~~^~~~~~~~~~~~~~
pipes.cpp:127:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0; i < edg[ nodes[0] ].size(); i++)
                        ~~^~~~~~~~~~~~~~~~~~~~~~~~
pipes.cpp:136:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 1; i < nodes.size(); i++)
                        ~~^~~~~~~~~~~~~~
pipes.cpp:49:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &N, &M);
     ~~~~~^~~~~~~~~~~~~~~~
pipes.cpp:58:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &x, &y);
         ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 7040 KB Output is correct
2 Correct 11 ms 3456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 125 ms 5760 KB Output is correct
2 Correct 117 ms 3280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 261 ms 21768 KB Memory limit exceeded (if you are sure your verdict is not MLE, please contact us)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 341 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 787 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 834 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 977 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 960 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 954 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -