답안 #329835

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
329835 2020-11-22T18:59:47 Z jebouin 슈퍼트리 잇기 (IOI20_supertrees) C++17
100 / 100
279 ms 30828 KB
#include "supertrees.h"

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define rng(a) a.begin(), a.end()
#define ina(n,a) cin >> n; for(int i = 1; i <= n; i++) cin >> a[i]
#define sz(x) (int)(x).size()
#define se second
#define fi first
#define prev            coyhhhhhhyoc    
#define next          sdNNNmNNNNNNNmds  
#define y0           hNNNNy_yNNNNNN_sNh
#define y1          mNNNNNdtdNNNNNNtsNNm
#define yn          mNNNNNNNNy___smNNNms
#define tm            oooooosyysooooot 
#define read               tyhyt        
#define rank              ytmNmo         
#define index              yyy
#define pb push_back
#define pcnt __builtin_popcountll
#define rrep(i,a,b) for(int i = (b); i >= (a); i--)
#define rall(x,a) for(auto x : a)
#define MOD 1000000007
#define endl "\n"
typedef long long ll;
using namespace std;

const int N = 1111;
int p[N][N], b[N][N], dsu[N], rank[N];
int n;

int solve1() {
    rep(i, 1, n - 1) {
        int ii = i % n + 1;
        b[i][ii] = 1;
        b[ii][i] = 1;
    }
    return 1;
}

int find(int u) {
    if(dsu[u] == u) return u;
    dsu[u] = find(dsu[u]);
    return dsu[u];
}

bool merge(int u, int v) {
    u = find(u), v = find(v);
    if(u == v) return false;
    if(rank[u] > rank[v]) {
        dsu[v] = u;
    } else {
        dsu[u] = v;
        if(rank[u] == rank[v]) {
            rank[v]++;
        }
    }
    return true;
}

int buildDSU() {
    rep(i, 1, n) {
        dsu[i] = i;
    }
    rep(i, 1, n) {
        rep(j, 1, n) {
            if(p[i][j]) {
                merge(i, j);
            }
        }
    }
    return 1;
}

int solve2() {
    if(!buildDSU()) {
        return 0;
    }
    rep(i, 1, n) {
        rep(j, 1, n) {
            int u = find(i), v = find(j);
            if((u == v) != p[i][j]) {
                return 0;
            }
        }
    }
    rep(u, 1, n) {
        int v = find(u);
        if(u != v) {
            b[u][v] = 1;
            b[v][u] = 1;
        }
    }
    return 1;
}

int solve3() {
    if(!buildDSU()) {
        return 0;
    }
    vector<vector<int> > gps(n + 1);
    rep(u, 1, n) {
        int r = find(u);
        gps[r].pb(u);
    }
    rep(i, 1, n) {
        rep(j, i + 1, n) {
            int u = find(i), v = find(j);
            if((u == v) != (p[i][j] == 2)) {
                return 0;
            }
            if(p[i][j] == 2 && sz(gps[u]) < 3) {
                return 0;
            }
        }
    }
    rep(r, 1, n) {
        int k = sz(gps[r]);
        rep(i, 0, k - 1) {
            int u = gps[r][i], v = gps[r][(i + 1) % k];
            if(u != v) {
                b[u][v] = 1;
                b[v][u] = 1;
            }
        }
    }
    return 1;
}

int updateComp(vector<int>& comp) {
    set<int> st;
    rall(u, comp) {
        st.insert(u);
        rall(v, comp) {
            if(u == v) continue;
            if(p[u][v] == 0) {
                return 0;
            }
        }
    }
    map<vector<int>, int> trees;
    rep(u, 1, n) {
        if(st.find(u) == st.end()) continue;
        vector<int> tree;
        rep(v, 1, n) {
            if(st.find(v) == st.end()) continue;
            if(p[u][v] == 1) {
                tree.pb(v);
            }
        }
        trees[tree]++;
    }
    int fi, pr = -1;
    rall(pa, trees) {
        auto tree = pa.fi;
        if(tree.size() != pa.se) {
            return 0;
        }
        rall(u, tree) {
            set<int> tot;
            rall(v, tree) {
                if(u != v) {
                    tot.insert(v);
                }
            }
            rep(v, 1, n) {
                if(v == u) continue;
                if(st.find(v) == st.end()) {
                    if(p[u][v] != 0) {
                        return 0;
                    }
                } else if(tot.find(v) != tot.end()) {
                    if(p[u][v] != 1) {
                        return 0;
                    }
                } else if(p[u][v] != 2) {
                    return 0;
                }
            }
        }
        int root = tree[0];
        if(pr > 0) {
            b[root][pr] = 1;
            b[pr][root] = 1;
        } else {
            fi = root;
        }
        rall(x, tree) {
            if(x != root) {
                b[root][x] = 1;
                b[x][root] = 1;
            }
        }
        pr = root;
    }
    if(sz(trees) > 2) {
        b[pr][fi] = 1;
        b[fi][pr] = 1;
    } else {
        rall(u, comp) {
            rall(v, comp) {
                if(p[u][v] != 1) {
                    return 0;
                }
            }
        }
    }
    return 1;
}

int solve45() {
    if(!buildDSU()) {
        return 0;
    }
    vector<vector<int> > gps(n + 1);
    rep(u, 1, n) {
        int r = find(u);
        gps[r].pb(u);
    }
    rep(u, 1, n) {
        if(gps[u].size() > 0) {
            int ret = updateComp(gps[u]);
            if(ret == 0) {
                return 0;
            }
        }
    }
    return 1;
}

int solve6() {
    return 0;
}

int construct(vector<vector<int>> in) {
    n = sz(in);
    bool task1 = true, task2 = true, task3 = true, task45 = true;
    rep(i, 1, n) {
        rep(j, 1, n) {
            p[i][j] = in[i - 1][j - 1];
            if(i != j) {
                if(p[i][j] != 1) {
                    task1 = false;
                }
                if(p[i][j] > 1) {
                    task2 = false;
                }
                if(p[i][j] != 0 && p[i][j] != 2) {
                    task3 = false;
                }
                if(p[i][j] > 2) {
                    task45 = false;
                }
            }
        }
    }
    bool ret;
    if(task1) {
        ret = solve1();
    } else if(task2) {
        ret = solve2();
    } else if(task3) {
        ret = solve3();
    } else if(task45) {
        ret = solve45();
    } else {
        ret = solve6();
    }
    if(ret) {
        vector<vector<int> > ans(n, vector<int>(n));
        rep(i, 1, n) {
            rep(j, 1, n) {
                ans[i - 1][j - 1] = b[i][j];
            }
        }
        build(ans);
        return 1;
    }
	return 0;
}

Compilation message

supertrees.cpp: In function 'int updateComp(std::vector<int>&)':
supertrees.cpp:156:24: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  156 |         if(tree.size() != pa.se) {
      |                        ^
supertrees.cpp:198:19: warning: 'first' may be used uninitialized in this function [-Wmaybe-uninitialized]
  198 |         b[fi][pr] = 1;
      |         ~~~~~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 11 ms 2924 KB Output is correct
7 Correct 240 ms 30444 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 1 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 2924 KB Output is correct
7 Correct 240 ms 30444 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 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 2156 KB Output is correct
13 Correct 249 ms 26604 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 6 ms 1516 KB Output is correct
17 Correct 122 ms 12524 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 64 ms 9836 KB Output is correct
21 Correct 256 ms 30444 KB Output is correct
22 Correct 278 ms 30312 KB Output is correct
23 Correct 270 ms 30444 KB Output is correct
24 Correct 249 ms 26880 KB Output is correct
25 Correct 108 ms 12524 KB Output is correct
26 Correct 103 ms 12524 KB Output is correct
27 Correct 270 ms 30444 KB Output is correct
28 Correct 244 ms 26732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 0 ms 364 KB Output is correct
5 Correct 0 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 2156 KB Output is correct
9 Correct 244 ms 26476 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 2924 KB Output is correct
13 Correct 259 ms 30484 KB Output is correct
14 Correct 1 ms 492 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 6 ms 1516 KB Output is correct
17 Correct 124 ms 12652 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 0 ms 364 KB Output is correct
20 Correct 0 ms 364 KB Output is correct
21 Correct 64 ms 9964 KB Output is correct
22 Correct 253 ms 30572 KB Output is correct
23 Correct 262 ms 30828 KB Output is correct
24 Correct 268 ms 30572 KB Output is correct
25 Correct 104 ms 12652 KB Output is correct
26 Correct 107 ms 12652 KB Output is correct
27 Correct 246 ms 30700 KB Output is correct
28 Correct 266 ms 30444 KB Output is correct
29 Correct 104 ms 12652 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 1 ms 380 KB Output is correct
4 Correct 66 ms 9836 KB Output is correct
5 Correct 253 ms 30444 KB Output is correct
6 Correct 251 ms 30312 KB Output is correct
7 Correct 279 ms 30444 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 64 ms 9964 KB Output is correct
10 Correct 253 ms 30572 KB Output is correct
11 Correct 246 ms 30700 KB Output is correct
12 Correct 267 ms 30572 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 66 ms 9996 KB Output is correct
17 Correct 256 ms 30700 KB Output is correct
18 Correct 276 ms 30316 KB Output is correct
19 Correct 267 ms 30444 KB Output is correct
20 Correct 250 ms 26732 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 1 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 2924 KB Output is correct
7 Correct 240 ms 30444 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 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 2156 KB Output is correct
13 Correct 249 ms 26604 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 6 ms 1516 KB Output is correct
17 Correct 122 ms 12524 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 64 ms 9836 KB Output is correct
21 Correct 256 ms 30444 KB Output is correct
22 Correct 278 ms 30312 KB Output is correct
23 Correct 270 ms 30444 KB Output is correct
24 Correct 249 ms 26880 KB Output is correct
25 Correct 108 ms 12524 KB Output is correct
26 Correct 103 ms 12524 KB Output is correct
27 Correct 270 ms 30444 KB Output is correct
28 Correct 244 ms 26732 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 0 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 0 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 2156 KB Output is correct
37 Correct 244 ms 26476 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 2924 KB Output is correct
41 Correct 259 ms 30484 KB Output is correct
42 Correct 1 ms 492 KB Output is correct
43 Correct 0 ms 364 KB Output is correct
44 Correct 6 ms 1516 KB Output is correct
45 Correct 124 ms 12652 KB Output is correct
46 Correct 1 ms 364 KB Output is correct
47 Correct 0 ms 364 KB Output is correct
48 Correct 0 ms 364 KB Output is correct
49 Correct 64 ms 9964 KB Output is correct
50 Correct 253 ms 30572 KB Output is correct
51 Correct 262 ms 30828 KB Output is correct
52 Correct 268 ms 30572 KB Output is correct
53 Correct 104 ms 12652 KB Output is correct
54 Correct 107 ms 12652 KB Output is correct
55 Correct 246 ms 30700 KB Output is correct
56 Correct 266 ms 30444 KB Output is correct
57 Correct 104 ms 12652 KB Output is correct
58 Correct 1 ms 364 KB Output is correct
59 Correct 1 ms 364 KB Output is correct
60 Correct 6 ms 1516 KB Output is correct
61 Correct 129 ms 12780 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 9964 KB Output is correct
66 Correct 114 ms 13420 KB Output is correct
67 Correct 128 ms 14700 KB Output is correct
68 Correct 108 ms 13164 KB Output is correct
69 Correct 109 ms 12908 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 1 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 2924 KB Output is correct
7 Correct 240 ms 30444 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 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 2156 KB Output is correct
13 Correct 249 ms 26604 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 6 ms 1516 KB Output is correct
17 Correct 122 ms 12524 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 64 ms 9836 KB Output is correct
21 Correct 256 ms 30444 KB Output is correct
22 Correct 278 ms 30312 KB Output is correct
23 Correct 270 ms 30444 KB Output is correct
24 Correct 249 ms 26880 KB Output is correct
25 Correct 108 ms 12524 KB Output is correct
26 Correct 103 ms 12524 KB Output is correct
27 Correct 270 ms 30444 KB Output is correct
28 Correct 244 ms 26732 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 0 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 0 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 2156 KB Output is correct
37 Correct 244 ms 26476 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 2924 KB Output is correct
41 Correct 259 ms 30484 KB Output is correct
42 Correct 1 ms 492 KB Output is correct
43 Correct 0 ms 364 KB Output is correct
44 Correct 6 ms 1516 KB Output is correct
45 Correct 124 ms 12652 KB Output is correct
46 Correct 1 ms 364 KB Output is correct
47 Correct 0 ms 364 KB Output is correct
48 Correct 0 ms 364 KB Output is correct
49 Correct 64 ms 9964 KB Output is correct
50 Correct 253 ms 30572 KB Output is correct
51 Correct 262 ms 30828 KB Output is correct
52 Correct 268 ms 30572 KB Output is correct
53 Correct 104 ms 12652 KB Output is correct
54 Correct 107 ms 12652 KB Output is correct
55 Correct 246 ms 30700 KB Output is correct
56 Correct 266 ms 30444 KB Output is correct
57 Correct 104 ms 12652 KB Output is correct
58 Correct 0 ms 364 KB Output is correct
59 Correct 1 ms 364 KB Output is correct
60 Correct 1 ms 380 KB Output is correct
61 Correct 66 ms 9836 KB Output is correct
62 Correct 253 ms 30444 KB Output is correct
63 Correct 251 ms 30312 KB Output is correct
64 Correct 279 ms 30444 KB Output is correct
65 Correct 1 ms 364 KB Output is correct
66 Correct 64 ms 9964 KB Output is correct
67 Correct 253 ms 30572 KB Output is correct
68 Correct 246 ms 30700 KB Output is correct
69 Correct 267 ms 30572 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 66 ms 9996 KB Output is correct
74 Correct 256 ms 30700 KB Output is correct
75 Correct 276 ms 30316 KB Output is correct
76 Correct 267 ms 30444 KB Output is correct
77 Correct 250 ms 26732 KB Output is correct
78 Correct 1 ms 364 KB Output is correct
79 Correct 1 ms 364 KB Output is correct
80 Correct 6 ms 1516 KB Output is correct
81 Correct 129 ms 12780 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 9964 KB Output is correct
86 Correct 114 ms 13420 KB Output is correct
87 Correct 128 ms 14700 KB Output is correct
88 Correct 108 ms 13164 KB Output is correct
89 Correct 109 ms 12908 KB Output is correct
90 Correct 1 ms 364 KB Output is correct
91 Correct 1 ms 364 KB Output is correct
92 Correct 6 ms 1644 KB Output is correct
93 Correct 115 ms 14572 KB Output is correct
94 Correct 1 ms 364 KB Output is correct
95 Correct 1 ms 364 KB Output is correct
96 Correct 0 ms 364 KB Output is correct
97 Correct 27 ms 4972 KB Output is correct
98 Correct 105 ms 14572 KB Output is correct
99 Correct 107 ms 14572 KB Output is correct
100 Correct 106 ms 14572 KB Output is correct
101 Correct 105 ms 14572 KB Output is correct
102 Correct 102 ms 14572 KB Output is correct
103 Correct 103 ms 14572 KB Output is correct
104 Correct 108 ms 14572 KB Output is correct
105 Correct 116 ms 14572 KB Output is correct