Submission #675744

# Submission time Handle Problem Language Result Execution time Memory
675744 2022-12-27T19:31:53 Z YENGOYAN Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
258 ms 26244 KB
#include "supertrees.h"
#include <bits/stdc++.h>

using namespace std;

vector<int> par, sizes;

int get(int u) {
    if (par[u] == u) return u;
    return par[u] = get(par[u]);
}

bool areInOne(int u, int v) {
    return get(u) == get(v);
}

void union_sets(int u, int v) {
    int a = get(u), b = get(v);
    if (a == b) return;
    if (sizes[a] < sizes[b]) swap(a, b);
    sizes[a] += sizes[b];
    par[b] = a;
}

vector<int> comp;

void dfs(int u, vector<vector<int>>& gp, vector<bool>& vis) {
    comp.push_back(u);
    vis[u] = 1;
    for (int& v : gp[u]) {
        if (!vis[v]) {
            dfs(v, gp, vis);
        }
    }
}

int construct(vector<vector<int>> p) {

    int n = p.size();
    vector<vector<int>> gp(n, vector<int>(n));
    par = sizes = vector<int>(n);
    for (int i = 0; i < n; i++) par[i] = i, sizes[i] = 1;
    set<int> vec;
    vector<vector<int>> gppppp(n);
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            if (p[i][j] == 3) {
                gppppp[i].push_back(j);
                gppppp[j].push_back(i);
                union_sets(i, j);   
            }
        }
    }
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            if (p[i][j] != 3 && areInOne(i, j)) return 0;
        }
    }
    vector<bool> mv(n);
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            if (p[i][j] == 3) {
                if (!mv[i]) {
                    comp.clear();
                    dfs(i, gppppp, mv);
                    if (comp.size() < 4) return 0;
                    for (int i = 0; i < comp.size() - 1; ++i) gp[comp[i]][comp[i + 1]] = gp[comp[i + 1]][comp[i]] = 1;
                    gp[comp.back()][comp[0]] = gp[comp[0]][comp.back()] = 1;
                    gp[comp[0]][comp[2]] = gp[comp[2]][comp[0]];
                    set<int> st;
                    for (auto x : comp) {
                        for (int i = 0; i < n; ++i) {
                            if (p[x][i] && st.find(i) != st.end()) return 0;
                            if(p[x][i] && p[x][i] < 3)st.insert(i);
                        }
                    }
                }
            }
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (!areInOne(i, j) && p[i][j] == 1) {
                gp[i][j] = gp[j][i] = 1;
                union_sets(i, j);
            }
        }
    }
    vector<bool> used(n);
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (p[i][j] == 1) {
                used[i] = used[j] = 1;
                vec.insert(get(i));
            }
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int u = get(i), v = get(j);
            if (u == v && !p[i][j]) return 0;
            if (u != v && p[i][j]==1) return 0;
        }
    }
    vector<vector<int>> graph(n);
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (p[i][j]) {
                union_sets(i, j);
                graph[i].push_back(j);
                graph[j].push_back(i);
            }
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int u = get(i), v = get(j);
            if (u == v && !p[i][j]) return 0;
            if (u != v && p[i][j]) return 0;
        }
    }

    vector<bool> vis(n);
    for (int i = 0; i < n; i++) {
        if (vis[i]) continue;
        comp.clear();
        dfs(i, graph, vis);
        bool f = 0;
        vector<int> rightComp;
        int node = -1;
        for (int i = 0; i < comp.size(); i++) {
            if (!used[comp[i]]) rightComp.push_back(comp[i]);
            else if (vec.find(comp[i]) != vec.end()) rightComp.push_back(comp[i]);
        }
        for (int i = 0; i < rightComp.size() - 1; i++) {
            gp[rightComp[i]][rightComp[i + 1]] = gp[rightComp[i + 1]][rightComp[i]] = 1;
        }
        if (rightComp.size() > 2) gp[rightComp[0]][rightComp.back()] = gp[rightComp.back()][rightComp[0]] = 1;
        else if (rightComp.size() == 2) {
            return 0;
        }
    }
    /*
        1 1 2 2 2
        1 1 2 2 2
        2 2 1 1 2
        2 2 1 1 2
        2 2 2 2 1




    */
    build(gp);
    return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:67:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |                     for (int i = 0; i < comp.size() - 1; ++i) gp[comp[i]][comp[i + 1]] = gp[comp[i + 1]][comp[i]] = 1;
      |                                     ~~^~~~~~~~~~~~~~~~~
supertrees.cpp:131:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  131 |         for (int i = 0; i < comp.size(); i++) {
      |                         ~~^~~~~~~~~~~~~
supertrees.cpp:135:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  135 |         for (int i = 0; i < rightComp.size() - 1; i++) {
      |                         ~~^~~~~~~~~~~~~~~~~~~~~~
supertrees.cpp:128:14: warning: unused variable 'f' [-Wunused-variable]
  128 |         bool f = 0;
      |              ^
supertrees.cpp:130:13: warning: unused variable 'node' [-Wunused-variable]
  130 |         int node = -1;
      |             ^~~~
# 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 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 10 ms 1416 KB Output is correct
7 Correct 206 ms 26244 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 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 10 ms 1416 KB Output is correct
7 Correct 206 ms 26244 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 0 ms 212 KB Output is correct
12 Correct 10 ms 1108 KB Output is correct
13 Correct 186 ms 22068 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 6 ms 720 KB Output is correct
17 Correct 99 ms 12048 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 70 ms 5764 KB Output is correct
21 Correct 227 ms 22620 KB Output is correct
22 Correct 208 ms 22060 KB Output is correct
23 Correct 238 ms 24140 KB Output is correct
24 Correct 217 ms 22056 KB Output is correct
25 Correct 95 ms 12164 KB Output is correct
26 Correct 97 ms 12160 KB Output is correct
27 Correct 255 ms 25224 KB Output is correct
28 Correct 216 ms 22036 KB Output is correct
# 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 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 8 ms 1176 KB Output is correct
9 Correct 213 ms 22028 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 10 ms 1384 KB Output is correct
13 Correct 202 ms 26172 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 852 KB Output is correct
17 Correct 143 ms 14932 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 47 ms 5788 KB Output is correct
22 Correct 193 ms 22628 KB Output is correct
23 Correct 195 ms 22028 KB Output is correct
24 Correct 225 ms 25184 KB Output is correct
25 Correct 93 ms 12164 KB Output is correct
26 Correct 98 ms 12672 KB Output is correct
27 Correct 213 ms 22088 KB Output is correct
28 Correct 210 ms 24196 KB Output is correct
29 Correct 97 ms 12164 KB Output is correct
# 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 1 ms 212 KB Output is correct
4 Correct 71 ms 5768 KB Output is correct
5 Correct 195 ms 22576 KB Output is correct
6 Correct 252 ms 22148 KB Output is correct
7 Correct 258 ms 24092 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 48 ms 5748 KB Output is correct
10 Correct 192 ms 22616 KB Output is correct
11 Correct 183 ms 22100 KB Output is correct
12 Correct 215 ms 25216 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 50 ms 5820 KB Output is correct
17 Correct 236 ms 22540 KB Output is correct
18 Correct 205 ms 22656 KB Output is correct
19 Correct 208 ms 22244 KB Output is correct
20 Correct 178 ms 22092 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 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 10 ms 1416 KB Output is correct
7 Correct 206 ms 26244 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 0 ms 212 KB Output is correct
12 Correct 10 ms 1108 KB Output is correct
13 Correct 186 ms 22068 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 6 ms 720 KB Output is correct
17 Correct 99 ms 12048 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 70 ms 5764 KB Output is correct
21 Correct 227 ms 22620 KB Output is correct
22 Correct 208 ms 22060 KB Output is correct
23 Correct 238 ms 24140 KB Output is correct
24 Correct 217 ms 22056 KB Output is correct
25 Correct 95 ms 12164 KB Output is correct
26 Correct 97 ms 12160 KB Output is correct
27 Correct 255 ms 25224 KB Output is correct
28 Correct 216 ms 22036 KB Output is correct
29 Correct 0 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 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 8 ms 1176 KB Output is correct
37 Correct 213 ms 22028 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 10 ms 1384 KB Output is correct
41 Correct 202 ms 26172 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 852 KB Output is correct
45 Correct 143 ms 14932 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 47 ms 5788 KB Output is correct
50 Correct 193 ms 22628 KB Output is correct
51 Correct 195 ms 22028 KB Output is correct
52 Correct 225 ms 25184 KB Output is correct
53 Correct 93 ms 12164 KB Output is correct
54 Correct 98 ms 12672 KB Output is correct
55 Correct 213 ms 22088 KB Output is correct
56 Correct 210 ms 24196 KB Output is correct
57 Correct 97 ms 12164 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 3 ms 724 KB Output is correct
61 Correct 85 ms 12164 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 0 ms 212 KB Output is correct
64 Correct 0 ms 212 KB Output is correct
65 Correct 50 ms 5836 KB Output is correct
66 Correct 83 ms 12688 KB Output is correct
67 Correct 75 ms 12164 KB Output is correct
68 Correct 87 ms 12304 KB Output is correct
69 Correct 89 ms 12164 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 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 10 ms 1416 KB Output is correct
7 Correct 206 ms 26244 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 0 ms 212 KB Output is correct
12 Correct 10 ms 1108 KB Output is correct
13 Correct 186 ms 22068 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 6 ms 720 KB Output is correct
17 Correct 99 ms 12048 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 70 ms 5764 KB Output is correct
21 Correct 227 ms 22620 KB Output is correct
22 Correct 208 ms 22060 KB Output is correct
23 Correct 238 ms 24140 KB Output is correct
24 Correct 217 ms 22056 KB Output is correct
25 Correct 95 ms 12164 KB Output is correct
26 Correct 97 ms 12160 KB Output is correct
27 Correct 255 ms 25224 KB Output is correct
28 Correct 216 ms 22036 KB Output is correct
29 Correct 0 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 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 8 ms 1176 KB Output is correct
37 Correct 213 ms 22028 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 10 ms 1384 KB Output is correct
41 Correct 202 ms 26172 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 852 KB Output is correct
45 Correct 143 ms 14932 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 47 ms 5788 KB Output is correct
50 Correct 193 ms 22628 KB Output is correct
51 Correct 195 ms 22028 KB Output is correct
52 Correct 225 ms 25184 KB Output is correct
53 Correct 93 ms 12164 KB Output is correct
54 Correct 98 ms 12672 KB Output is correct
55 Correct 213 ms 22088 KB Output is correct
56 Correct 210 ms 24196 KB Output is correct
57 Correct 97 ms 12164 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 71 ms 5768 KB Output is correct
62 Correct 195 ms 22576 KB Output is correct
63 Correct 252 ms 22148 KB Output is correct
64 Correct 258 ms 24092 KB Output is correct
65 Correct 1 ms 212 KB Output is correct
66 Correct 48 ms 5748 KB Output is correct
67 Correct 192 ms 22616 KB Output is correct
68 Correct 183 ms 22100 KB Output is correct
69 Correct 215 ms 25216 KB Output is correct
70 Correct 1 ms 212 KB Output is correct
71 Correct 1 ms 212 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 50 ms 5820 KB Output is correct
74 Correct 236 ms 22540 KB Output is correct
75 Correct 205 ms 22656 KB Output is correct
76 Correct 208 ms 22244 KB Output is correct
77 Correct 178 ms 22092 KB Output is correct
78 Correct 0 ms 212 KB Output is correct
79 Correct 0 ms 212 KB Output is correct
80 Correct 3 ms 724 KB Output is correct
81 Correct 85 ms 12164 KB Output is correct
82 Correct 1 ms 212 KB Output is correct
83 Correct 0 ms 212 KB Output is correct
84 Correct 0 ms 212 KB Output is correct
85 Correct 50 ms 5836 KB Output is correct
86 Correct 83 ms 12688 KB Output is correct
87 Correct 75 ms 12164 KB Output is correct
88 Correct 87 ms 12304 KB Output is correct
89 Correct 89 ms 12164 KB Output is correct
90 Correct 0 ms 276 KB Output is correct
91 Correct 0 ms 212 KB Output is correct
92 Correct 4 ms 852 KB Output is correct
93 Correct 78 ms 13696 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 17 ms 3304 KB Output is correct
98 Correct 71 ms 12160 KB Output is correct
99 Correct 69 ms 12164 KB Output is correct
100 Correct 69 ms 12164 KB Output is correct
101 Correct 69 ms 12088 KB Output is correct
102 Correct 77 ms 16276 KB Output is correct
103 Correct 77 ms 16844 KB Output is correct
104 Correct 77 ms 16948 KB Output is correct
105 Correct 99 ms 16896 KB Output is correct