답안 #705788

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
705788 2023-03-05T09:06:42 Z finn__ 슈퍼트리 잇기 (IOI20_supertrees) C++17
100 / 100
238 ms 24072 KB
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;

struct Dsu
{
    vector<int64_t> t;

    Dsu(size_t n) { t = vector<int64_t>(n, -1); }

    int64_t repr(int64_t u) { return t[u] < 0 ? u : t[u] = repr(t[u]); }

    void merge(int64_t u, int64_t v)
    {
        u = repr(u);
        v = repr(v);
        if (u == v)
            return;

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

    bool same_set(int64_t u, int64_t v) { return repr(u) == repr(v); }
};

int construct(vector<vector<int>> p)
{
    Dsu connected(p.size()), one_connected(p.size());

    for (size_t i = 0; i < p.size(); i++)
        for (size_t j = 0; j < p.size(); j++)
        {
            if (p[i][j] == 3)
                return 0;
            if (p[i][j])
                connected.merge(i, j);
            if (p[i][j] == 1)
                one_connected.merge(i, j);
        }

    for (size_t i = 0; i < p.size(); i++)
        for (size_t j = 0; j < p.size(); j++)
        {
            if ((p[i][j] == 0 && connected.same_set(i, j)) ||
                (p[i][j] != 0 && !connected.same_set(i, j)))
                return 0;
            if ((p[i][j] == 1 && !one_connected.same_set(i, j)) ||
                (p[i][j] != 1 && one_connected.same_set(i, j)))
                return 0;
            if ((p[i][j] == 2 && !(connected.same_set(i, j) && !one_connected.same_set(i, j))) ||
                (p[i][j] != 2 && connected.same_set(i, j) && !one_connected.same_set(i, j)))
                return 0;
        }

    vector<vector<int>> adj(p.size(), vector<int>(p.size(), 0));
    vector<vector<size_t>> roots(p.size());
    vector<bool> visited(p.size(), 0);

    for (size_t i = 0; i < p.size(); i++)
        if (!visited[i])
        {
            visited[i] = 1;
            roots[connected.repr(i)].push_back(i);
            for (size_t j = 0; j < p.size(); j++)
                if (j != i && one_connected.same_set(i, j))
                {
                    adj[i][j] = adj[j][i] = 1;
                    visited[j] = 1;
                }
        }
    for (auto const &v : roots)
    {
        if (v.size() > 1)
            for (size_t i = 0; i < v.size(); i++)
                adj[v[i]][v[(i + 1) % v.size()]] = adj[v[(i + 1) % v.size()]][v[i]] = 1;
        if (v.size() == 2)
            return 0;
    }

    build(adj);
    return 1;
}
# 결과 실행 시간 메모리 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 1 ms 212 KB Output is correct
6 Correct 9 ms 1108 KB Output is correct
7 Correct 210 ms 22024 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 1 ms 212 KB Output is correct
6 Correct 9 ms 1108 KB Output is correct
7 Correct 210 ms 22024 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 1 ms 212 KB Output is correct
12 Correct 9 ms 1136 KB Output is correct
13 Correct 181 ms 22012 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 596 KB Output is correct
17 Correct 83 ms 8196 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 66 ms 5676 KB Output is correct
21 Correct 186 ms 22104 KB Output is correct
22 Correct 189 ms 22016 KB Output is correct
23 Correct 218 ms 22276 KB Output is correct
24 Correct 187 ms 22028 KB Output is correct
25 Correct 68 ms 8076 KB Output is correct
26 Correct 68 ms 8196 KB Output is correct
27 Correct 209 ms 22028 KB Output is correct
28 Correct 181 ms 22092 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 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 8 ms 1276 KB Output is correct
9 Correct 178 ms 24060 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 9 ms 1248 KB Output is correct
13 Correct 201 ms 24048 KB Output is correct
14 Correct 0 ms 304 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 604 KB Output is correct
17 Correct 85 ms 10100 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 50 ms 6348 KB Output is correct
22 Correct 197 ms 24064 KB Output is correct
23 Correct 188 ms 24012 KB Output is correct
24 Correct 226 ms 24072 KB Output is correct
25 Correct 81 ms 14084 KB Output is correct
26 Correct 69 ms 10096 KB Output is correct
27 Correct 189 ms 24000 KB Output is correct
28 Correct 214 ms 23948 KB Output is correct
29 Correct 78 ms 14068 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 0 ms 212 KB Output is correct
4 Correct 48 ms 5672 KB Output is correct
5 Correct 185 ms 22092 KB Output is correct
6 Correct 191 ms 22032 KB Output is correct
7 Correct 210 ms 22036 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 56 ms 5696 KB Output is correct
10 Correct 238 ms 22092 KB Output is correct
11 Correct 190 ms 22000 KB Output is correct
12 Correct 205 ms 22032 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 49 ms 5780 KB Output is correct
17 Correct 195 ms 22008 KB Output is correct
18 Correct 201 ms 22072 KB Output is correct
19 Correct 192 ms 22064 KB Output is correct
20 Correct 193 ms 22108 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 1 ms 212 KB Output is correct
6 Correct 9 ms 1108 KB Output is correct
7 Correct 210 ms 22024 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 1 ms 212 KB Output is correct
12 Correct 9 ms 1136 KB Output is correct
13 Correct 181 ms 22012 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 596 KB Output is correct
17 Correct 83 ms 8196 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 66 ms 5676 KB Output is correct
21 Correct 186 ms 22104 KB Output is correct
22 Correct 189 ms 22016 KB Output is correct
23 Correct 218 ms 22276 KB Output is correct
24 Correct 187 ms 22028 KB Output is correct
25 Correct 68 ms 8076 KB Output is correct
26 Correct 68 ms 8196 KB Output is correct
27 Correct 209 ms 22028 KB Output is correct
28 Correct 181 ms 22092 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 8 ms 1276 KB Output is correct
37 Correct 178 ms 24060 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 9 ms 1248 KB Output is correct
41 Correct 201 ms 24048 KB Output is correct
42 Correct 0 ms 304 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 4 ms 604 KB Output is correct
45 Correct 85 ms 10100 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 50 ms 6348 KB Output is correct
50 Correct 197 ms 24064 KB Output is correct
51 Correct 188 ms 24012 KB Output is correct
52 Correct 226 ms 24072 KB Output is correct
53 Correct 81 ms 14084 KB Output is correct
54 Correct 69 ms 10096 KB Output is correct
55 Correct 189 ms 24000 KB Output is correct
56 Correct 214 ms 23948 KB Output is correct
57 Correct 78 ms 14068 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 4 ms 596 KB Output is correct
61 Correct 86 ms 10100 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 52 ms 6268 KB Output is correct
66 Correct 72 ms 10148 KB Output is correct
67 Correct 69 ms 10152 KB Output is correct
68 Correct 67 ms 10060 KB Output is correct
69 Correct 78 ms 14072 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 1 ms 212 KB Output is correct
6 Correct 9 ms 1108 KB Output is correct
7 Correct 210 ms 22024 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 1 ms 212 KB Output is correct
12 Correct 9 ms 1136 KB Output is correct
13 Correct 181 ms 22012 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 596 KB Output is correct
17 Correct 83 ms 8196 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 66 ms 5676 KB Output is correct
21 Correct 186 ms 22104 KB Output is correct
22 Correct 189 ms 22016 KB Output is correct
23 Correct 218 ms 22276 KB Output is correct
24 Correct 187 ms 22028 KB Output is correct
25 Correct 68 ms 8076 KB Output is correct
26 Correct 68 ms 8196 KB Output is correct
27 Correct 209 ms 22028 KB Output is correct
28 Correct 181 ms 22092 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 8 ms 1276 KB Output is correct
37 Correct 178 ms 24060 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 9 ms 1248 KB Output is correct
41 Correct 201 ms 24048 KB Output is correct
42 Correct 0 ms 304 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 4 ms 604 KB Output is correct
45 Correct 85 ms 10100 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 50 ms 6348 KB Output is correct
50 Correct 197 ms 24064 KB Output is correct
51 Correct 188 ms 24012 KB Output is correct
52 Correct 226 ms 24072 KB Output is correct
53 Correct 81 ms 14084 KB Output is correct
54 Correct 69 ms 10096 KB Output is correct
55 Correct 189 ms 24000 KB Output is correct
56 Correct 214 ms 23948 KB Output is correct
57 Correct 78 ms 14068 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 48 ms 5672 KB Output is correct
62 Correct 185 ms 22092 KB Output is correct
63 Correct 191 ms 22032 KB Output is correct
64 Correct 210 ms 22036 KB Output is correct
65 Correct 0 ms 212 KB Output is correct
66 Correct 56 ms 5696 KB Output is correct
67 Correct 238 ms 22092 KB Output is correct
68 Correct 190 ms 22000 KB Output is correct
69 Correct 205 ms 22032 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 49 ms 5780 KB Output is correct
74 Correct 195 ms 22008 KB Output is correct
75 Correct 201 ms 22072 KB Output is correct
76 Correct 192 ms 22064 KB Output is correct
77 Correct 193 ms 22108 KB Output is correct
78 Correct 0 ms 212 KB Output is correct
79 Correct 0 ms 212 KB Output is correct
80 Correct 4 ms 596 KB Output is correct
81 Correct 86 ms 10100 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 52 ms 6268 KB Output is correct
86 Correct 72 ms 10148 KB Output is correct
87 Correct 69 ms 10152 KB Output is correct
88 Correct 67 ms 10060 KB Output is correct
89 Correct 78 ms 14072 KB Output is correct
90 Correct 1 ms 308 KB Output is correct
91 Correct 0 ms 304 KB Output is correct
92 Correct 3 ms 596 KB Output is correct
93 Correct 76 ms 10100 KB Output is correct
94 Correct 0 ms 212 KB Output is correct
95 Correct 1 ms 212 KB Output is correct
96 Correct 0 ms 212 KB Output is correct
97 Correct 17 ms 2688 KB Output is correct
98 Correct 67 ms 10112 KB Output is correct
99 Correct 102 ms 10112 KB Output is correct
100 Correct 73 ms 10100 KB Output is correct
101 Correct 69 ms 10100 KB Output is correct
102 Correct 72 ms 10168 KB Output is correct
103 Correct 72 ms 10152 KB Output is correct
104 Correct 68 ms 10112 KB Output is correct
105 Correct 78 ms 10100 KB Output is correct