답안 #303320

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
303320 2020-09-20T08:00:41 Z Kevin_Zhang_TW 슈퍼트리 잇기 (IOI20_supertrees) C++17
100 / 100
268 ms 26360 KB
#include "supertrees.h"
#include <vector>

#include<bits/stdc++.h>
#define pb emplace_back
using namespace std;
using ll = long long;
#ifdef KEV
#define DE(i, e) cerr << #i << ' ' << i << e
void debug(auto L, auto R) { while (L < R) cerr << *L << " \n"[L+1==R], ++L; }
#else
#define DE(...) 0
void debug(...) {}
#endif
const int maxn = 300010;
int n, cnt[10];
std::vector<std::vector<int>> p;
struct dsu {
    int g[maxn], sz[maxn];
    int find(int i) { return g[i] == i ? g[i] : g[i] = find(g[i]); }
    void init() {
        for (int i = 0;i < n;++i)
            g[i] = i, sz[i] = 1;
    }
    bool merge(int a, int b) {
        a = find(a), b = find(b);
        if (a == b) return false;
        if ( sz[a] < sz[b]) swap(a, b);
        sz[a] += sz[b];
        return g[b] = a, true;
    }
    int operator()(int i) { return find(i); }
}d1, d2;
bool C1() {
    d1.init();
    for (int i = 0;i < n;++i)
        for (int j = i+1;j < n;++j)
            if (p[i][j])
                d1.merge(i, j);
    for (int i = 0;i < n;++i)
        for (int j = i+1;j < n;++j)
            if (!p[i][j] && d1(i) == d1(j))
                return false;
    return true;
}
void M1() {
    d1.init();
    for (int i = 0;i < n;++i)
        for (int j = i+1;j < n;++j)
            if (p[i][j] == 1)
                d1.merge(i, j);
}
void M2() {
    d2.init();
    for (int i = 0;i < n;++i)
        for (int j = i+1;j < n;++j)
            if (p[i][j] == 2) 
                d2.merge(i, j);
}
bool build_ok(vector<vector<int>> &edge) {
    if (!C1()) return false;
    M1(), M2();
    vector<vector<int>> c1(n), c2(n);
    for (int i = 0;i < n;++i) 
        c1[d1(i)].pb(i), c2[d2(i)].pb(i);
    // connect same chain
    for (int i = 0;i < n;++i) {
        if (c1[i].size() > 1) {
            int lst = -1;
            for (int u : c1[i]) {
                if (lst != -1) 
                    edge[lst][u] = edge[u][lst] = true;
                lst = u;
            }
        }
    }
    // check valid
    for (int i = 0;i < n;++i)
        for (int j = i+1;j < n;++j) {
            if (p[i][j] != 1 && d1(i) == d1(j))
                return false;
            if (d2(i) == d2(j))
                if (d1(i) != d1(j) && p[i][j] != 2)
                    return false;
        }

    for (int i = 0;i < n;++i) {
        auto &C = c2[i];
        for (int &u : C) u = d1(u);
        sort(C.begin(), C.end());
        C.resize(unique(C.begin(), C.end()) - C.begin());
        if (c2[i].size() == 2) return false;
        if (c2[i].size() > 2) {
            int lst = c2[i].back();
            for (int u : c2[i]) {
                edge[u][lst] = edge[lst][u] = true;
                lst = u;
            }
        }
    }
    return true;
}
int construct(std::vector<std::vector<int>> p) {
	n = p.size();
    ::p = p;
	std::vector<std::vector<int>> answer;
	for (int i = 0; i < n; i++) {
		std::vector<int> row(n);
		answer.push_back(row);
        for (int j = i+1;j < n;++j)
            ++cnt[ p[i][j] ];
	}
    if (cnt[3] || !build_ok(answer))
        return 0;
	build(answer);
	return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:113:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
  113 |     if (cnt[3] || !build_ok(answer))
      |     ^~
supertrees.cpp:115:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
  115 |  build(answer);
      |  ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 11 ms 1408 KB Output is correct
7 Correct 251 ms 26104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 11 ms 1408 KB Output is correct
7 Correct 251 ms 26104 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 11 ms 1408 KB Output is correct
13 Correct 244 ms 26104 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 5 ms 1024 KB Output is correct
17 Correct 112 ms 16092 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 73 ms 6904 KB Output is correct
21 Correct 252 ms 26104 KB Output is correct
22 Correct 250 ms 26160 KB Output is correct
23 Correct 264 ms 26104 KB Output is correct
24 Correct 240 ms 26104 KB Output is correct
25 Correct 101 ms 16120 KB Output is correct
26 Correct 105 ms 16100 KB Output is correct
27 Correct 254 ms 26104 KB Output is correct
28 Correct 246 ms 26104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 10 ms 1408 KB Output is correct
9 Correct 244 ms 26060 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 10 ms 1408 KB Output is correct
13 Correct 244 ms 26104 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 6 ms 1024 KB Output is correct
17 Correct 111 ms 16120 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 63 ms 6952 KB Output is correct
22 Correct 245 ms 26104 KB Output is correct
23 Correct 246 ms 26232 KB Output is correct
24 Correct 263 ms 26104 KB Output is correct
25 Correct 105 ms 16220 KB Output is correct
26 Correct 102 ms 16120 KB Output is correct
27 Correct 248 ms 26104 KB Output is correct
28 Correct 253 ms 26060 KB Output is correct
29 Correct 107 ms 16220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 63 ms 6904 KB Output is correct
5 Correct 249 ms 26076 KB Output is correct
6 Correct 240 ms 26104 KB Output is correct
7 Correct 268 ms 26232 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 64 ms 6904 KB Output is correct
10 Correct 249 ms 26104 KB Output is correct
11 Correct 260 ms 26104 KB Output is correct
12 Correct 257 ms 26104 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 62 ms 6904 KB Output is correct
17 Correct 243 ms 26108 KB Output is correct
18 Correct 252 ms 26104 KB Output is correct
19 Correct 251 ms 26360 KB Output is correct
20 Correct 238 ms 26104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 11 ms 1408 KB Output is correct
7 Correct 251 ms 26104 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 11 ms 1408 KB Output is correct
13 Correct 244 ms 26104 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 5 ms 1024 KB Output is correct
17 Correct 112 ms 16092 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 73 ms 6904 KB Output is correct
21 Correct 252 ms 26104 KB Output is correct
22 Correct 250 ms 26160 KB Output is correct
23 Correct 264 ms 26104 KB Output is correct
24 Correct 240 ms 26104 KB Output is correct
25 Correct 101 ms 16120 KB Output is correct
26 Correct 105 ms 16100 KB Output is correct
27 Correct 254 ms 26104 KB Output is correct
28 Correct 246 ms 26104 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 0 ms 384 KB Output is correct
31 Correct 0 ms 384 KB Output is correct
32 Correct 1 ms 384 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 0 ms 384 KB Output is correct
35 Correct 1 ms 384 KB Output is correct
36 Correct 10 ms 1408 KB Output is correct
37 Correct 244 ms 26060 KB Output is correct
38 Correct 0 ms 384 KB Output is correct
39 Correct 1 ms 384 KB Output is correct
40 Correct 10 ms 1408 KB Output is correct
41 Correct 244 ms 26104 KB Output is correct
42 Correct 0 ms 384 KB Output is correct
43 Correct 0 ms 384 KB Output is correct
44 Correct 6 ms 1024 KB Output is correct
45 Correct 111 ms 16120 KB Output is correct
46 Correct 0 ms 384 KB Output is correct
47 Correct 0 ms 384 KB Output is correct
48 Correct 1 ms 384 KB Output is correct
49 Correct 63 ms 6952 KB Output is correct
50 Correct 245 ms 26104 KB Output is correct
51 Correct 246 ms 26232 KB Output is correct
52 Correct 263 ms 26104 KB Output is correct
53 Correct 105 ms 16220 KB Output is correct
54 Correct 102 ms 16120 KB Output is correct
55 Correct 248 ms 26104 KB Output is correct
56 Correct 253 ms 26060 KB Output is correct
57 Correct 107 ms 16220 KB Output is correct
58 Correct 1 ms 384 KB Output is correct
59 Correct 0 ms 384 KB Output is correct
60 Correct 5 ms 1024 KB Output is correct
61 Correct 107 ms 16120 KB Output is correct
62 Correct 1 ms 384 KB Output is correct
63 Correct 1 ms 384 KB Output is correct
64 Correct 1 ms 384 KB Output is correct
65 Correct 64 ms 6904 KB Output is correct
66 Correct 101 ms 16120 KB Output is correct
67 Correct 101 ms 16124 KB Output is correct
68 Correct 101 ms 16120 KB Output is correct
69 Correct 109 ms 16220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 11 ms 1408 KB Output is correct
7 Correct 251 ms 26104 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 11 ms 1408 KB Output is correct
13 Correct 244 ms 26104 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 5 ms 1024 KB Output is correct
17 Correct 112 ms 16092 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 73 ms 6904 KB Output is correct
21 Correct 252 ms 26104 KB Output is correct
22 Correct 250 ms 26160 KB Output is correct
23 Correct 264 ms 26104 KB Output is correct
24 Correct 240 ms 26104 KB Output is correct
25 Correct 101 ms 16120 KB Output is correct
26 Correct 105 ms 16100 KB Output is correct
27 Correct 254 ms 26104 KB Output is correct
28 Correct 246 ms 26104 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 0 ms 384 KB Output is correct
31 Correct 0 ms 384 KB Output is correct
32 Correct 1 ms 384 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 0 ms 384 KB Output is correct
35 Correct 1 ms 384 KB Output is correct
36 Correct 10 ms 1408 KB Output is correct
37 Correct 244 ms 26060 KB Output is correct
38 Correct 0 ms 384 KB Output is correct
39 Correct 1 ms 384 KB Output is correct
40 Correct 10 ms 1408 KB Output is correct
41 Correct 244 ms 26104 KB Output is correct
42 Correct 0 ms 384 KB Output is correct
43 Correct 0 ms 384 KB Output is correct
44 Correct 6 ms 1024 KB Output is correct
45 Correct 111 ms 16120 KB Output is correct
46 Correct 0 ms 384 KB Output is correct
47 Correct 0 ms 384 KB Output is correct
48 Correct 1 ms 384 KB Output is correct
49 Correct 63 ms 6952 KB Output is correct
50 Correct 245 ms 26104 KB Output is correct
51 Correct 246 ms 26232 KB Output is correct
52 Correct 263 ms 26104 KB Output is correct
53 Correct 105 ms 16220 KB Output is correct
54 Correct 102 ms 16120 KB Output is correct
55 Correct 248 ms 26104 KB Output is correct
56 Correct 253 ms 26060 KB Output is correct
57 Correct 107 ms 16220 KB Output is correct
58 Correct 1 ms 384 KB Output is correct
59 Correct 1 ms 384 KB Output is correct
60 Correct 0 ms 384 KB Output is correct
61 Correct 63 ms 6904 KB Output is correct
62 Correct 249 ms 26076 KB Output is correct
63 Correct 240 ms 26104 KB Output is correct
64 Correct 268 ms 26232 KB Output is correct
65 Correct 0 ms 384 KB Output is correct
66 Correct 64 ms 6904 KB Output is correct
67 Correct 249 ms 26104 KB Output is correct
68 Correct 260 ms 26104 KB Output is correct
69 Correct 257 ms 26104 KB Output is correct
70 Correct 1 ms 384 KB Output is correct
71 Correct 0 ms 384 KB Output is correct
72 Correct 1 ms 384 KB Output is correct
73 Correct 62 ms 6904 KB Output is correct
74 Correct 243 ms 26108 KB Output is correct
75 Correct 252 ms 26104 KB Output is correct
76 Correct 251 ms 26360 KB Output is correct
77 Correct 238 ms 26104 KB Output is correct
78 Correct 1 ms 384 KB Output is correct
79 Correct 0 ms 384 KB Output is correct
80 Correct 5 ms 1024 KB Output is correct
81 Correct 107 ms 16120 KB Output is correct
82 Correct 1 ms 384 KB Output is correct
83 Correct 1 ms 384 KB Output is correct
84 Correct 1 ms 384 KB Output is correct
85 Correct 64 ms 6904 KB Output is correct
86 Correct 101 ms 16120 KB Output is correct
87 Correct 101 ms 16124 KB Output is correct
88 Correct 101 ms 16120 KB Output is correct
89 Correct 109 ms 16220 KB Output is correct
90 Correct 0 ms 256 KB Output is correct
91 Correct 0 ms 256 KB Output is correct
92 Correct 5 ms 1024 KB Output is correct
93 Correct 109 ms 16092 KB Output is correct
94 Correct 1 ms 256 KB Output is correct
95 Correct 1 ms 256 KB Output is correct
96 Correct 1 ms 256 KB Output is correct
97 Correct 26 ms 4308 KB Output is correct
98 Correct 100 ms 16120 KB Output is correct
99 Correct 99 ms 16120 KB Output is correct
100 Correct 101 ms 16120 KB Output is correct
101 Correct 98 ms 16120 KB Output is correct
102 Correct 102 ms 16120 KB Output is correct
103 Correct 101 ms 16120 KB Output is correct
104 Correct 103 ms 16096 KB Output is correct
105 Correct 110 ms 16120 KB Output is correct