답안 #54809

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
54809 2018-07-05T07:02:00 Z 강태규(#1507) Pipes (CEOI15_pipes) C++11
50 / 100
5000 ms 5224 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_map>
#include <functional>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>

using namespace std;
typedef long long llong;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<llong, llong> pll;

int n;
struct link {
    int x;
    link * nxt;
} db[200000];
int tp = 0;

link * edge[100001];

int par[100001];
int dep[100001];

int par1[100001];
int sz[100001];

int par2[100001];

int find1(int x) {
    if (par1[x]) return par1[x] = find1(par1[x]);
    return x;
}

int find2(int x) {
    if (par2[x]) return par2[x] = find2(par2[x]);
    return x;
}

int merge1(int x, int y) {
    x = find1(x);
    y = find1(y);
    if (x == y) return 0;
    if (sz[x] < sz[y]) swap(x, y);
    par1[y] = x;
    sz[x] += sz[y];
    return 1;
}

int merge2(int x, int y) {
    x = find2(x);
    y = find2(y);
    if (x == y) return 0;
    par2[y] = x;
    return 1;
}

void dfs(int x, int p) {
    for (link * i = edge[x]; i; i = i->nxt) {
        if (i->x == p) continue;
        if (find2(i->x) != find2(x)) {
            par[find2(i->x)] = find2(x);
            dep[find2(i->x)] = dep[find2(x)] + 1;
        }
        dfs(i->x, x);
    }
}

void addEdge(int x, int t) {
    link * ls = db + tp++;
    ls->x = t;
    ls->nxt = edge[x];
    edge[x] = ls;
}

int main() {
    int m;
    scanf("%d%d", &n, &m);
    while (m--) {
        int u, v;
        scanf("%d%d", &u, &v);
        if (find1(u) != find1(v)) {
            if (sz[find1(u)] < sz[find1(v)]) swap(u, v);
            addEdge(u, v);
            addEdge(v, u);
            par[find2(v)] = find2(u);
            dep[find2(v)] = dep[find2(u)] + 1;
            dfs(v, u);
            merge1(u, v);
        }
        else if (find2(u) != find2(v)) {
            u = find2(u);
            v = find2(v);
            while (u != v) {
                if (dep[u] < dep[v]) swap(u, v);
                merge2(v, u);
                u = find2(par[u]);
            }
        }
    }
    for (int i = 1; i <= n; ++i) {
        for (link * j = edge[i]; j; j = j->nxt) {
            if (i < j->x) continue;
            if (find2(i) != find2(j->x)) printf("%d %d\n", i, j->x);
        }
    }
	return 0;
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:86: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:89:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &u, &v);
         ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 300 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 640 KB Output is correct
2 Correct 30 ms 648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 153 ms 512 KB Output is correct
2 Correct 123 ms 592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 657 ms 908 KB Output is correct
2 Correct 305 ms 888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3840 ms 1896 KB Output is correct
2 Correct 2075 ms 2124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5098 ms 3936 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5017 ms 4484 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5002 ms 5224 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5099 ms 5124 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5030 ms 4904 KB Time limit exceeded
2 Halted 0 ms 0 KB -