답안 #996401

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
996401 2024-06-10T14:40:29 Z phoenix 슈퍼트리 잇기 (IOI20_supertrees) C++17
100 / 100
163 ms 30216 KB
#include<bits/stdc++.h>
#include "supertrees.h"

using namespace std;

struct dsu {
    vector<int> p;
    vector<vector<int>> v;
    void init(int n) {
        p.resize(n);
        v.resize(n);
        for(int i = 0;i < n;i++) {
            p[ i ] = i;
            v[ i ].push_back({ i });
        }
    }
    int find(int x) {
        return p[ x ] = (p[ x ] == x ? p[ x ] : find(p[ x ]));
    }
    void unite(int a, int b) {
        a = find(a);
        b = find(b);
        if(a == b) return;
        if(v[ a ].size() < v[ b ].size()) swap(a, b);
        for(int i = 0;i < v[ b ].size();i++) 
            v[ a ].push_back(v[ b ][ i ]);
        p[ b ] = a;
    }
};

const int N = 1000;
vector<vector<int>> rs;
vector<int> g[ N ];

int rt;
bool visited[ N ];
int dfs(int s, int rt) {
    visited[ s ] = 1;
    for(int c : g[ s ]) {
        if(visited[ c ])
            continue;
        rs[ s ][ c ] = 1;
        rs[ c ][ s ] = 1;
        return dfs(c, rt) + 1;
    }
    if(s != rt) {
        rs[ s ][ rt ] = 1;
        rs[ rt ][ s ] = 1;
    }
    return 1;
}

// void build(vector<vector<int>> p) {
//     for(auto c : p) {
//         for(int x : c) cout << x << ' ';
//         cout << '\n';
//     }
// }

int construct(vector<vector<int>> p) {
    int n = p.size();
    dsu ds;
    dsu ds0;
    ds.init(n);
    ds0.init(n);
    rs.resize(n, vector<int>(n));
    vector<pair<int, int>> e[3];
    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 ]) {
                ds0.unite(i, j);
                if(p[ i ][ j ] == 1)
                    ds.unite(i, j); 
            }         
            e[p[ i ][ j ]].push_back({i, j});
       }
    }
    for(pair<int, int> c : e[ 0 ]) 
        if(ds0.find(c.first) == ds0.find(c.second)) 
            return 0;
    for(pair<int, int> c : e[ 2 ]) 
        if(ds.find(c.first) == ds.find(c.second)) 
            return 0;
        else {
            g[ ds.find(c.first) ].push_back(ds.find(c.second));
            g[ ds.find(c.second) ].push_back(ds.find(c.first));
        }
    for(int i = 0;i < n;i++) {
        if(visited[ i ]) continue;
        if(dfs(i, i) == 2) return 0;
    }
    bool us[ n ] = {};
    for(int i = 0;i < n;i++) {
        if(us[ i ]) continue;
        int x = ds.find(i);
        for(int to : ds.v[ x ]) {
            if(to == x) continue;
            us[ to ] = 1;
            rs[ x ][ to ] = 1;
            rs[ to ][ x ] = 1;
        }
    }
    build(rs);
    return 1;
}

// int main() {
//     int n;
//     cin >> n;
//     vector<vector<int>> p(n, vector<int>(n));
//     for(int i = 0;i < n;i++) {
//         for(int j = 0;j < n;j++) {
//             cin >> p[i][j];
//         }
//     }
//     cout << construct(p);
// }

Compilation message

supertrees.cpp: In member function 'void dsu::unite(int, int)':
supertrees.cpp:25:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |         for(int i = 0;i < v[ b ].size();i++)
      |                       ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 6 ms 1504 KB Output is correct
7 Correct 133 ms 26056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 6 ms 1504 KB Output is correct
7 Correct 133 ms 26056 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 456 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 8 ms 1504 KB Output is correct
13 Correct 124 ms 26052 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 1116 KB Output is correct
17 Correct 63 ms 18240 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 32 ms 6860 KB Output is correct
21 Correct 123 ms 26080 KB Output is correct
22 Correct 144 ms 26048 KB Output is correct
23 Correct 135 ms 26040 KB Output is correct
24 Correct 129 ms 26052 KB Output is correct
25 Correct 61 ms 20196 KB Output is correct
26 Correct 56 ms 19768 KB Output is correct
27 Correct 150 ms 26040 KB Output is correct
28 Correct 125 ms 26052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 428 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 5 ms 1444 KB Output is correct
9 Correct 123 ms 26104 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 8 ms 1600 KB Output is correct
13 Correct 138 ms 30216 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 1372 KB Output is correct
17 Correct 67 ms 18280 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 31 ms 6864 KB Output is correct
22 Correct 135 ms 26592 KB Output is correct
23 Correct 119 ms 26052 KB Output is correct
24 Correct 134 ms 29316 KB Output is correct
25 Correct 84 ms 18820 KB Output is correct
26 Correct 58 ms 19176 KB Output is correct
27 Correct 117 ms 26052 KB Output is correct
28 Correct 134 ms 28176 KB Output is correct
29 Correct 59 ms 19652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 33 ms 6860 KB Output is correct
5 Correct 131 ms 26596 KB Output is correct
6 Correct 121 ms 26052 KB Output is correct
7 Correct 131 ms 26120 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 32 ms 6800 KB Output is correct
10 Correct 163 ms 26596 KB Output is correct
11 Correct 129 ms 26308 KB Output is correct
12 Correct 136 ms 29108 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 460 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 32 ms 6884 KB Output is correct
17 Correct 141 ms 27360 KB Output is correct
18 Correct 125 ms 26668 KB Output is correct
19 Correct 127 ms 26376 KB Output is correct
20 Correct 133 ms 26052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 6 ms 1504 KB Output is correct
7 Correct 133 ms 26056 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 456 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 8 ms 1504 KB Output is correct
13 Correct 124 ms 26052 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 1116 KB Output is correct
17 Correct 63 ms 18240 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 32 ms 6860 KB Output is correct
21 Correct 123 ms 26080 KB Output is correct
22 Correct 144 ms 26048 KB Output is correct
23 Correct 135 ms 26040 KB Output is correct
24 Correct 129 ms 26052 KB Output is correct
25 Correct 61 ms 20196 KB Output is correct
26 Correct 56 ms 19768 KB Output is correct
27 Correct 150 ms 26040 KB Output is correct
28 Correct 125 ms 26052 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 428 KB Output is correct
32 Correct 0 ms 344 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 5 ms 1444 KB Output is correct
37 Correct 123 ms 26104 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 8 ms 1600 KB Output is correct
41 Correct 138 ms 30216 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 3 ms 1372 KB Output is correct
45 Correct 67 ms 18280 KB Output is correct
46 Correct 0 ms 344 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 31 ms 6864 KB Output is correct
50 Correct 135 ms 26592 KB Output is correct
51 Correct 119 ms 26052 KB Output is correct
52 Correct 134 ms 29316 KB Output is correct
53 Correct 84 ms 18820 KB Output is correct
54 Correct 58 ms 19176 KB Output is correct
55 Correct 117 ms 26052 KB Output is correct
56 Correct 134 ms 28176 KB Output is correct
57 Correct 59 ms 19652 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 4 ms 1116 KB Output is correct
61 Correct 67 ms 18552 KB Output is correct
62 Correct 0 ms 344 KB Output is correct
63 Correct 0 ms 348 KB Output is correct
64 Correct 0 ms 348 KB Output is correct
65 Correct 31 ms 6864 KB Output is correct
66 Correct 60 ms 18920 KB Output is correct
67 Correct 55 ms 20712 KB Output is correct
68 Correct 53 ms 18644 KB Output is correct
69 Correct 60 ms 20280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 6 ms 1504 KB Output is correct
7 Correct 133 ms 26056 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 456 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 8 ms 1504 KB Output is correct
13 Correct 124 ms 26052 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 1116 KB Output is correct
17 Correct 63 ms 18240 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 32 ms 6860 KB Output is correct
21 Correct 123 ms 26080 KB Output is correct
22 Correct 144 ms 26048 KB Output is correct
23 Correct 135 ms 26040 KB Output is correct
24 Correct 129 ms 26052 KB Output is correct
25 Correct 61 ms 20196 KB Output is correct
26 Correct 56 ms 19768 KB Output is correct
27 Correct 150 ms 26040 KB Output is correct
28 Correct 125 ms 26052 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 428 KB Output is correct
32 Correct 0 ms 344 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 5 ms 1444 KB Output is correct
37 Correct 123 ms 26104 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 8 ms 1600 KB Output is correct
41 Correct 138 ms 30216 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 3 ms 1372 KB Output is correct
45 Correct 67 ms 18280 KB Output is correct
46 Correct 0 ms 344 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 31 ms 6864 KB Output is correct
50 Correct 135 ms 26592 KB Output is correct
51 Correct 119 ms 26052 KB Output is correct
52 Correct 134 ms 29316 KB Output is correct
53 Correct 84 ms 18820 KB Output is correct
54 Correct 58 ms 19176 KB Output is correct
55 Correct 117 ms 26052 KB Output is correct
56 Correct 134 ms 28176 KB Output is correct
57 Correct 59 ms 19652 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 33 ms 6860 KB Output is correct
62 Correct 131 ms 26596 KB Output is correct
63 Correct 121 ms 26052 KB Output is correct
64 Correct 131 ms 26120 KB Output is correct
65 Correct 0 ms 348 KB Output is correct
66 Correct 32 ms 6800 KB Output is correct
67 Correct 163 ms 26596 KB Output is correct
68 Correct 129 ms 26308 KB Output is correct
69 Correct 136 ms 29108 KB Output is correct
70 Correct 0 ms 348 KB Output is correct
71 Correct 0 ms 460 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 32 ms 6884 KB Output is correct
74 Correct 141 ms 27360 KB Output is correct
75 Correct 125 ms 26668 KB Output is correct
76 Correct 127 ms 26376 KB Output is correct
77 Correct 133 ms 26052 KB Output is correct
78 Correct 0 ms 348 KB Output is correct
79 Correct 0 ms 348 KB Output is correct
80 Correct 4 ms 1116 KB Output is correct
81 Correct 67 ms 18552 KB Output is correct
82 Correct 0 ms 344 KB Output is correct
83 Correct 0 ms 348 KB Output is correct
84 Correct 0 ms 348 KB Output is correct
85 Correct 31 ms 6864 KB Output is correct
86 Correct 60 ms 18920 KB Output is correct
87 Correct 55 ms 20712 KB Output is correct
88 Correct 53 ms 18644 KB Output is correct
89 Correct 60 ms 20280 KB Output is correct
90 Correct 0 ms 348 KB Output is correct
91 Correct 0 ms 344 KB Output is correct
92 Correct 3 ms 860 KB Output is correct
93 Correct 57 ms 14364 KB Output is correct
94 Correct 0 ms 348 KB Output is correct
95 Correct 0 ms 348 KB Output is correct
96 Correct 0 ms 348 KB Output is correct
97 Correct 13 ms 4252 KB Output is correct
98 Correct 50 ms 14836 KB Output is correct
99 Correct 54 ms 15340 KB Output is correct
100 Correct 51 ms 15320 KB Output is correct
101 Correct 52 ms 19204 KB Output is correct
102 Correct 50 ms 14416 KB Output is correct
103 Correct 54 ms 14220 KB Output is correct
104 Correct 50 ms 14344 KB Output is correct
105 Correct 59 ms 14232 KB Output is correct