Submission #768783

# Submission time Handle Problem Language Result Execution time Memory
768783 2023-06-28T15:19:32 Z AlesL0 Connecting Supertrees (IOI20_supertrees) C++17
56 / 100
179 ms 22476 KB
#include "supertrees.h"
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

vector <ll> P, H;
vector <vector <int>> sol;

ll dsu_find(ll x){
    if (P[x] == x)return x;
    return (P[x] = dsu_find(P[x]));
}

void dsu_union(ll x, ll y){
    x = dsu_find(x), y = dsu_find(y);
    if (x == y)return;
    if (H[x] > H[y])P[y] = x;
    if (H[x] < H[y])P[x] = y;
    if (H[x] == H[y]){
        P[y] = x;
        H[x]++;
    }
}

bool cmp(ll a, ll b){
    return (P[a] < P[b]);
}

bool solve(vector <ll> &v, vector <vector <int>> &p){
    ll n = v.size();
    for (int i = 0; i < n; i++){
        for (int j = i+1; j < n; j++)if (p[v[i]][v[j]] == 1)dsu_union(v[i], v[j]);
    }
    for (int i = 0; i < n; i++)P[v[i]] = dsu_find(v[i]);
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++)if (p[v[i]][v[j]] != 1 && P[v[i]] == P[v[j]])return 0;
    }
    sort(v.begin(), v.end(), cmp);
    ll current = 0;
    for (int i = 1; i < n; i++){
        if (P[v[i]] == P[v[i-1]]){
            sol[v[i]][v[i-1]] = 1;
            sol[v[i-1]][v[i]] = 1;
        }
        else {
            sol[v[i]][v[current]] = 1;
            sol[v[current]][v[i]] = 1;
            current = i;
        }
    }
    sol[v[current]][v[0]] = 1;
    sol[v[0]][v[current]] = 1;
    return 1;
}

/*void build(vector <vector <int>> b){
    /*for (int i = 0; i < b.size(); i++){
        cerr << endl;
        for (int j = 0; j < b.size(); j++){
            cerr << b[i][j] << " ";
        }
    }
    return;
}*/

int construct(vector <vector <int>> p){
    ll n = p.size();
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++)if (p[i][j] == 3)return 0;
    }
    P.resize(n);
    H.resize(n, 0);
    for (int i = 0; i < n; i++)P[i] = i;
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++)if (p[i][j] > 0)dsu_union(i, j);
    }
    for (int i = 0; i < n; i++)P[i] = dsu_find(i);
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++)if (p[i][j] == 0 && P[i] == P[j])return 0;
    }
    vector <pair<ll, ll>> t(n);
    for (int i = 0; i < n; i++)t[i] = {P[i], i};
    sort(t.begin(), t.end());
    sol.resize(n, vector <int> (n, 0));
    for (int i = 0; i < n; i++){
        P[i] = i;
        H[i] = 0;
    }
    vector <ll> v;
    for (int i = 0; i < n; i++){
        if (i > 0 && t[i].first != t[i-1].first){
            if (!solve(v, p))return 0;
            v.clear();
        }
        v.push_back(t[i].second);
    }
    if (!solve(v, p))return 0;
    for (int i = 0; i < n; i++)sol[i][i] = 0;
    build(sol);
    return 1;
}

/*int main(){
    int n; cin >> n;
    vector <vector <int>> p(n, vector <int> (n));
    for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)cin >> p[i][j];
    cout << construct(p);
}*/

/*
4
1 1 2 2
1 1 2 2
2 2 1 2
2 2 2 1
*/

Compilation message

supertrees.cpp:59:5: warning: "/*" within comment [-Wcomment]
   59 |     /*for (int i = 0; i < b.size(); i++){
      |
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
6 Correct 7 ms 1204 KB Output is correct
7 Correct 166 ms 22388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
6 Correct 7 ms 1204 KB Output is correct
7 Correct 166 ms 22388 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 7 ms 1204 KB Output is correct
13 Correct 150 ms 22316 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 3 ms 596 KB Output is correct
17 Correct 74 ms 8952 KB Output is correct
18 Correct 0 ms 300 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 38 ms 6056 KB Output is correct
21 Correct 151 ms 22332 KB Output is correct
22 Correct 158 ms 22320 KB Output is correct
23 Correct 161 ms 22236 KB Output is correct
24 Correct 162 ms 22252 KB Output is correct
25 Correct 59 ms 8780 KB Output is correct
26 Correct 59 ms 8732 KB Output is correct
27 Correct 158 ms 22360 KB Output is correct
28 Correct 150 ms 22288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Incorrect 1 ms 304 KB Answer gives possible 1 while actual possible 0
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 296 KB Output is correct
4 Correct 41 ms 6052 KB Output is correct
5 Correct 157 ms 22224 KB Output is correct
6 Correct 179 ms 22272 KB Output is correct
7 Correct 162 ms 22220 KB Output is correct
8 Correct 0 ms 300 KB Output is correct
9 Correct 46 ms 5976 KB Output is correct
10 Correct 149 ms 22464 KB Output is correct
11 Correct 158 ms 22468 KB Output is correct
12 Correct 156 ms 22456 KB Output is correct
13 Correct 1 ms 300 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 38 ms 6068 KB Output is correct
17 Correct 150 ms 22468 KB Output is correct
18 Correct 152 ms 22476 KB Output is correct
19 Correct 157 ms 22448 KB Output is correct
20 Correct 148 ms 22448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
6 Correct 7 ms 1204 KB Output is correct
7 Correct 166 ms 22388 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 7 ms 1204 KB Output is correct
13 Correct 150 ms 22316 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 3 ms 596 KB Output is correct
17 Correct 74 ms 8952 KB Output is correct
18 Correct 0 ms 300 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 38 ms 6056 KB Output is correct
21 Correct 151 ms 22332 KB Output is correct
22 Correct 158 ms 22320 KB Output is correct
23 Correct 161 ms 22236 KB Output is correct
24 Correct 162 ms 22252 KB Output is correct
25 Correct 59 ms 8780 KB Output is correct
26 Correct 59 ms 8732 KB Output is correct
27 Correct 158 ms 22360 KB Output is correct
28 Correct 150 ms 22288 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 1 ms 304 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Incorrect 1 ms 304 KB Answer gives possible 1 while actual possible 0
33 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
6 Correct 7 ms 1204 KB Output is correct
7 Correct 166 ms 22388 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 7 ms 1204 KB Output is correct
13 Correct 150 ms 22316 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 3 ms 596 KB Output is correct
17 Correct 74 ms 8952 KB Output is correct
18 Correct 0 ms 300 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 38 ms 6056 KB Output is correct
21 Correct 151 ms 22332 KB Output is correct
22 Correct 158 ms 22320 KB Output is correct
23 Correct 161 ms 22236 KB Output is correct
24 Correct 162 ms 22252 KB Output is correct
25 Correct 59 ms 8780 KB Output is correct
26 Correct 59 ms 8732 KB Output is correct
27 Correct 158 ms 22360 KB Output is correct
28 Correct 150 ms 22288 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 1 ms 304 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Incorrect 1 ms 304 KB Answer gives possible 1 while actual possible 0
33 Halted 0 ms 0 KB -