답안 #772880

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
772880 2023-07-04T12:16:44 Z fve5 슈퍼트리 잇기 (IOI20_supertrees) C++17
100 / 100
165 ms 26240 KB
#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;

struct DSU {
    vector<int> nodes;

    int find(int node) {
        if (nodes[node] < 0) return node;
        return nodes[node] = find(nodes[node]);
    }

    void join(int u, int v) {
        u = find(u), v = find(v);
        if (u == v) return;

        if (nodes[u] > nodes[v]) swap(u, v);
        nodes[u] += nodes[v];
        nodes[v] = u;
    }

    map<int, vector<int>> get_cc() {
        map<int, vector<int>> ans;
        for (int i = 0; i < nodes.size(); i++) {
            ans[find(i)].push_back(i);
        }
        return ans;
    }

    DSU(int size) : nodes(size, -1) { }
};

int construct(vector<vector<int>> p) {
    int n = p.size();
    
    vector adj(n, vector(n, 0));
    vector act_p(n, vector(n, 0));
    DSU dsu(n);
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (p[i][j] == 3) return 0;
            if (p[i][j]) dsu.join(i, j);
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (dsu.find(i) == dsu.find(j) && p[i][j] == 0) {
                return 0;
            }
        }
    }

    auto ccs = dsu.get_cc();
    for (auto [_, cc]: ccs) {
        bool found2 = false;
        DSU dsu2(cc.size());
        for (int i = 0; i < cc.size(); i++) {
            for (int j = 0; j < cc.size(); j++) {
                if (p[cc[i]][cc[j]] == 1) dsu2.join(i, j);
                if (p[cc[i]][cc[j]] == 2) found2 = true;
            }
        }
        for (int i = 0; i < cc.size(); i++) {
            for (int j = 0; j < cc.size(); j++) {
                //cout << i << " " << j << " " << dsu2.find(i) << " " << dsu2.find(j) << endl;
                if (dsu2.find(i) == dsu2.find(j) && p[cc[i]][cc[j]] == 2) return 0;
            }
        }

        auto ccs2 = dsu2.get_cc();
        vector<int> cycle;
        for (auto [r, cc2]: ccs2) {
            cycle.push_back(cc[r]);
            for (int i = 1; i < cc2.size(); i++) {
                adj[cc[cc2[i]]][cc[cc2[i - 1]]] = adj[cc[cc2[i - 1]]][cc[cc2[i]]] = 1;
            }
        }
        if (cycle.size() < 3) {
            if (found2) return 0;
            else continue;
        }
        for (int i = 0; i < cycle.size(); i++) {
            adj[cycle[i]][cycle[(i + 1) % cycle.size()]] = adj[cycle[(i + 1) % cycle.size()]][cycle[i]] = 1;
        }
    }

    build(adj);
    return 1;
}

Compilation message

supertrees.cpp: In member function 'std::map<int, std::vector<int> > DSU::get_cc()':
supertrees.cpp:24:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |         for (int i = 0; i < nodes.size(); i++) {
      |                         ~~^~~~~~~~~~~~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:57:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |         for (int i = 0; i < cc.size(); i++) {
      |                         ~~^~~~~~~~~~~
supertrees.cpp:58:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |             for (int j = 0; j < cc.size(); j++) {
      |                             ~~^~~~~~~~~~~
supertrees.cpp:63:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |         for (int i = 0; i < cc.size(); i++) {
      |                         ~~^~~~~~~~~~~
supertrees.cpp:64:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |             for (int j = 0; j < cc.size(); j++) {
      |                             ~~^~~~~~~~~~~
supertrees.cpp:74:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |             for (int i = 1; i < cc2.size(); i++) {
      |                             ~~^~~~~~~~~~~~
supertrees.cpp:82:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |         for (int i = 0; i < cycle.size(); i++) {
      |                         ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1356 KB Output is correct
7 Correct 159 ms 25988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1356 KB Output is correct
7 Correct 159 ms 25988 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 7 ms 1364 KB Output is correct
13 Correct 144 ms 26016 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 852 KB Output is correct
17 Correct 65 ms 16088 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 38 ms 6720 KB Output is correct
21 Correct 151 ms 26000 KB Output is correct
22 Correct 146 ms 25984 KB Output is correct
23 Correct 161 ms 25904 KB Output is correct
24 Correct 145 ms 25984 KB Output is correct
25 Correct 58 ms 16076 KB Output is correct
26 Correct 57 ms 16012 KB Output is correct
27 Correct 165 ms 26016 KB Output is correct
28 Correct 147 ms 26000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 6 ms 1364 KB Output is correct
9 Correct 145 ms 26000 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 6 ms 1364 KB Output is correct
13 Correct 150 ms 25996 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 852 KB Output is correct
17 Correct 64 ms 16004 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 40 ms 6716 KB Output is correct
22 Correct 149 ms 25932 KB Output is correct
23 Correct 150 ms 25932 KB Output is correct
24 Correct 159 ms 25920 KB Output is correct
25 Correct 60 ms 16076 KB Output is correct
26 Correct 58 ms 16076 KB Output is correct
27 Correct 146 ms 25976 KB Output is correct
28 Correct 153 ms 25988 KB Output is correct
29 Correct 63 ms 16128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 300 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 37 ms 6668 KB Output is correct
5 Correct 148 ms 25988 KB Output is correct
6 Correct 151 ms 26240 KB Output is correct
7 Correct 159 ms 26000 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 37 ms 6760 KB Output is correct
10 Correct 160 ms 26108 KB Output is correct
11 Correct 148 ms 26000 KB Output is correct
12 Correct 154 ms 26000 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 37 ms 6676 KB Output is correct
17 Correct 147 ms 26000 KB Output is correct
18 Correct 153 ms 26060 KB Output is correct
19 Correct 148 ms 25996 KB Output is correct
20 Correct 155 ms 26072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1356 KB Output is correct
7 Correct 159 ms 25988 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 7 ms 1364 KB Output is correct
13 Correct 144 ms 26016 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 852 KB Output is correct
17 Correct 65 ms 16088 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 38 ms 6720 KB Output is correct
21 Correct 151 ms 26000 KB Output is correct
22 Correct 146 ms 25984 KB Output is correct
23 Correct 161 ms 25904 KB Output is correct
24 Correct 145 ms 25984 KB Output is correct
25 Correct 58 ms 16076 KB Output is correct
26 Correct 57 ms 16012 KB Output is correct
27 Correct 165 ms 26016 KB Output is correct
28 Correct 147 ms 26000 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 6 ms 1364 KB Output is correct
37 Correct 145 ms 26000 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 6 ms 1364 KB Output is correct
41 Correct 150 ms 25996 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 3 ms 852 KB Output is correct
45 Correct 64 ms 16004 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 40 ms 6716 KB Output is correct
50 Correct 149 ms 25932 KB Output is correct
51 Correct 150 ms 25932 KB Output is correct
52 Correct 159 ms 25920 KB Output is correct
53 Correct 60 ms 16076 KB Output is correct
54 Correct 58 ms 16076 KB Output is correct
55 Correct 146 ms 25976 KB Output is correct
56 Correct 153 ms 25988 KB Output is correct
57 Correct 63 ms 16128 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 3 ms 852 KB Output is correct
61 Correct 63 ms 16004 KB Output is correct
62 Correct 0 ms 212 KB Output is correct
63 Correct 0 ms 212 KB Output is correct
64 Correct 0 ms 212 KB Output is correct
65 Correct 38 ms 6800 KB Output is correct
66 Correct 58 ms 16012 KB Output is correct
67 Correct 58 ms 16004 KB Output is correct
68 Correct 94 ms 16056 KB Output is correct
69 Correct 59 ms 16140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1356 KB Output is correct
7 Correct 159 ms 25988 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 7 ms 1364 KB Output is correct
13 Correct 144 ms 26016 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 852 KB Output is correct
17 Correct 65 ms 16088 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 38 ms 6720 KB Output is correct
21 Correct 151 ms 26000 KB Output is correct
22 Correct 146 ms 25984 KB Output is correct
23 Correct 161 ms 25904 KB Output is correct
24 Correct 145 ms 25984 KB Output is correct
25 Correct 58 ms 16076 KB Output is correct
26 Correct 57 ms 16012 KB Output is correct
27 Correct 165 ms 26016 KB Output is correct
28 Correct 147 ms 26000 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 6 ms 1364 KB Output is correct
37 Correct 145 ms 26000 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 6 ms 1364 KB Output is correct
41 Correct 150 ms 25996 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 3 ms 852 KB Output is correct
45 Correct 64 ms 16004 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 40 ms 6716 KB Output is correct
50 Correct 149 ms 25932 KB Output is correct
51 Correct 150 ms 25932 KB Output is correct
52 Correct 159 ms 25920 KB Output is correct
53 Correct 60 ms 16076 KB Output is correct
54 Correct 58 ms 16076 KB Output is correct
55 Correct 146 ms 25976 KB Output is correct
56 Correct 153 ms 25988 KB Output is correct
57 Correct 63 ms 16128 KB Output is correct
58 Correct 0 ms 300 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 37 ms 6668 KB Output is correct
62 Correct 148 ms 25988 KB Output is correct
63 Correct 151 ms 26240 KB Output is correct
64 Correct 159 ms 26000 KB Output is correct
65 Correct 0 ms 212 KB Output is correct
66 Correct 37 ms 6760 KB Output is correct
67 Correct 160 ms 26108 KB Output is correct
68 Correct 148 ms 26000 KB Output is correct
69 Correct 154 ms 26000 KB Output is correct
70 Correct 0 ms 212 KB Output is correct
71 Correct 0 ms 212 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 37 ms 6676 KB Output is correct
74 Correct 147 ms 26000 KB Output is correct
75 Correct 153 ms 26060 KB Output is correct
76 Correct 148 ms 25996 KB Output is correct
77 Correct 155 ms 26072 KB Output is correct
78 Correct 0 ms 212 KB Output is correct
79 Correct 0 ms 212 KB Output is correct
80 Correct 3 ms 852 KB Output is correct
81 Correct 63 ms 16004 KB Output is correct
82 Correct 0 ms 212 KB Output is correct
83 Correct 0 ms 212 KB Output is correct
84 Correct 0 ms 212 KB Output is correct
85 Correct 38 ms 6800 KB Output is correct
86 Correct 58 ms 16012 KB Output is correct
87 Correct 58 ms 16004 KB Output is correct
88 Correct 94 ms 16056 KB Output is correct
89 Correct 59 ms 16140 KB Output is correct
90 Correct 0 ms 212 KB Output is correct
91 Correct 0 ms 212 KB Output is correct
92 Correct 3 ms 852 KB Output is correct
93 Correct 60 ms 16004 KB Output is correct
94 Correct 0 ms 212 KB Output is correct
95 Correct 1 ms 212 KB Output is correct
96 Correct 1 ms 212 KB Output is correct
97 Correct 15 ms 4664 KB Output is correct
98 Correct 60 ms 18032 KB Output is correct
99 Correct 61 ms 18016 KB Output is correct
100 Correct 76 ms 18036 KB Output is correct
101 Correct 72 ms 17964 KB Output is correct
102 Correct 71 ms 18040 KB Output is correct
103 Correct 59 ms 17996 KB Output is correct
104 Correct 58 ms 17916 KB Output is correct
105 Correct 63 ms 18036 KB Output is correct