답안 #705872

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
705872 2023-03-05T14:43:09 Z Alihan_8 슈퍼트리 잇기 (IOI20_supertrees) C++17
100 / 100
236 ms 24208 KB
#include <bits/stdc++.h>
#include "supertrees.h"
// include <ext/pb_ds/assoc_container.hpp>
// include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define all(x) x.begin(), x.end()
#define pb push_back
// define ordered_set tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
#define mpr make_pair
#define ln '\n'
void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
//#define int long long
// don't forget to call "build" and return (true or false)
int construct(vector <vector<int>> p){
    int n = (int)p.size();
    vector <vector<int>> ans(n, vector <int> (n));
    struct DSU{
        vector <int> par;
        void modify(int n){
            par.resize(n);
            iota(all(par), 0);
        }
        int find(int x){
            return x == par[x] ? x : par[x] = find(par[x]);
        }
        bool merge(int x, int y){
            x = find(x), y = find(y);
            if ( x == y ) return false;
            par[y] = x; return true;
        }
        bool same(int x, int y){
            return find(x) == find(y);
        };
    } L, R;
    L.modify(n), R.modify(n);
    // L - with one edge
    for ( int i = 0; i < n; i++ ){
        for ( int j = 0; j < n; j++ ){
            if ( i == j ) continue;
            if ( p[i][j] == 3 ) return false;
            if ( p[i][j] ) R.merge(i, j);
            if ( p[i][j] == 1 ) L.merge(i, j);
        }
    }
    for ( int i = 0; i < n; i++ ){
        for ( int j = 0; j < n; j++ ){
            if ( i == j ) continue;
            if ( !p[i][j] ){
                if ( R.same(i, j) ) return false;
                continue;
            }
            auto flag = p[i][j] != 1;
            if ( flag and L.same(i, j) ) return false;
        }
    }
    vector <int> used(n), roots[n];
    for ( int i = 0; i < n; i++ ){
        if ( used[i] ) continue;
        used[i] = true;
        roots[R.find(i)].pb(i);
        for ( int j = 0; j < n; j++ ){
            if ( i != j and L.same(i, j) ){
                ans[i][j] = ans[j][i] = true;
                used[j] = true;
            }
        }
    }
    for ( auto it: roots ){
        int sz = (int)it.size();
        if ( sz == 2 ) return false;
        for ( int i = 0; i < sz; i++ ){
            ans[it[i]][it[(i+1)%sz]] = true;
        }
    }
    for ( int i = 0; i < n; i++ ){
        for ( int j = 0; j < n; j++ ){
            if ( ans[i][j] ) ans[j][i] = true;
        }
        ans[i][i] = false;
    }
    build(ans);
    return true;
}
#if false
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n; cin >> n;
    vector <vector<int>> p(n, vector <int> (n));
    for ( auto &i: p ){
        for ( auto &j: i ) cin >> j;
    }
    construct(p);

    cout << '\n';
/*
4
1 1 2 2
1 1 2 2
2 2 1 2
2 2 2 1
*/
}
#endif

Compilation message

supertrees.cpp: In function 'void IO(std::string)':
supertrees.cpp:12:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 | void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
supertrees.cpp:12:70: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 | void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                                                               ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 296 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 300 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
6 Correct 8 ms 1236 KB Output is correct
7 Correct 184 ms 22400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 296 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 300 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
6 Correct 8 ms 1236 KB Output is correct
7 Correct 184 ms 22400 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 300 KB Output is correct
11 Correct 1 ms 296 KB Output is correct
12 Correct 8 ms 1236 KB Output is correct
13 Correct 190 ms 22432 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 4 ms 892 KB Output is correct
17 Correct 87 ms 12492 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 49 ms 6220 KB Output is correct
21 Correct 179 ms 22404 KB Output is correct
22 Correct 181 ms 22432 KB Output is correct
23 Correct 199 ms 22344 KB Output is correct
24 Correct 202 ms 22388 KB Output is correct
25 Correct 70 ms 12492 KB Output is correct
26 Correct 68 ms 12460 KB Output is correct
27 Correct 236 ms 22428 KB Output is correct
28 Correct 175 ms 22408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 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 1208 KB Output is correct
9 Correct 177 ms 23784 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 8 ms 1236 KB Output is correct
13 Correct 190 ms 23800 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 300 KB Output is correct
16 Correct 5 ms 852 KB Output is correct
17 Correct 93 ms 14068 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 52 ms 6320 KB Output is correct
22 Correct 189 ms 23904 KB Output is correct
23 Correct 217 ms 23820 KB Output is correct
24 Correct 205 ms 23824 KB Output is correct
25 Correct 102 ms 14156 KB Output is correct
26 Correct 79 ms 14080 KB Output is correct
27 Correct 177 ms 23776 KB Output is correct
28 Correct 203 ms 23840 KB Output is correct
29 Correct 70 ms 14084 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 45 ms 6200 KB Output is correct
5 Correct 180 ms 24012 KB Output is correct
6 Correct 176 ms 24012 KB Output is correct
7 Correct 191 ms 23936 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 50 ms 6152 KB Output is correct
10 Correct 182 ms 24004 KB Output is correct
11 Correct 185 ms 23920 KB Output is correct
12 Correct 194 ms 24072 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 304 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 48 ms 6256 KB Output is correct
17 Correct 178 ms 23968 KB Output is correct
18 Correct 179 ms 24184 KB Output is correct
19 Correct 182 ms 23928 KB Output is correct
20 Correct 176 ms 24208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 296 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 300 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
6 Correct 8 ms 1236 KB Output is correct
7 Correct 184 ms 22400 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 300 KB Output is correct
11 Correct 1 ms 296 KB Output is correct
12 Correct 8 ms 1236 KB Output is correct
13 Correct 190 ms 22432 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 4 ms 892 KB Output is correct
17 Correct 87 ms 12492 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 49 ms 6220 KB Output is correct
21 Correct 179 ms 22404 KB Output is correct
22 Correct 181 ms 22432 KB Output is correct
23 Correct 199 ms 22344 KB Output is correct
24 Correct 202 ms 22388 KB Output is correct
25 Correct 70 ms 12492 KB Output is correct
26 Correct 68 ms 12460 KB Output is correct
27 Correct 236 ms 22428 KB Output is correct
28 Correct 175 ms 22408 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 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 1208 KB Output is correct
37 Correct 177 ms 23784 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 8 ms 1236 KB Output is correct
41 Correct 190 ms 23800 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 300 KB Output is correct
44 Correct 5 ms 852 KB Output is correct
45 Correct 93 ms 14068 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 52 ms 6320 KB Output is correct
50 Correct 189 ms 23904 KB Output is correct
51 Correct 217 ms 23820 KB Output is correct
52 Correct 205 ms 23824 KB Output is correct
53 Correct 102 ms 14156 KB Output is correct
54 Correct 79 ms 14080 KB Output is correct
55 Correct 177 ms 23776 KB Output is correct
56 Correct 203 ms 23840 KB Output is correct
57 Correct 70 ms 14084 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 5 ms 852 KB Output is correct
61 Correct 95 ms 14068 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 1 ms 212 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Correct 50 ms 6220 KB Output is correct
66 Correct 75 ms 14028 KB Output is correct
67 Correct 69 ms 14084 KB Output is correct
68 Correct 73 ms 13988 KB Output is correct
69 Correct 71 ms 14064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 296 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 300 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
6 Correct 8 ms 1236 KB Output is correct
7 Correct 184 ms 22400 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 300 KB Output is correct
11 Correct 1 ms 296 KB Output is correct
12 Correct 8 ms 1236 KB Output is correct
13 Correct 190 ms 22432 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 4 ms 892 KB Output is correct
17 Correct 87 ms 12492 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 49 ms 6220 KB Output is correct
21 Correct 179 ms 22404 KB Output is correct
22 Correct 181 ms 22432 KB Output is correct
23 Correct 199 ms 22344 KB Output is correct
24 Correct 202 ms 22388 KB Output is correct
25 Correct 70 ms 12492 KB Output is correct
26 Correct 68 ms 12460 KB Output is correct
27 Correct 236 ms 22428 KB Output is correct
28 Correct 175 ms 22408 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 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 1208 KB Output is correct
37 Correct 177 ms 23784 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 8 ms 1236 KB Output is correct
41 Correct 190 ms 23800 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 300 KB Output is correct
44 Correct 5 ms 852 KB Output is correct
45 Correct 93 ms 14068 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 52 ms 6320 KB Output is correct
50 Correct 189 ms 23904 KB Output is correct
51 Correct 217 ms 23820 KB Output is correct
52 Correct 205 ms 23824 KB Output is correct
53 Correct 102 ms 14156 KB Output is correct
54 Correct 79 ms 14080 KB Output is correct
55 Correct 177 ms 23776 KB Output is correct
56 Correct 203 ms 23840 KB Output is correct
57 Correct 70 ms 14084 KB Output is correct
58 Correct 0 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 45 ms 6200 KB Output is correct
62 Correct 180 ms 24012 KB Output is correct
63 Correct 176 ms 24012 KB Output is correct
64 Correct 191 ms 23936 KB Output is correct
65 Correct 1 ms 212 KB Output is correct
66 Correct 50 ms 6152 KB Output is correct
67 Correct 182 ms 24004 KB Output is correct
68 Correct 185 ms 23920 KB Output is correct
69 Correct 194 ms 24072 KB Output is correct
70 Correct 1 ms 212 KB Output is correct
71 Correct 0 ms 304 KB Output is correct
72 Correct 1 ms 212 KB Output is correct
73 Correct 48 ms 6256 KB Output is correct
74 Correct 178 ms 23968 KB Output is correct
75 Correct 179 ms 24184 KB Output is correct
76 Correct 182 ms 23928 KB Output is correct
77 Correct 176 ms 24208 KB Output is correct
78 Correct 1 ms 212 KB Output is correct
79 Correct 1 ms 212 KB Output is correct
80 Correct 5 ms 852 KB Output is correct
81 Correct 95 ms 14068 KB Output is correct
82 Correct 1 ms 212 KB Output is correct
83 Correct 1 ms 212 KB Output is correct
84 Correct 1 ms 212 KB Output is correct
85 Correct 50 ms 6220 KB Output is correct
86 Correct 75 ms 14028 KB Output is correct
87 Correct 69 ms 14084 KB Output is correct
88 Correct 73 ms 13988 KB Output is correct
89 Correct 71 ms 14064 KB Output is correct
90 Correct 1 ms 300 KB Output is correct
91 Correct 1 ms 212 KB Output is correct
92 Correct 4 ms 852 KB Output is correct
93 Correct 81 ms 14068 KB Output is correct
94 Correct 1 ms 212 KB Output is correct
95 Correct 0 ms 300 KB Output is correct
96 Correct 1 ms 212 KB Output is correct
97 Correct 18 ms 3704 KB Output is correct
98 Correct 70 ms 14080 KB Output is correct
99 Correct 71 ms 14028 KB Output is correct
100 Correct 67 ms 14076 KB Output is correct
101 Correct 68 ms 14068 KB Output is correct
102 Correct 73 ms 13996 KB Output is correct
103 Correct 76 ms 14064 KB Output is correct
104 Correct 75 ms 14064 KB Output is correct
105 Correct 80 ms 14028 KB Output is correct