답안 #335559

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
335559 2020-12-13T04:51:16 Z aanastasov 슈퍼트리 잇기 (IOI20_supertrees) C++17
100 / 100
290 ms 22252 KB
#include "supertrees.h"
#include <vector>
#include <queue>
#include <iostream>

int construct(std::vector<std::vector<int>> p) {
    const int nodes = p.size();
    auto hasEdge = std::vector<std::vector<int>>(nodes, std::vector<int>(nodes, 0));
    auto marked = std::vector<bool>(nodes, false);
    for (int start = 0; start < nodes; ++start) {
        if (marked[start]) continue;
        // Step 1: Find all the nodes in the connected component that contains <start>.
        auto component = std::vector<int>();
        auto q = std::queue<int>();
        marked[start] = true;
        q.push(start);
        marked[start] = true;
        while (!q.empty()) {
            int src = q.front();
            q.pop();
            component.push_back(src);
            for (int dst = 0; dst < nodes; ++dst)
                if (p[src][dst] > 0 && !marked[dst]) {
                    marked[dst] = true;
                    q.push(dst);
                }
        }
        // Step 2: Check that all nodes in the component are connected to each other,
        // and that all pairs of nodes are connected by either one or two simple paths.
        for (int src : component)
            for (int dst : component) {
                if (src != dst && p[src][dst] == 0) return 0;
                if (p[src][dst] >= 3) return 0;
            }
        // Step 3: Divide all nodes into disjoint sets by going over only edges s.t. p[src][dst] = 1.
        auto disjointSets = std::vector<std::vector<int>>();
        auto visited = std::vector<bool>(nodes, false);
        auto groupIndex = std::vector<int>(nodes, -1);
        auto order = std::queue<int>();
        for (int node : component) if (!visited[node]) {
            visited[node] = true;
            order.push(node);
            auto group = std::vector<int>();
            while (!order.empty()) {
                int src = order.front();
                order.pop();
                groupIndex[src] = disjointSets.size();
                group.push_back(src);
                for (int dst : component) if (!visited[dst] && p[src][dst] == 1) {
                    visited[dst] = true;
                    order.push(dst);
                }
            }

            for (int src : group) {
                for (int dst : group)
                    if (src != dst && p[src][dst] != 1) return 0;
                for (int dst : component) 
                    if (src != dst && groupIndex[dst] != disjointSets.size() && p[src][dst] != 2) return 0;
            }
            disjointSets.push_back(group);
        }
        if (disjointSets.size() == 2) return 0;
        // Step 4: Connect the resulting 1-tree.
        for (int index = 0; index < disjointSets.size(); ++index) {
            int curRepr = disjointSets[index][0];
            int nextRepr = disjointSets[(index + 1) % disjointSets.size()][0];
            if (disjointSets.size() > 1)
                hasEdge[curRepr][nextRepr] = hasEdge[nextRepr][curRepr] = 1;
            for (int j = 1; j < disjointSets[index].size(); ++j) {
                int child = disjointSets[index][j];
                hasEdge[curRepr][child] = hasEdge[child][curRepr] = 1;
            }
        }
    }
    build(hasEdge);
    return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:59:55: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |                     if (src != dst && groupIndex[dst] != disjointSets.size() && p[src][dst] != 2) return 0;
supertrees.cpp:65:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |         for (int index = 0; index < disjointSets.size(); ++index) {
      |                             ~~~~~~^~~~~~~~~~~~~~~~~~~~~
supertrees.cpp:70:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |             for (int j = 1; j < disjointSets[index].size(); ++j) {
      |                             ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 11 ms 1388 KB Output is correct
7 Correct 255 ms 22124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 11 ms 1388 KB Output is correct
7 Correct 255 ms 22124 KB Output is correct
8 Correct 1 ms 376 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 0 ms 364 KB Output is correct
12 Correct 11 ms 1260 KB Output is correct
13 Correct 279 ms 22124 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 5 ms 748 KB Output is correct
17 Correct 121 ms 12128 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 66 ms 5868 KB Output is correct
21 Correct 256 ms 22112 KB Output is correct
22 Correct 257 ms 22124 KB Output is correct
23 Correct 269 ms 22124 KB Output is correct
24 Correct 260 ms 22124 KB Output is correct
25 Correct 109 ms 12232 KB Output is correct
26 Correct 103 ms 12140 KB Output is correct
27 Correct 258 ms 22124 KB Output is correct
28 Correct 290 ms 22124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 11 ms 1260 KB Output is correct
9 Correct 264 ms 22124 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 11 ms 1260 KB Output is correct
13 Correct 264 ms 22252 KB Output is correct
14 Correct 0 ms 364 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 5 ms 748 KB Output is correct
17 Correct 112 ms 12140 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 0 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 78 ms 5868 KB Output is correct
22 Correct 267 ms 22252 KB Output is correct
23 Correct 277 ms 22252 KB Output is correct
24 Correct 264 ms 22252 KB Output is correct
25 Correct 131 ms 12140 KB Output is correct
26 Correct 107 ms 12140 KB Output is correct
27 Correct 257 ms 22124 KB Output is correct
28 Correct 262 ms 22252 KB Output is correct
29 Correct 104 ms 12140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 67 ms 5868 KB Output is correct
5 Correct 255 ms 22124 KB Output is correct
6 Correct 252 ms 22124 KB Output is correct
7 Correct 259 ms 22124 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 65 ms 5868 KB Output is correct
10 Correct 254 ms 22124 KB Output is correct
11 Correct 252 ms 22124 KB Output is correct
12 Correct 260 ms 22124 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 65 ms 5868 KB Output is correct
17 Correct 258 ms 22124 KB Output is correct
18 Correct 258 ms 22124 KB Output is correct
19 Correct 259 ms 22124 KB Output is correct
20 Correct 255 ms 22124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 11 ms 1388 KB Output is correct
7 Correct 255 ms 22124 KB Output is correct
8 Correct 1 ms 376 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 0 ms 364 KB Output is correct
12 Correct 11 ms 1260 KB Output is correct
13 Correct 279 ms 22124 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 5 ms 748 KB Output is correct
17 Correct 121 ms 12128 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 66 ms 5868 KB Output is correct
21 Correct 256 ms 22112 KB Output is correct
22 Correct 257 ms 22124 KB Output is correct
23 Correct 269 ms 22124 KB Output is correct
24 Correct 260 ms 22124 KB Output is correct
25 Correct 109 ms 12232 KB Output is correct
26 Correct 103 ms 12140 KB Output is correct
27 Correct 258 ms 22124 KB Output is correct
28 Correct 290 ms 22124 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 0 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 11 ms 1260 KB Output is correct
37 Correct 264 ms 22124 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 11 ms 1260 KB Output is correct
41 Correct 264 ms 22252 KB Output is correct
42 Correct 0 ms 364 KB Output is correct
43 Correct 0 ms 364 KB Output is correct
44 Correct 5 ms 748 KB Output is correct
45 Correct 112 ms 12140 KB Output is correct
46 Correct 1 ms 364 KB Output is correct
47 Correct 0 ms 364 KB Output is correct
48 Correct 1 ms 364 KB Output is correct
49 Correct 78 ms 5868 KB Output is correct
50 Correct 267 ms 22252 KB Output is correct
51 Correct 277 ms 22252 KB Output is correct
52 Correct 264 ms 22252 KB Output is correct
53 Correct 131 ms 12140 KB Output is correct
54 Correct 107 ms 12140 KB Output is correct
55 Correct 257 ms 22124 KB Output is correct
56 Correct 262 ms 22252 KB Output is correct
57 Correct 104 ms 12140 KB Output is correct
58 Correct 1 ms 364 KB Output is correct
59 Correct 0 ms 364 KB Output is correct
60 Correct 5 ms 748 KB Output is correct
61 Correct 109 ms 12140 KB Output is correct
62 Correct 1 ms 364 KB Output is correct
63 Correct 1 ms 364 KB Output is correct
64 Correct 1 ms 364 KB Output is correct
65 Correct 65 ms 5868 KB Output is correct
66 Correct 103 ms 12140 KB Output is correct
67 Correct 107 ms 12140 KB Output is correct
68 Correct 107 ms 12140 KB Output is correct
69 Correct 101 ms 12140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 11 ms 1388 KB Output is correct
7 Correct 255 ms 22124 KB Output is correct
8 Correct 1 ms 376 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 0 ms 364 KB Output is correct
12 Correct 11 ms 1260 KB Output is correct
13 Correct 279 ms 22124 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 5 ms 748 KB Output is correct
17 Correct 121 ms 12128 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 66 ms 5868 KB Output is correct
21 Correct 256 ms 22112 KB Output is correct
22 Correct 257 ms 22124 KB Output is correct
23 Correct 269 ms 22124 KB Output is correct
24 Correct 260 ms 22124 KB Output is correct
25 Correct 109 ms 12232 KB Output is correct
26 Correct 103 ms 12140 KB Output is correct
27 Correct 258 ms 22124 KB Output is correct
28 Correct 290 ms 22124 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 0 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 11 ms 1260 KB Output is correct
37 Correct 264 ms 22124 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 11 ms 1260 KB Output is correct
41 Correct 264 ms 22252 KB Output is correct
42 Correct 0 ms 364 KB Output is correct
43 Correct 0 ms 364 KB Output is correct
44 Correct 5 ms 748 KB Output is correct
45 Correct 112 ms 12140 KB Output is correct
46 Correct 1 ms 364 KB Output is correct
47 Correct 0 ms 364 KB Output is correct
48 Correct 1 ms 364 KB Output is correct
49 Correct 78 ms 5868 KB Output is correct
50 Correct 267 ms 22252 KB Output is correct
51 Correct 277 ms 22252 KB Output is correct
52 Correct 264 ms 22252 KB Output is correct
53 Correct 131 ms 12140 KB Output is correct
54 Correct 107 ms 12140 KB Output is correct
55 Correct 257 ms 22124 KB Output is correct
56 Correct 262 ms 22252 KB Output is correct
57 Correct 104 ms 12140 KB Output is correct
58 Correct 0 ms 364 KB Output is correct
59 Correct 0 ms 364 KB Output is correct
60 Correct 1 ms 364 KB Output is correct
61 Correct 67 ms 5868 KB Output is correct
62 Correct 255 ms 22124 KB Output is correct
63 Correct 252 ms 22124 KB Output is correct
64 Correct 259 ms 22124 KB Output is correct
65 Correct 1 ms 364 KB Output is correct
66 Correct 65 ms 5868 KB Output is correct
67 Correct 254 ms 22124 KB Output is correct
68 Correct 252 ms 22124 KB Output is correct
69 Correct 260 ms 22124 KB Output is correct
70 Correct 1 ms 364 KB Output is correct
71 Correct 1 ms 364 KB Output is correct
72 Correct 1 ms 364 KB Output is correct
73 Correct 65 ms 5868 KB Output is correct
74 Correct 258 ms 22124 KB Output is correct
75 Correct 258 ms 22124 KB Output is correct
76 Correct 259 ms 22124 KB Output is correct
77 Correct 255 ms 22124 KB Output is correct
78 Correct 1 ms 364 KB Output is correct
79 Correct 0 ms 364 KB Output is correct
80 Correct 5 ms 748 KB Output is correct
81 Correct 109 ms 12140 KB Output is correct
82 Correct 1 ms 364 KB Output is correct
83 Correct 1 ms 364 KB Output is correct
84 Correct 1 ms 364 KB Output is correct
85 Correct 65 ms 5868 KB Output is correct
86 Correct 103 ms 12140 KB Output is correct
87 Correct 107 ms 12140 KB Output is correct
88 Correct 107 ms 12140 KB Output is correct
89 Correct 101 ms 12140 KB Output is correct
90 Correct 1 ms 384 KB Output is correct
91 Correct 1 ms 364 KB Output is correct
92 Correct 5 ms 748 KB Output is correct
93 Correct 107 ms 12140 KB Output is correct
94 Correct 1 ms 364 KB Output is correct
95 Correct 1 ms 364 KB Output is correct
96 Correct 1 ms 364 KB Output is correct
97 Correct 27 ms 3308 KB Output is correct
98 Correct 104 ms 12140 KB Output is correct
99 Correct 108 ms 12140 KB Output is correct
100 Correct 104 ms 12140 KB Output is correct
101 Correct 102 ms 12140 KB Output is correct
102 Correct 98 ms 12140 KB Output is correct
103 Correct 102 ms 12116 KB Output is correct
104 Correct 98 ms 12268 KB Output is correct
105 Correct 113 ms 12140 KB Output is correct