Submission #434573

# Submission time Handle Problem Language Result Execution time Memory
434573 2021-06-21T12:14:15 Z yuto1115 Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
269 ms 22148 KB
#include "supertrees.h"
#include <bits/stdc++.h>
#define rep(i, n) for(ll i = 0; i < ll(n); i++)
#define rep2(i, s, n) for(ll i = ll(s); i < ll(n); i++)
#define rrep(i, n) for(ll i = ll(n)-1; i >= 0; i--)
#define pb push_back
#define eb emplace_back
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vp = vector<P>;
using vvp = vector<vp>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vs = vector<string>;
const int inf = 1001001001;
const ll linf = 1001001001001001001;

template<class T>
bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

class unionfind {
    vi p;
public:
    unionfind(int n) : p(n, -1) {}
    
    int root(int x) {
        if (p[x] < 0) return x;
        return p[x] = root(p[x]);
    }
    
    bool same(int x, int y) { return root(x) == root(y); }
    
    void merge(int x, int y) {
        x = root(x), y = root(y);
        if (x == y) return;
        if (p[x] > p[y]) swap(x, y);
        p[x] += p[y];
        p[y] = x;
    }
};

int construct(vvi p) {
    int n = p.size();
    unionfind uf(n);
    rep(i, n) rep2(j, i + 1, n) {
            if (p[i][j] == 3) return 0;
            if (p[i][j]) uf.merge(i, j);
        }
    rep(i, n) rep2(j, i + 1, n) {
            if (!p[i][j] and uf.same(i, j)) return 0;
        }
    map<int, vi> mp;
    rep(i, n) mp[uf.root(i)].pb(i);
    vvi ans(n, vi(n));
    for (auto[_, ls] : mp) {
        int sz = ls.size();
        uf = unionfind(sz);
        rep(i, sz) rep2(j, i + 1, sz) {
                if (p[ls[i]][ls[j]] == 1) uf.merge(i, j);
            }
        rep(i, sz) rep2(j, i + 1, sz) {
                if (p[ls[i]][ls[j]] == 2 and uf.same(i, j)) return 0;
            }
        vi rs;
        rep(i, sz) {
            int r = uf.root(i);
            if (r == i) {
                rs.pb(r);
            } else {
                ans[ls[r]][ls[i]] = 1;
                ans[ls[i]][ls[r]] = 1;
            }
        }
        if (rs.size() >= 2) {
            if(rs.size() == 2) return 0;
            rep(i, rs.size() - 1) {
                ans[ls[rs[i]]][ls[rs[i + 1]]] = 1;
                ans[ls[rs[i + 1]]][ls[rs[i]]] = 1;
            }
            ans[ls[rs[0]]][ls[rs.back()]] = 1;
            ans[ls[rs.back()]][ls[rs[0]]] = 1;
        }
    }
    build(ans);
    return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 10 ms 1140 KB Output is correct
7 Correct 269 ms 22012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 10 ms 1140 KB Output is correct
7 Correct 269 ms 22012 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 12 ms 1088 KB Output is correct
13 Correct 230 ms 22112 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 588 KB Output is correct
17 Correct 107 ms 8056 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 58 ms 5716 KB Output is correct
21 Correct 228 ms 22012 KB Output is correct
22 Correct 228 ms 22088 KB Output is correct
23 Correct 246 ms 22016 KB Output is correct
24 Correct 236 ms 22068 KB Output is correct
25 Correct 100 ms 8072 KB Output is correct
26 Correct 95 ms 8072 KB Output is correct
27 Correct 241 ms 21956 KB Output is correct
28 Correct 260 ms 22140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 13 ms 1196 KB Output is correct
9 Correct 225 ms 22148 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 13 ms 1100 KB Output is correct
13 Correct 235 ms 22112 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 588 KB Output is correct
17 Correct 110 ms 8068 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 57 ms 5748 KB Output is correct
22 Correct 242 ms 22008 KB Output is correct
23 Correct 244 ms 21960 KB Output is correct
24 Correct 234 ms 21956 KB Output is correct
25 Correct 103 ms 12164 KB Output is correct
26 Correct 94 ms 8068 KB Output is correct
27 Correct 224 ms 22012 KB Output is correct
28 Correct 241 ms 22024 KB Output is correct
29 Correct 99 ms 12228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 71 ms 5700 KB Output is correct
5 Correct 257 ms 21964 KB Output is correct
6 Correct 230 ms 22028 KB Output is correct
7 Correct 266 ms 22024 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 58 ms 5688 KB Output is correct
10 Correct 232 ms 22148 KB Output is correct
11 Correct 230 ms 22024 KB Output is correct
12 Correct 242 ms 21940 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 57 ms 5752 KB Output is correct
17 Correct 233 ms 22012 KB Output is correct
18 Correct 228 ms 22024 KB Output is correct
19 Correct 249 ms 22028 KB Output is correct
20 Correct 231 ms 22124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 10 ms 1140 KB Output is correct
7 Correct 269 ms 22012 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 12 ms 1088 KB Output is correct
13 Correct 230 ms 22112 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 588 KB Output is correct
17 Correct 107 ms 8056 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 58 ms 5716 KB Output is correct
21 Correct 228 ms 22012 KB Output is correct
22 Correct 228 ms 22088 KB Output is correct
23 Correct 246 ms 22016 KB Output is correct
24 Correct 236 ms 22068 KB Output is correct
25 Correct 100 ms 8072 KB Output is correct
26 Correct 95 ms 8072 KB Output is correct
27 Correct 241 ms 21956 KB Output is correct
28 Correct 260 ms 22140 KB Output is correct
29 Correct 0 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 13 ms 1196 KB Output is correct
37 Correct 225 ms 22148 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 13 ms 1100 KB Output is correct
41 Correct 235 ms 22112 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 5 ms 588 KB Output is correct
45 Correct 110 ms 8068 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 57 ms 5748 KB Output is correct
50 Correct 242 ms 22008 KB Output is correct
51 Correct 244 ms 21960 KB Output is correct
52 Correct 234 ms 21956 KB Output is correct
53 Correct 103 ms 12164 KB Output is correct
54 Correct 94 ms 8068 KB Output is correct
55 Correct 224 ms 22012 KB Output is correct
56 Correct 241 ms 22024 KB Output is correct
57 Correct 99 ms 12228 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 4 ms 588 KB Output is correct
61 Correct 101 ms 8056 KB Output is correct
62 Correct 1 ms 204 KB Output is correct
63 Correct 1 ms 204 KB Output is correct
64 Correct 0 ms 204 KB Output is correct
65 Correct 57 ms 5756 KB Output is correct
66 Correct 118 ms 8064 KB Output is correct
67 Correct 99 ms 8064 KB Output is correct
68 Correct 112 ms 8068 KB Output is correct
69 Correct 103 ms 12156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 10 ms 1140 KB Output is correct
7 Correct 269 ms 22012 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 12 ms 1088 KB Output is correct
13 Correct 230 ms 22112 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 588 KB Output is correct
17 Correct 107 ms 8056 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 58 ms 5716 KB Output is correct
21 Correct 228 ms 22012 KB Output is correct
22 Correct 228 ms 22088 KB Output is correct
23 Correct 246 ms 22016 KB Output is correct
24 Correct 236 ms 22068 KB Output is correct
25 Correct 100 ms 8072 KB Output is correct
26 Correct 95 ms 8072 KB Output is correct
27 Correct 241 ms 21956 KB Output is correct
28 Correct 260 ms 22140 KB Output is correct
29 Correct 0 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 13 ms 1196 KB Output is correct
37 Correct 225 ms 22148 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 13 ms 1100 KB Output is correct
41 Correct 235 ms 22112 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 5 ms 588 KB Output is correct
45 Correct 110 ms 8068 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 57 ms 5748 KB Output is correct
50 Correct 242 ms 22008 KB Output is correct
51 Correct 244 ms 21960 KB Output is correct
52 Correct 234 ms 21956 KB Output is correct
53 Correct 103 ms 12164 KB Output is correct
54 Correct 94 ms 8068 KB Output is correct
55 Correct 224 ms 22012 KB Output is correct
56 Correct 241 ms 22024 KB Output is correct
57 Correct 99 ms 12228 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 71 ms 5700 KB Output is correct
62 Correct 257 ms 21964 KB Output is correct
63 Correct 230 ms 22028 KB Output is correct
64 Correct 266 ms 22024 KB Output is correct
65 Correct 1 ms 204 KB Output is correct
66 Correct 58 ms 5688 KB Output is correct
67 Correct 232 ms 22148 KB Output is correct
68 Correct 230 ms 22024 KB Output is correct
69 Correct 242 ms 21940 KB Output is correct
70 Correct 1 ms 204 KB Output is correct
71 Correct 1 ms 204 KB Output is correct
72 Correct 1 ms 204 KB Output is correct
73 Correct 57 ms 5752 KB Output is correct
74 Correct 233 ms 22012 KB Output is correct
75 Correct 228 ms 22024 KB Output is correct
76 Correct 249 ms 22028 KB Output is correct
77 Correct 231 ms 22124 KB Output is correct
78 Correct 1 ms 204 KB Output is correct
79 Correct 1 ms 204 KB Output is correct
80 Correct 4 ms 588 KB Output is correct
81 Correct 101 ms 8056 KB Output is correct
82 Correct 1 ms 204 KB Output is correct
83 Correct 1 ms 204 KB Output is correct
84 Correct 0 ms 204 KB Output is correct
85 Correct 57 ms 5756 KB Output is correct
86 Correct 118 ms 8064 KB Output is correct
87 Correct 99 ms 8064 KB Output is correct
88 Correct 112 ms 8068 KB Output is correct
89 Correct 103 ms 12156 KB Output is correct
90 Correct 0 ms 204 KB Output is correct
91 Correct 1 ms 204 KB Output is correct
92 Correct 4 ms 588 KB Output is correct
93 Correct 100 ms 8060 KB Output is correct
94 Correct 0 ms 204 KB Output is correct
95 Correct 0 ms 204 KB Output is correct
96 Correct 0 ms 204 KB Output is correct
97 Correct 24 ms 2176 KB Output is correct
98 Correct 97 ms 8060 KB Output is correct
99 Correct 92 ms 8132 KB Output is correct
100 Correct 95 ms 8072 KB Output is correct
101 Correct 93 ms 8060 KB Output is correct
102 Correct 92 ms 8056 KB Output is correct
103 Correct 123 ms 10092 KB Output is correct
104 Correct 119 ms 10092 KB Output is correct
105 Correct 100 ms 10052 KB Output is correct