Submission #1109892

# Submission time Handle Problem Language Result Execution time Memory
1109892 2024-11-08T02:52:30 Z TrinhKhanhDung Connecting Supertrees (IOI20_supertrees) C++14
100 / 100
157 ms 27984 KB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)
 #include "supertrees.h"

using namespace std;

template<class T1, class T2>
    bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}

template<class T1, class T2>
    bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}

template<class T1, class T2>
    void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}

template<class T1, class T2>
    void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}

template<class T>
    void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}

struct DSU{
    vector<int> par, Sz;

    DSU(int n = 0){
        par.resize(n + 3, 0);
        Sz.resize(n + 3, 1);
        iota(ALL(par), 0);
    }

    int root(int u){
        return u == par[u] ? u : par[u] = root(par[u]);
    }

    bool join(int a, int b){
        a = root(a);
        b = root(b);
        if(a == b) return false;
        if(Sz[a] < Sz[b]) swap(a, b);
        par[b] = a;
        Sz[a] += Sz[b];
        return true;
    }
};

const int MAX = 1003;

int N;
int f[MAX][MAX], cnt[MAX];
bool visited[MAX];
vector<int> save;

void dfs1(int u){
    if(visited[u]) return;
    visited[u] = true;
    save.push_back(u);
    for(int v = 0; v < N; v++) if(f[u][v] == 1){
        dfs1(v);
    }
}

void dfs2(int u){
    if(visited[u]) return;
    visited[u] = true;
    save.push_back(u);
    for(int v = 0; v < N; v++) if(f[u][v] != 0){
        dfs2(v);
    }
}

int construct(vector<vector<int>> p){
    N = sz(p);
    for(int i = 0; i < N; i++){
        for(int j = 0; j < N; j++){
            f[i][j] = p[i][j];
            if(p[i][j] == 3) return 0;
            if(i != j && p[i][j] == 1){
                cnt[i]++;
            }
        }
    }

    vector<vector<int>> ans(N, vector<int>(N, false));
    DSU dsu(N);

    for(int i = 0; i < N; i++) if(!visited[i]){
        save.clear();
        dfs1(i);

        int sum = 0;
        for(int x: save) sum += cnt[x];
        if(sz(save) * (sz(save) - 1) != sum){
            return 0;
        }

        for(int i = 1; i < sz(save); i++){
            dsu.join(save[i], save[0]);
            ans[save[i]][save[0]] = ans[save[0]][save[i]] = true;
        }

        for(int i = 0; i < sz(save); i++){
            if(f[i][save[0]] == 2){
                for(int x: save){
                    if(f[x][i] != 2) return 0;
                }
            }
        }
    }

    memset(visited, false, sizeof visited);
    for(int i = 0; i < N; i++) if(!visited[i]){
        save.clear();
        dfs2(i);

        sort(ALL(save), [&](int u, int v){return dsu.root(u) < dsu.root(v);});
        for(int j = 0; j < sz(save); j++){
            for(int k = j + 1; k < sz(save); k++) if(dsu.root(save[j]) != dsu.root(save[k])){
                if(f[save[j]][save[k]] != 2) return 0;
            }
        }

        vector<int> v;
        for(int x: save){
            v.push_back(dsu.root(x));
        }
        cps(v);
        if(sz(v) == 2){
            return 0;
        }
        if(sz(v) == 1) continue;
        for(int j = 1; j < sz(v); j++){
            ans[v[j - 1]][v[j]] = ans[v[j]][v[j - 1]] = true;
        }
        ans[v[0]][v.back()] = ans[v.back()][v[0]] = true;
    }

    build(ans);

    return 1;
}

//int main(){
//    ios_base::sync_with_stdio(0); cin.tie(0);
//    // freopen("ADVERT.inp","r",stdin);
//    // freopen("ADVERT.out","w",stdout);
//
//     cout << construct({{1, 1, 2, 2}, {1, 1, 2, 2}, {2, 2, 1, 2}, {2, 2, 2, 1}});
//
//    return 0;
//}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 508 KB Output is correct
6 Correct 6 ms 3408 KB Output is correct
7 Correct 127 ms 26144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 508 KB Output is correct
6 Correct 6 ms 3408 KB Output is correct
7 Correct 127 ms 26144 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 508 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 6 ms 3408 KB Output is correct
13 Correct 125 ms 26156 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 504 KB Output is correct
16 Correct 4 ms 2908 KB Output is correct
17 Correct 75 ms 16160 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 38 ms 7936 KB Output is correct
21 Correct 133 ms 25928 KB Output is correct
22 Correct 130 ms 25928 KB Output is correct
23 Correct 157 ms 26144 KB Output is correct
24 Correct 125 ms 25928 KB Output is correct
25 Correct 57 ms 16160 KB Output is correct
26 Correct 53 ms 16208 KB Output is correct
27 Correct 136 ms 25928 KB Output is correct
28 Correct 129 ms 25928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 504 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 6 ms 3536 KB Output is correct
9 Correct 144 ms 25928 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 7 ms 3408 KB Output is correct
13 Correct 132 ms 26148 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 5 ms 3324 KB Output is correct
17 Correct 73 ms 17992 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 448 KB Output is correct
21 Correct 34 ms 8584 KB Output is correct
22 Correct 140 ms 27984 KB Output is correct
23 Correct 132 ms 27976 KB Output is correct
24 Correct 137 ms 27976 KB Output is correct
25 Correct 58 ms 17944 KB Output is correct
26 Correct 59 ms 17992 KB Output is correct
27 Correct 128 ms 27920 KB Output is correct
28 Correct 137 ms 27980 KB Output is correct
29 Correct 61 ms 17992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 32 ms 8120 KB Output is correct
5 Correct 127 ms 25928 KB Output is correct
6 Correct 137 ms 25928 KB Output is correct
7 Correct 149 ms 25932 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 31 ms 8088 KB Output is correct
10 Correct 136 ms 25928 KB Output is correct
11 Correct 122 ms 25928 KB Output is correct
12 Correct 138 ms 26144 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 40 ms 7980 KB Output is correct
17 Correct 129 ms 26152 KB Output is correct
18 Correct 145 ms 25928 KB Output is correct
19 Correct 120 ms 25928 KB Output is correct
20 Correct 122 ms 25888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 508 KB Output is correct
6 Correct 6 ms 3408 KB Output is correct
7 Correct 127 ms 26144 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 508 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 6 ms 3408 KB Output is correct
13 Correct 125 ms 26156 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 504 KB Output is correct
16 Correct 4 ms 2908 KB Output is correct
17 Correct 75 ms 16160 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 38 ms 7936 KB Output is correct
21 Correct 133 ms 25928 KB Output is correct
22 Correct 130 ms 25928 KB Output is correct
23 Correct 157 ms 26144 KB Output is correct
24 Correct 125 ms 25928 KB Output is correct
25 Correct 57 ms 16160 KB Output is correct
26 Correct 53 ms 16208 KB Output is correct
27 Correct 136 ms 25928 KB Output is correct
28 Correct 129 ms 25928 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 336 KB Output is correct
32 Correct 1 ms 504 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 6 ms 3536 KB Output is correct
37 Correct 144 ms 25928 KB Output is correct
38 Correct 1 ms 336 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 7 ms 3408 KB Output is correct
41 Correct 132 ms 26148 KB Output is correct
42 Correct 1 ms 336 KB Output is correct
43 Correct 1 ms 336 KB Output is correct
44 Correct 5 ms 3324 KB Output is correct
45 Correct 73 ms 17992 KB Output is correct
46 Correct 1 ms 336 KB Output is correct
47 Correct 1 ms 336 KB Output is correct
48 Correct 1 ms 448 KB Output is correct
49 Correct 34 ms 8584 KB Output is correct
50 Correct 140 ms 27984 KB Output is correct
51 Correct 132 ms 27976 KB Output is correct
52 Correct 137 ms 27976 KB Output is correct
53 Correct 58 ms 17944 KB Output is correct
54 Correct 59 ms 17992 KB Output is correct
55 Correct 128 ms 27920 KB Output is correct
56 Correct 137 ms 27980 KB Output is correct
57 Correct 61 ms 17992 KB Output is correct
58 Correct 1 ms 504 KB Output is correct
59 Correct 1 ms 336 KB Output is correct
60 Correct 4 ms 3152 KB Output is correct
61 Correct 73 ms 18188 KB Output is correct
62 Correct 1 ms 336 KB Output is correct
63 Correct 1 ms 336 KB Output is correct
64 Correct 1 ms 336 KB Output is correct
65 Correct 35 ms 8400 KB Output is correct
66 Correct 67 ms 17992 KB Output is correct
67 Correct 60 ms 18056 KB Output is correct
68 Correct 62 ms 17940 KB Output is correct
69 Correct 62 ms 17992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 508 KB Output is correct
6 Correct 6 ms 3408 KB Output is correct
7 Correct 127 ms 26144 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 508 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 6 ms 3408 KB Output is correct
13 Correct 125 ms 26156 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 504 KB Output is correct
16 Correct 4 ms 2908 KB Output is correct
17 Correct 75 ms 16160 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 38 ms 7936 KB Output is correct
21 Correct 133 ms 25928 KB Output is correct
22 Correct 130 ms 25928 KB Output is correct
23 Correct 157 ms 26144 KB Output is correct
24 Correct 125 ms 25928 KB Output is correct
25 Correct 57 ms 16160 KB Output is correct
26 Correct 53 ms 16208 KB Output is correct
27 Correct 136 ms 25928 KB Output is correct
28 Correct 129 ms 25928 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 336 KB Output is correct
32 Correct 1 ms 504 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 6 ms 3536 KB Output is correct
37 Correct 144 ms 25928 KB Output is correct
38 Correct 1 ms 336 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 7 ms 3408 KB Output is correct
41 Correct 132 ms 26148 KB Output is correct
42 Correct 1 ms 336 KB Output is correct
43 Correct 1 ms 336 KB Output is correct
44 Correct 5 ms 3324 KB Output is correct
45 Correct 73 ms 17992 KB Output is correct
46 Correct 1 ms 336 KB Output is correct
47 Correct 1 ms 336 KB Output is correct
48 Correct 1 ms 448 KB Output is correct
49 Correct 34 ms 8584 KB Output is correct
50 Correct 140 ms 27984 KB Output is correct
51 Correct 132 ms 27976 KB Output is correct
52 Correct 137 ms 27976 KB Output is correct
53 Correct 58 ms 17944 KB Output is correct
54 Correct 59 ms 17992 KB Output is correct
55 Correct 128 ms 27920 KB Output is correct
56 Correct 137 ms 27980 KB Output is correct
57 Correct 61 ms 17992 KB Output is correct
58 Correct 1 ms 336 KB Output is correct
59 Correct 1 ms 336 KB Output is correct
60 Correct 1 ms 336 KB Output is correct
61 Correct 32 ms 8120 KB Output is correct
62 Correct 127 ms 25928 KB Output is correct
63 Correct 137 ms 25928 KB Output is correct
64 Correct 149 ms 25932 KB Output is correct
65 Correct 1 ms 336 KB Output is correct
66 Correct 31 ms 8088 KB Output is correct
67 Correct 136 ms 25928 KB Output is correct
68 Correct 122 ms 25928 KB Output is correct
69 Correct 138 ms 26144 KB Output is correct
70 Correct 1 ms 336 KB Output is correct
71 Correct 1 ms 336 KB Output is correct
72 Correct 1 ms 336 KB Output is correct
73 Correct 40 ms 7980 KB Output is correct
74 Correct 129 ms 26152 KB Output is correct
75 Correct 145 ms 25928 KB Output is correct
76 Correct 120 ms 25928 KB Output is correct
77 Correct 122 ms 25888 KB Output is correct
78 Correct 1 ms 504 KB Output is correct
79 Correct 1 ms 336 KB Output is correct
80 Correct 4 ms 3152 KB Output is correct
81 Correct 73 ms 18188 KB Output is correct
82 Correct 1 ms 336 KB Output is correct
83 Correct 1 ms 336 KB Output is correct
84 Correct 1 ms 336 KB Output is correct
85 Correct 35 ms 8400 KB Output is correct
86 Correct 67 ms 17992 KB Output is correct
87 Correct 60 ms 18056 KB Output is correct
88 Correct 62 ms 17940 KB Output is correct
89 Correct 62 ms 17992 KB Output is correct
90 Correct 1 ms 336 KB Output is correct
91 Correct 1 ms 336 KB Output is correct
92 Correct 3 ms 688 KB Output is correct
93 Correct 60 ms 10232 KB Output is correct
94 Correct 1 ms 336 KB Output is correct
95 Correct 1 ms 336 KB Output is correct
96 Correct 1 ms 336 KB Output is correct
97 Correct 15 ms 2896 KB Output is correct
98 Correct 54 ms 12436 KB Output is correct
99 Correct 55 ms 12432 KB Output is correct
100 Correct 58 ms 12436 KB Output is correct
101 Correct 63 ms 12432 KB Output is correct
102 Correct 53 ms 10056 KB Output is correct
103 Correct 52 ms 10056 KB Output is correct
104 Correct 73 ms 10252 KB Output is correct
105 Correct 62 ms 10128 KB Output is correct