Submission #1109882

# Submission time Handle Problem Language Result Execution time Memory
1109882 2024-11-08T02:27:44 Z TrinhKhanhDung Connecting Supertrees (IOI20_supertrees) C++14
56 / 100
155 ms 28364 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);
        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 504 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 508 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 8 ms 3500 KB Output is correct
7 Correct 148 ms 27976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 504 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 508 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 8 ms 3500 KB Output is correct
7 Correct 148 ms 27976 KB Output is correct
8 Correct 1 ms 504 KB Output is correct
9 Correct 1 ms 336 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 131 ms 27976 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 3152 KB Output is correct
17 Correct 80 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 46 ms 8608 KB Output is correct
21 Correct 135 ms 27976 KB Output is correct
22 Correct 134 ms 27976 KB Output is correct
23 Correct 146 ms 27916 KB Output is correct
24 Correct 119 ms 27912 KB Output is correct
25 Correct 57 ms 18068 KB Output is correct
26 Correct 55 ms 18076 KB Output is correct
27 Correct 142 ms 27976 KB Output is correct
28 Correct 120 ms 27920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 504 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 508 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 7 ms 3408 KB Output is correct
9 Correct 132 ms 27976 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 151 ms 28364 KB Output is correct
14 Incorrect 1 ms 336 KB Answer gives possible 1 while actual possible 0
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 448 KB Output is correct
3 Correct 1 ms 504 KB Output is correct
4 Correct 37 ms 8452 KB Output is correct
5 Correct 134 ms 28052 KB Output is correct
6 Correct 134 ms 27924 KB Output is correct
7 Correct 155 ms 28056 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 34 ms 8628 KB Output is correct
10 Correct 138 ms 27976 KB Output is correct
11 Correct 138 ms 27976 KB Output is correct
12 Correct 146 ms 27908 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 35 ms 8460 KB Output is correct
17 Correct 131 ms 28048 KB Output is correct
18 Correct 122 ms 27976 KB Output is correct
19 Correct 122 ms 27980 KB Output is correct
20 Correct 131 ms 27976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 504 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 508 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 8 ms 3500 KB Output is correct
7 Correct 148 ms 27976 KB Output is correct
8 Correct 1 ms 504 KB Output is correct
9 Correct 1 ms 336 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 131 ms 27976 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 3152 KB Output is correct
17 Correct 80 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 46 ms 8608 KB Output is correct
21 Correct 135 ms 27976 KB Output is correct
22 Correct 134 ms 27976 KB Output is correct
23 Correct 146 ms 27916 KB Output is correct
24 Correct 119 ms 27912 KB Output is correct
25 Correct 57 ms 18068 KB Output is correct
26 Correct 55 ms 18076 KB Output is correct
27 Correct 142 ms 27976 KB Output is correct
28 Correct 120 ms 27920 KB Output is correct
29 Correct 1 ms 504 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 508 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 7 ms 3408 KB Output is correct
37 Correct 132 ms 27976 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 151 ms 28364 KB Output is correct
42 Incorrect 1 ms 336 KB Answer gives possible 1 while actual possible 0
43 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 504 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 508 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 8 ms 3500 KB Output is correct
7 Correct 148 ms 27976 KB Output is correct
8 Correct 1 ms 504 KB Output is correct
9 Correct 1 ms 336 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 131 ms 27976 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 3152 KB Output is correct
17 Correct 80 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 46 ms 8608 KB Output is correct
21 Correct 135 ms 27976 KB Output is correct
22 Correct 134 ms 27976 KB Output is correct
23 Correct 146 ms 27916 KB Output is correct
24 Correct 119 ms 27912 KB Output is correct
25 Correct 57 ms 18068 KB Output is correct
26 Correct 55 ms 18076 KB Output is correct
27 Correct 142 ms 27976 KB Output is correct
28 Correct 120 ms 27920 KB Output is correct
29 Correct 1 ms 504 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 508 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 7 ms 3408 KB Output is correct
37 Correct 132 ms 27976 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 151 ms 28364 KB Output is correct
42 Incorrect 1 ms 336 KB Answer gives possible 1 while actual possible 0
43 Halted 0 ms 0 KB -