Submission #572740

# Submission time Handle Problem Language Result Execution time Memory
572740 2022-06-05T07:20:43 Z kartel Connecting Supertrees (IOI20_supertrees) C++14
96 / 100
201 ms 22232 KB
#include <bits/stdc++.h>
//#include "grader.cpp"
#include "supertrees.h"
#define sz(x) (int)x.size()
#define pb push_back
using namespace std;

const int N = 1e3 + 500;

int pr[N];
int prl[N];

int f(int v) {return (pr[v] == v ? v : pr[v] = f(pr[v]));}
void link(int a, int b) {
    a = f(a); b = f(b);
    if (a == b) {
        return;
    }
    pr[a] = b;
}

int fl(int v) {return (prl[v] == v ? v : prl[v] = fl(prl[v]));}
void linkl(int a, int b) {
    a = fl(a); b = fl(b);
    if (a == b) {
        return;
    }
    prl[a] = b;
}

int construct(vector <vector <int> > p) {
    int n = sz(p);
    for (int i = 0; i < n; i++) {
        pr[i] = i;
        prl[i] = i;
    }
    vector <int> cnt(4, 0);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (p[i][j] == 1 && i != j) {
                link(i, j);
            }
            cnt[p[i][j]]++;
            if (p[i][j] > 0 && i != j) {
                linkl(i, j);
            }
        }
    }
    if (cnt[2] && cnt[3]) {
        return 0;
    }

    vector <vector <int> > b(n, vector <int> (n, 0));
    vector <set<int> > cp(n);
    set<int> cpp;

    for (int i = 0; i < n; i++) {
        cp[fl(i)].insert(i);
        cpp.insert(fl(i));
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (fl(i) == fl(j) && !p[i][j]) {
                return 0;
            }
        }
    }

    vector <int> used(n, 0);
    for (auto pp : cpp) {
        set <int> cyc;
        vector <int> c;
        for (auto i : cp[pp]) {
            if (cyc.count(f(i))) {
                continue;
            }
            cyc.insert(f(i));
            c.pb(i);
        }

        if (sz(c) > 1) {
            bool is3 = (p[c[0]][c[1]] == 3);
            bool is2 = (p[c[0]][c[1]] == 2);

            if (is3 && (sz(cyc) == 3 || sz(cyc) == 2)) {
                return 0;
            }
            if (is2 && sz(cyc) == 2) {
                return 0;
            }
            for (int i = 0; i < sz(c); i++) {
                b[c[i]][c[(i + 1) % sz(c)]] = 1;
                b[c[(i + 1) % sz(c)]][c[i]] = 1;
            }
            if (is3) {
                b[c[0]][c[2]] = 1;
                b[c[2]][c[0]] = 1;
            }
        }

        vector <vector <int> > cmp(n);
        set <int> cmps;
        for (auto i : cp[pp]) {
            cmp[f(i)].pb(i);
            cmps.insert(f(i));
        }

        int it = 0;
        for (auto i : cp[pp]) {
            if (sz(cmp[i])) {
                int last = cmp[i][0];
                for (auto x : cmp[i]) {
                    if (x == last) {
                        continue;
                    }
                    b[last][x] = 1;
                    b[x][last] = 1;
                    last = x;
                }
            }
        }
    }
    for (int i = 0; i < n; i++) {
        b[i][i] = 0;
    }

    build(b);
    return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:109:13: warning: unused variable 'it' [-Wunused-variable]
  109 |         int it = 0;
      |             ^~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1108 KB Output is correct
7 Correct 170 ms 22148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1108 KB Output is correct
7 Correct 170 ms 22148 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 8 ms 1236 KB Output is correct
13 Correct 170 ms 22160 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 764 KB Output is correct
17 Correct 93 ms 12164 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 43 ms 5812 KB Output is correct
21 Correct 176 ms 22152 KB Output is correct
22 Correct 172 ms 22208 KB Output is correct
23 Correct 191 ms 22152 KB Output is correct
24 Correct 166 ms 22168 KB Output is correct
25 Correct 76 ms 12172 KB Output is correct
26 Correct 71 ms 12176 KB Output is correct
27 Correct 190 ms 22092 KB Output is correct
28 Correct 174 ms 22164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 8 ms 1108 KB Output is correct
9 Correct 175 ms 22176 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 7 ms 1108 KB Output is correct
13 Correct 172 ms 22152 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 5 ms 760 KB Output is correct
17 Correct 86 ms 12164 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 43 ms 5764 KB Output is correct
22 Correct 171 ms 22156 KB Output is correct
23 Correct 172 ms 22112 KB Output is correct
24 Correct 185 ms 22120 KB Output is correct
25 Correct 77 ms 12308 KB Output is correct
26 Correct 75 ms 12180 KB Output is correct
27 Correct 178 ms 22132 KB Output is correct
28 Correct 193 ms 22152 KB Output is correct
29 Correct 74 ms 12236 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 0 ms 212 KB Output is correct
4 Correct 45 ms 5772 KB Output is correct
5 Correct 176 ms 22092 KB Output is correct
6 Correct 169 ms 22164 KB Output is correct
7 Correct 196 ms 22232 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 43 ms 5788 KB Output is correct
10 Correct 178 ms 22156 KB Output is correct
11 Correct 174 ms 22196 KB Output is correct
12 Correct 201 ms 22088 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 45 ms 5792 KB Output is correct
17 Correct 184 ms 22080 KB Output is correct
18 Correct 177 ms 22104 KB Output is correct
19 Correct 171 ms 22148 KB Output is correct
20 Correct 173 ms 22084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1108 KB Output is correct
7 Correct 170 ms 22148 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 8 ms 1236 KB Output is correct
13 Correct 170 ms 22160 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 764 KB Output is correct
17 Correct 93 ms 12164 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 43 ms 5812 KB Output is correct
21 Correct 176 ms 22152 KB Output is correct
22 Correct 172 ms 22208 KB Output is correct
23 Correct 191 ms 22152 KB Output is correct
24 Correct 166 ms 22168 KB Output is correct
25 Correct 76 ms 12172 KB Output is correct
26 Correct 71 ms 12176 KB Output is correct
27 Correct 190 ms 22092 KB Output is correct
28 Correct 174 ms 22164 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 8 ms 1108 KB Output is correct
37 Correct 175 ms 22176 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 7 ms 1108 KB Output is correct
41 Correct 172 ms 22152 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 5 ms 760 KB Output is correct
45 Correct 86 ms 12164 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 43 ms 5764 KB Output is correct
50 Correct 171 ms 22156 KB Output is correct
51 Correct 172 ms 22112 KB Output is correct
52 Correct 185 ms 22120 KB Output is correct
53 Correct 77 ms 12308 KB Output is correct
54 Correct 75 ms 12180 KB Output is correct
55 Correct 178 ms 22132 KB Output is correct
56 Correct 193 ms 22152 KB Output is correct
57 Correct 74 ms 12236 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 4 ms 724 KB Output is correct
61 Correct 88 ms 12124 KB Output is correct
62 Correct 0 ms 212 KB Output is correct
63 Correct 1 ms 212 KB Output is correct
64 Correct 0 ms 212 KB Output is correct
65 Correct 49 ms 5756 KB Output is correct
66 Correct 76 ms 12236 KB Output is correct
67 Correct 75 ms 12184 KB Output is correct
68 Correct 73 ms 12180 KB Output is correct
69 Correct 79 ms 12236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1108 KB Output is correct
7 Correct 170 ms 22148 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 8 ms 1236 KB Output is correct
13 Correct 170 ms 22160 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 764 KB Output is correct
17 Correct 93 ms 12164 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 43 ms 5812 KB Output is correct
21 Correct 176 ms 22152 KB Output is correct
22 Correct 172 ms 22208 KB Output is correct
23 Correct 191 ms 22152 KB Output is correct
24 Correct 166 ms 22168 KB Output is correct
25 Correct 76 ms 12172 KB Output is correct
26 Correct 71 ms 12176 KB Output is correct
27 Correct 190 ms 22092 KB Output is correct
28 Correct 174 ms 22164 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 8 ms 1108 KB Output is correct
37 Correct 175 ms 22176 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 7 ms 1108 KB Output is correct
41 Correct 172 ms 22152 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 5 ms 760 KB Output is correct
45 Correct 86 ms 12164 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 43 ms 5764 KB Output is correct
50 Correct 171 ms 22156 KB Output is correct
51 Correct 172 ms 22112 KB Output is correct
52 Correct 185 ms 22120 KB Output is correct
53 Correct 77 ms 12308 KB Output is correct
54 Correct 75 ms 12180 KB Output is correct
55 Correct 178 ms 22132 KB Output is correct
56 Correct 193 ms 22152 KB Output is correct
57 Correct 74 ms 12236 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 45 ms 5772 KB Output is correct
62 Correct 176 ms 22092 KB Output is correct
63 Correct 169 ms 22164 KB Output is correct
64 Correct 196 ms 22232 KB Output is correct
65 Correct 0 ms 212 KB Output is correct
66 Correct 43 ms 5788 KB Output is correct
67 Correct 178 ms 22156 KB Output is correct
68 Correct 174 ms 22196 KB Output is correct
69 Correct 201 ms 22088 KB Output is correct
70 Correct 1 ms 212 KB Output is correct
71 Correct 0 ms 212 KB Output is correct
72 Correct 1 ms 212 KB Output is correct
73 Correct 45 ms 5792 KB Output is correct
74 Correct 184 ms 22080 KB Output is correct
75 Correct 177 ms 22104 KB Output is correct
76 Correct 171 ms 22148 KB Output is correct
77 Correct 173 ms 22084 KB Output is correct
78 Correct 0 ms 212 KB Output is correct
79 Correct 0 ms 212 KB Output is correct
80 Correct 4 ms 724 KB Output is correct
81 Correct 88 ms 12124 KB Output is correct
82 Correct 0 ms 212 KB Output is correct
83 Correct 1 ms 212 KB Output is correct
84 Correct 0 ms 212 KB Output is correct
85 Correct 49 ms 5756 KB Output is correct
86 Correct 76 ms 12236 KB Output is correct
87 Correct 75 ms 12184 KB Output is correct
88 Correct 73 ms 12180 KB Output is correct
89 Correct 79 ms 12236 KB Output is correct
90 Correct 0 ms 212 KB Output is correct
91 Correct 0 ms 212 KB Output is correct
92 Correct 5 ms 596 KB Output is correct
93 Correct 85 ms 8228 KB Output is correct
94 Correct 0 ms 212 KB Output is correct
95 Correct 0 ms 212 KB Output is correct
96 Correct 1 ms 212 KB Output is correct
97 Correct 18 ms 2260 KB Output is correct
98 Correct 75 ms 8072 KB Output is correct
99 Correct 73 ms 8088 KB Output is correct
100 Correct 77 ms 8068 KB Output is correct
101 Correct 79 ms 8068 KB Output is correct
102 Incorrect 171 ms 22132 KB Answer gives possible 1 while actual possible 0
103 Halted 0 ms 0 KB -