답안 #54849

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
54849 2018-07-05T08:13:10 Z admin Pipes (CEOI15_pipes) C++14
100 / 100
1528 ms 8696 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;
vector<int> edge[100001];
 
int par[100001];
int dep[100001];
 
int par1[100001];
int sz1[100001];
 
int par2[100001];
int sz2[100001];
int gr[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 (sz1[x] < sz1[y]) swap(x, y);
    par1[y] = x;
    sz1[x] += sz1[y];
    return 1;
}
 
int merge2(int x, int y) {
    x = find2(x);
    y = find2(y);
    if (x == y) return 0;
    gr[y] = gr[x];
    if (sz2[x] < sz2[y]) swap(x, y);
    par2[y] = x;
    sz2[x] += sz2[y];
    return 1;
}
 
void dfs(int x, int p) {
    for (int i : edge[x]) {
        if (i == p) continue;
        if (find2(i) != find2(x)) {
            par[gr[find2(i)]] = gr[find2(x)];
            dep[gr[find2(i)]] = dep[gr[find2(x)]] + 1;
        }
        dfs(i, x);
    }
}
 
int main() {
    int m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i) {
        sz1[i] = 1;
        sz2[i] = 1;
        gr[i] = i;
    }
    while (m--) {
        int u, v;
        scanf("%d%d", &u, &v);
        if (find1(u) != find1(v)) {
            if (sz1[find1(u)] < sz1[find1(v)]) swap(u, v);
            edge[u].push_back(v);
            edge[v].push_back(u);
            par[gr[find2(v)]] = gr[find2(u)];
            dep[gr[find2(v)]] = dep[gr[find2(u)]] + 1;
            dfs(v, u);
            merge1(u, v);
        }
        else if (gr[find2(u)] != gr[find2(v)]) {
            u = gr[find2(u)];
            v = gr[find2(v)];
            while (u != v) {
                if (dep[u] < dep[v]) swap(u, v);
                merge2(v, u);
                u = gr[find2(par[u])];
            }
        }
    }
    for (int i = 1; i <= n; ++i) {
        for (int j : edge[i]) {
            if (i < j) continue;
            if (gr[find2(i)] != gr[find2(j)]) printf("%d %d\n", i, j);
        }
    }
	return 0;
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:78: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:86: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 4 ms 2688 KB Output is correct
2 Correct 4 ms 2660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 2944 KB Output is correct
2 Correct 7 ms 2916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 117 ms 2944 KB Output is correct
2 Correct 115 ms 2948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 204 ms 3256 KB Output is correct
2 Correct 244 ms 3284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 333 ms 4096 KB Output is correct
2 Correct 294 ms 4460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 434 ms 6776 KB Output is correct
2 Correct 412 ms 6840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 694 ms 7440 KB Output is correct
2 Correct 710 ms 7420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1018 ms 8540 KB Output is correct
2 Correct 1044 ms 8696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1414 ms 8540 KB Output is correct
2 Correct 1194 ms 8644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1528 ms 8256 KB Output is correct
2 Correct 1414 ms 8552 KB Output is correct