Submission #772877

# Submission time Handle Problem Language Result Execution time Memory
772877 2023-07-04T12:13:09 Z fve5 Connecting Supertrees (IOI20_supertrees) C++17
96 / 100
196 ms 28112 KB
#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;

struct DSU {
    vector<int> nodes;

    int find(int node) {
        if (nodes[node] < 0) return node;
        return nodes[node] = find(nodes[node]);
    }

    void join(int u, int v) {
        u = find(u), v = find(v);
        if (u == v) return;

        if (nodes[u] > nodes[v]) swap(u, v);
        nodes[u] += nodes[v];
        nodes[v] = u;
    }

    map<int, vector<int>> get_cc() {
        map<int, vector<int>> ans;
        for (int i = 0; i < nodes.size(); i++) {
            ans[find(i)].push_back(i);
        }
        return ans;
    }

    DSU(int size) : nodes(size, -1) { }
};

int construct(vector<vector<int>> p) {
    int n = p.size();
    
    vector adj(n, vector(n, 0));
    vector act_p(n, vector(n, 0));
    DSU dsu(n);
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (p[i][j]) dsu.join(i, j);
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (dsu.find(i) == dsu.find(j) && p[i][j] == 0) {
                return 0;
            }
        }
    }

    auto ccs = dsu.get_cc();
    for (auto [_, cc]: ccs) {
        bool found2 = false;
        DSU dsu2(cc.size());
        for (int i = 0; i < cc.size(); i++) {
            for (int j = 0; j < cc.size(); j++) {
                if (p[cc[i]][cc[j]] == 1) dsu2.join(i, j);
                if (p[cc[i]][cc[j]] == 2) found2 = true;
            }
        }
        for (int i = 0; i < cc.size(); i++) {
            for (int j = 0; j < cc.size(); j++) {
                //cout << i << " " << j << " " << dsu2.find(i) << " " << dsu2.find(j) << endl;
                if (dsu2.find(i) == dsu2.find(j) && p[cc[i]][cc[j]] == 2) return 0;
            }
        }

        auto ccs2 = dsu2.get_cc();
        vector<int> cycle;
        for (auto [r, cc2]: ccs2) {
            cycle.push_back(cc[r]);
            for (int i = 1; i < cc2.size(); i++) {
                adj[cc[cc2[i]]][cc[cc2[i - 1]]] = adj[cc[cc2[i - 1]]][cc[cc2[i]]] = 1;
            }
        }
        if (cycle.size() < 3) {
            if (found2) return 0;
            else continue;
        }
        for (int i = 0; i < cycle.size(); i++) {
            adj[cycle[i]][cycle[(i + 1) % cycle.size()]] = adj[cycle[(i + 1) % cycle.size()]][cycle[i]] = 1;
        }
    }

    build(adj);
    return 1;
}

Compilation message

supertrees.cpp: In member function 'std::map<int, std::vector<int> > DSU::get_cc()':
supertrees.cpp:24:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |         for (int i = 0; i < nodes.size(); i++) {
      |                         ~~^~~~~~~~~~~~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:56:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |         for (int i = 0; i < cc.size(); i++) {
      |                         ~~^~~~~~~~~~~
supertrees.cpp:57:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |             for (int j = 0; j < cc.size(); j++) {
      |                             ~~^~~~~~~~~~~
supertrees.cpp:62:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |         for (int i = 0; i < cc.size(); i++) {
      |                         ~~^~~~~~~~~~~
supertrees.cpp:63:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |             for (int j = 0; j < cc.size(); j++) {
      |                             ~~^~~~~~~~~~~
supertrees.cpp:73:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |             for (int i = 1; i < cc2.size(); i++) {
      |                             ~~^~~~~~~~~~~~
supertrees.cpp:81:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |         for (int i = 0; i < cycle.size(); i++) {
      |                         ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1416 KB Output is correct
7 Correct 174 ms 26532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1416 KB Output is correct
7 Correct 174 ms 26532 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 300 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 300 KB Output is correct
12 Correct 7 ms 1416 KB Output is correct
13 Correct 156 ms 26716 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 952 KB Output is correct
17 Correct 72 ms 17328 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 40 ms 7100 KB Output is correct
21 Correct 171 ms 26376 KB Output is correct
22 Correct 157 ms 26420 KB Output is correct
23 Correct 196 ms 26316 KB Output is correct
24 Correct 167 ms 26416 KB Output is correct
25 Correct 66 ms 17324 KB Output is correct
26 Correct 68 ms 17612 KB Output is correct
27 Correct 166 ms 26344 KB Output is correct
28 Correct 155 ms 26448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 296 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 7 ms 1364 KB Output is correct
9 Correct 154 ms 28032 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 300 KB Output is correct
12 Correct 8 ms 1364 KB Output is correct
13 Correct 163 ms 27896 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 296 KB Output is correct
16 Correct 3 ms 928 KB Output is correct
17 Correct 71 ms 18036 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 49 ms 7152 KB Output is correct
22 Correct 158 ms 27968 KB Output is correct
23 Correct 156 ms 27952 KB Output is correct
24 Correct 162 ms 27900 KB Output is correct
25 Correct 69 ms 18088 KB Output is correct
26 Correct 65 ms 18044 KB Output is correct
27 Correct 156 ms 28112 KB Output is correct
28 Correct 162 ms 27956 KB Output is correct
29 Correct 66 ms 18032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 1 ms 304 KB Output is correct
4 Correct 46 ms 7108 KB Output is correct
5 Correct 158 ms 26512 KB Output is correct
6 Correct 173 ms 26568 KB Output is correct
7 Correct 167 ms 26492 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 40 ms 7012 KB Output is correct
10 Correct 164 ms 26740 KB Output is correct
11 Correct 160 ms 26508 KB Output is correct
12 Correct 167 ms 26468 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 212 KB Output is correct
16 Correct 42 ms 7008 KB Output is correct
17 Correct 158 ms 26512 KB Output is correct
18 Correct 173 ms 26444 KB Output is correct
19 Correct 165 ms 26516 KB Output is correct
20 Correct 162 ms 26616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1416 KB Output is correct
7 Correct 174 ms 26532 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 300 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 300 KB Output is correct
12 Correct 7 ms 1416 KB Output is correct
13 Correct 156 ms 26716 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 952 KB Output is correct
17 Correct 72 ms 17328 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 40 ms 7100 KB Output is correct
21 Correct 171 ms 26376 KB Output is correct
22 Correct 157 ms 26420 KB Output is correct
23 Correct 196 ms 26316 KB Output is correct
24 Correct 167 ms 26416 KB Output is correct
25 Correct 66 ms 17324 KB Output is correct
26 Correct 68 ms 17612 KB Output is correct
27 Correct 166 ms 26344 KB Output is correct
28 Correct 155 ms 26448 KB Output is correct
29 Correct 1 ms 296 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 300 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 7 ms 1364 KB Output is correct
37 Correct 154 ms 28032 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 300 KB Output is correct
40 Correct 8 ms 1364 KB Output is correct
41 Correct 163 ms 27896 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 296 KB Output is correct
44 Correct 3 ms 928 KB Output is correct
45 Correct 71 ms 18036 KB Output is correct
46 Correct 1 ms 256 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 49 ms 7152 KB Output is correct
50 Correct 158 ms 27968 KB Output is correct
51 Correct 156 ms 27952 KB Output is correct
52 Correct 162 ms 27900 KB Output is correct
53 Correct 69 ms 18088 KB Output is correct
54 Correct 65 ms 18044 KB Output is correct
55 Correct 156 ms 28112 KB Output is correct
56 Correct 162 ms 27956 KB Output is correct
57 Correct 66 ms 18032 KB Output is correct
58 Correct 1 ms 304 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 4 ms 924 KB Output is correct
61 Correct 69 ms 17916 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 1 ms 212 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Correct 40 ms 7152 KB Output is correct
66 Correct 65 ms 17916 KB Output is correct
67 Correct 66 ms 18036 KB Output is correct
68 Correct 63 ms 17968 KB Output is correct
69 Correct 65 ms 18032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1416 KB Output is correct
7 Correct 174 ms 26532 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 300 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 300 KB Output is correct
12 Correct 7 ms 1416 KB Output is correct
13 Correct 156 ms 26716 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 952 KB Output is correct
17 Correct 72 ms 17328 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 40 ms 7100 KB Output is correct
21 Correct 171 ms 26376 KB Output is correct
22 Correct 157 ms 26420 KB Output is correct
23 Correct 196 ms 26316 KB Output is correct
24 Correct 167 ms 26416 KB Output is correct
25 Correct 66 ms 17324 KB Output is correct
26 Correct 68 ms 17612 KB Output is correct
27 Correct 166 ms 26344 KB Output is correct
28 Correct 155 ms 26448 KB Output is correct
29 Correct 1 ms 296 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 300 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 7 ms 1364 KB Output is correct
37 Correct 154 ms 28032 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 300 KB Output is correct
40 Correct 8 ms 1364 KB Output is correct
41 Correct 163 ms 27896 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 296 KB Output is correct
44 Correct 3 ms 928 KB Output is correct
45 Correct 71 ms 18036 KB Output is correct
46 Correct 1 ms 256 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 49 ms 7152 KB Output is correct
50 Correct 158 ms 27968 KB Output is correct
51 Correct 156 ms 27952 KB Output is correct
52 Correct 162 ms 27900 KB Output is correct
53 Correct 69 ms 18088 KB Output is correct
54 Correct 65 ms 18044 KB Output is correct
55 Correct 156 ms 28112 KB Output is correct
56 Correct 162 ms 27956 KB Output is correct
57 Correct 66 ms 18032 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 1 ms 300 KB Output is correct
60 Correct 1 ms 304 KB Output is correct
61 Correct 46 ms 7108 KB Output is correct
62 Correct 158 ms 26512 KB Output is correct
63 Correct 173 ms 26568 KB Output is correct
64 Correct 167 ms 26492 KB Output is correct
65 Correct 0 ms 212 KB Output is correct
66 Correct 40 ms 7012 KB Output is correct
67 Correct 164 ms 26740 KB Output is correct
68 Correct 160 ms 26508 KB Output is correct
69 Correct 167 ms 26468 KB Output is correct
70 Correct 1 ms 212 KB Output is correct
71 Correct 1 ms 300 KB Output is correct
72 Correct 1 ms 212 KB Output is correct
73 Correct 42 ms 7008 KB Output is correct
74 Correct 158 ms 26512 KB Output is correct
75 Correct 173 ms 26444 KB Output is correct
76 Correct 165 ms 26516 KB Output is correct
77 Correct 162 ms 26616 KB Output is correct
78 Correct 1 ms 304 KB Output is correct
79 Correct 1 ms 212 KB Output is correct
80 Correct 4 ms 924 KB Output is correct
81 Correct 69 ms 17916 KB Output is correct
82 Correct 1 ms 212 KB Output is correct
83 Correct 1 ms 212 KB Output is correct
84 Correct 1 ms 212 KB Output is correct
85 Correct 40 ms 7152 KB Output is correct
86 Correct 65 ms 17916 KB Output is correct
87 Correct 66 ms 18036 KB Output is correct
88 Correct 63 ms 17968 KB Output is correct
89 Correct 65 ms 18032 KB Output is correct
90 Correct 1 ms 212 KB Output is correct
91 Correct 0 ms 296 KB Output is correct
92 Correct 3 ms 924 KB Output is correct
93 Correct 68 ms 17976 KB Output is correct
94 Incorrect 1 ms 212 KB Answer gives possible 1 while actual possible 0
95 Halted 0 ms 0 KB -