답안 #768776

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
768776 2023-06-28T15:06:30 Z AlesL0 슈퍼트리 잇기 (IOI20_supertrees) C++17
56 / 100
212 ms 24108 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]] == 2 && 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 = i+1; 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 = i+1; 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++){
      |
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 296 KB Output is correct
6 Correct 11 ms 1188 KB Output is correct
7 Correct 159 ms 23988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 296 KB Output is correct
6 Correct 11 ms 1188 KB Output is correct
7 Correct 159 ms 23988 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 8 ms 1208 KB Output is correct
13 Correct 156 ms 24016 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 696 KB Output is correct
17 Correct 73 ms 10100 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 54 ms 6264 KB Output is correct
21 Correct 191 ms 24016 KB Output is correct
22 Correct 171 ms 23936 KB Output is correct
23 Correct 212 ms 23936 KB Output is correct
24 Correct 157 ms 23936 KB Output is correct
25 Correct 63 ms 10112 KB Output is correct
26 Correct 60 ms 10100 KB Output is correct
27 Correct 161 ms 23936 KB Output is correct
28 Correct 155 ms 23960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Incorrect 1 ms 212 KB Answer gives possible 1 while actual possible 0
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 39 ms 6260 KB Output is correct
5 Correct 160 ms 24108 KB Output is correct
6 Correct 153 ms 23920 KB Output is correct
7 Correct 171 ms 23976 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 39 ms 6260 KB Output is correct
10 Correct 158 ms 23924 KB Output is correct
11 Correct 182 ms 23936 KB Output is correct
12 Correct 159 ms 23980 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 300 KB Output is correct
15 Correct 1 ms 300 KB Output is correct
16 Correct 39 ms 6212 KB Output is correct
17 Correct 154 ms 23924 KB Output is correct
18 Correct 158 ms 23924 KB Output is correct
19 Correct 162 ms 24048 KB Output is correct
20 Correct 152 ms 23980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 296 KB Output is correct
6 Correct 11 ms 1188 KB Output is correct
7 Correct 159 ms 23988 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 8 ms 1208 KB Output is correct
13 Correct 156 ms 24016 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 696 KB Output is correct
17 Correct 73 ms 10100 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 54 ms 6264 KB Output is correct
21 Correct 191 ms 24016 KB Output is correct
22 Correct 171 ms 23936 KB Output is correct
23 Correct 212 ms 23936 KB Output is correct
24 Correct 157 ms 23936 KB Output is correct
25 Correct 63 ms 10112 KB Output is correct
26 Correct 60 ms 10100 KB Output is correct
27 Correct 161 ms 23936 KB Output is correct
28 Correct 155 ms 23960 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Incorrect 1 ms 212 KB Answer gives possible 1 while actual possible 0
33 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 296 KB Output is correct
6 Correct 11 ms 1188 KB Output is correct
7 Correct 159 ms 23988 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 8 ms 1208 KB Output is correct
13 Correct 156 ms 24016 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 696 KB Output is correct
17 Correct 73 ms 10100 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 54 ms 6264 KB Output is correct
21 Correct 191 ms 24016 KB Output is correct
22 Correct 171 ms 23936 KB Output is correct
23 Correct 212 ms 23936 KB Output is correct
24 Correct 157 ms 23936 KB Output is correct
25 Correct 63 ms 10112 KB Output is correct
26 Correct 60 ms 10100 KB Output is correct
27 Correct 161 ms 23936 KB Output is correct
28 Correct 155 ms 23960 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Incorrect 1 ms 212 KB Answer gives possible 1 while actual possible 0
33 Halted 0 ms 0 KB -