답안 #1061859

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1061859 2024-08-16T14:24:30 Z vjudge1 슈퍼트리 잇기 (IOI20_supertrees) C++17
56 / 100
126 ms 24172 KB
#include "supertrees.h"
#include <bits/stdc++.h>

using namespace std;

using vi = vector<int>;

struct DSU {
    vi e;
    DSU(int n) : e(n, -1) {}

    int repr(int u) {
        int u0 = u;
        while(e[u] >= 0) u = e[u];
        while(e[u0] >= 0) {
            int v = e[u0];
            e[u0] = u;
            u0 = v;
        }
        return u;
    }

    void join(int u, int v) {
        u = repr(u); v = repr(v);
        if(u == v) return;
        if(e[u] >= e[v]) swap(u, v);
        e[u] += e[v];
        e[v] = u;
    }

    bool same(int u, int v) { return repr(u) == repr(v); }

    vector<vi> comp() {
        ///va da componentele
        int n = int(e.size()), nr = 0;
        map<int, int> Id;
        for(int i = 0; i < n; ++i) {
            if(e[i] < 0) {
                Id[i] = nr++;
            }
        }
        vector<vi> Re(nr);
        for(int i = 0; i < n; ++i)
            Re[Id[repr(i)]].push_back(i);
        return Re;
    }
};

bool solve(vi comp, vector<vi> &p, vector<vi> &L) {
    int m = (int)comp.size(), n = (int)p.size();
    DSU Sol(m);
    for(int i = 0; i < m; ++i)
        for(int j = i + 1; j < m; ++j)
            if(p[comp[i]][comp[j]] == 1) Sol.join(i, j); /// arborescenta
    bool ok = true;
    for(int i = 0; i < m; ++i)
        for(int j = i + 1; j < m; ++j)
            if(p[comp[i]][comp[j]] == 2 && Sol.same(i, j)) ok = false;
    auto Arb = Sol.comp();
    vi A;
    for(auto sir : Arb) {
        for(int i = 1; i < (int)sir.size(); ++i) {
            L[comp[sir[0]]][comp[sir[i]]] = 1;
            L[comp[sir[i]]][comp[sir[0]]] = 1;
        }
        A.push_back(comp[sir[0]]);
    }
    for(int i = 0; i + 1 < (int)A.size(); ++i) {
        L[A[i]][A[i + 1]] = 1;
        L[A[i + 1]][A[i]] = 1;
    }
    if(A.size() != 1) {
        L[A[0]][A.back()] = 1;
        L[A.back()][A[0]] = 1;
    }
    return ok;
}

int construct(vector<vi> p) {
    int n = int(p.size());
    DSU Sol(n);
    for(int i = 0; i < n; ++i) {
        for(int j = i + 1; j < n; ++j) {
            if(p[i][j]) Sol.join(i, j);
        }
    }
    bool ok = true;
    for(int i = 0; i < n; ++i) {
        for(int j = i + 1; j < n; ++j) {
            if(!p[i][j] == Sol.same(i, j))
                ok = false;
        }
    }
    if(!ok) return 0;
    auto C = Sol.comp();
    vector<vi> Muchii(n, vi(n, 0));
    for(auto comp : C) {
        ok &= solve(comp, p, Muchii);
    }
    if(ok) {
        build(Muchii);
    }
    return ok;
}

Compilation message

supertrees.cpp: In function 'bool solve(vi, std::vector<std::vector<int> >&, std::vector<std::vector<int> >&)':
supertrees.cpp:50:31: warning: unused variable 'n' [-Wunused-variable]
   50 |     int m = (int)comp.size(), n = (int)p.size();
      |                               ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 5 ms 1184 KB Output is correct
7 Correct 113 ms 22100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 5 ms 1184 KB Output is correct
7 Correct 113 ms 22100 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 5 ms 1116 KB Output is correct
13 Correct 113 ms 22032 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 604 KB Output is correct
17 Correct 56 ms 8216 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 30 ms 5768 KB Output is correct
21 Correct 117 ms 22084 KB Output is correct
22 Correct 110 ms 22024 KB Output is correct
23 Correct 126 ms 22068 KB Output is correct
24 Correct 117 ms 22100 KB Output is correct
25 Correct 53 ms 8196 KB Output is correct
26 Correct 47 ms 8216 KB Output is correct
27 Correct 122 ms 22096 KB Output is correct
28 Correct 110 ms 22100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Incorrect 1 ms 344 KB Answer gives possible 1 while actual possible 0
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 40 ms 6264 KB Output is correct
5 Correct 114 ms 23992 KB Output is correct
6 Correct 121 ms 24148 KB Output is correct
7 Correct 119 ms 24148 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 28 ms 6232 KB Output is correct
10 Correct 118 ms 24172 KB Output is correct
11 Correct 112 ms 23896 KB Output is correct
12 Correct 120 ms 24068 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 29 ms 6420 KB Output is correct
17 Correct 114 ms 24148 KB Output is correct
18 Correct 114 ms 23980 KB Output is correct
19 Correct 110 ms 24148 KB Output is correct
20 Correct 109 ms 24068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 5 ms 1184 KB Output is correct
7 Correct 113 ms 22100 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 5 ms 1116 KB Output is correct
13 Correct 113 ms 22032 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 604 KB Output is correct
17 Correct 56 ms 8216 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 30 ms 5768 KB Output is correct
21 Correct 117 ms 22084 KB Output is correct
22 Correct 110 ms 22024 KB Output is correct
23 Correct 126 ms 22068 KB Output is correct
24 Correct 117 ms 22100 KB Output is correct
25 Correct 53 ms 8196 KB Output is correct
26 Correct 47 ms 8216 KB Output is correct
27 Correct 122 ms 22096 KB Output is correct
28 Correct 110 ms 22100 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Incorrect 1 ms 344 KB Answer gives possible 1 while actual possible 0
33 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 5 ms 1184 KB Output is correct
7 Correct 113 ms 22100 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 5 ms 1116 KB Output is correct
13 Correct 113 ms 22032 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 604 KB Output is correct
17 Correct 56 ms 8216 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 30 ms 5768 KB Output is correct
21 Correct 117 ms 22084 KB Output is correct
22 Correct 110 ms 22024 KB Output is correct
23 Correct 126 ms 22068 KB Output is correct
24 Correct 117 ms 22100 KB Output is correct
25 Correct 53 ms 8196 KB Output is correct
26 Correct 47 ms 8216 KB Output is correct
27 Correct 122 ms 22096 KB Output is correct
28 Correct 110 ms 22100 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Incorrect 1 ms 344 KB Answer gives possible 1 while actual possible 0
33 Halted 0 ms 0 KB -