Submission #619523

# Submission time Handle Problem Language Result Execution time Memory
619523 2022-08-02T12:42:41 Z sokratisi Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
281 ms 28164 KB
#include "supertrees.h"
#include <vector>
#include <unordered_set>
#include <unordered_map>

using namespace std;

vector<vector<int>> groups;
int n;
vector<vector<int>> adj;
vector<bool> vis;
int total;
unordered_map<int, int> group;

// void build(vector<vector<int>> answer) {
//     for (int i = 0; i < answer.size(); i++) {
//         for (int j = 0; j < answer.size(); j++) {
//             printf("%d ", answer[i][j]);
//         }
//         printf("\n");
//     }
// }

void dfs(int s) {
    vis[s] = true;
    groups[groups.size() - 1].push_back(s);
    group[s] = groups.size() - 1;
    for (auto u: adj[s]) {
        total++;
        if (!vis[u]) {
            dfs(u);
        }
    }
}

int construct(vector<vector<int>> p) {
	n = p.size();
    adj.resize(n);
    vis.resize(n, false);
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (p[i][j] == 1) {
                adj[i].push_back(j);
                adj[j].push_back(i);
            }
        }
    }
    vector<int> pb;
    bool ok = true;
    for (int i = 0; i < n; i++) {
        if (!vis[i]) {
            total = 0;
            groups.push_back(pb);
            dfs(i);
            int t = groups[groups.size() - 1].size();
            if (t * (t - 1) != total) ok = false;
        }
    }
    if (!ok) return 0;

    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (p[i][j] == 2) {
                adj[i].push_back(j);
                adj[j].push_back(i);
            }
        }
    }
    unordered_set<int> rep;
    for (auto u: groups) {
        rep.insert(u[0]);
    }
    vector<vector<int>> concomp;
    for (int i = 0; i < n; i++) vis[i] = false;
    for (auto u: groups) {
        if (!vis[u[0]]) {
            vis[u[0]] = true;
            concomp.push_back(pb);
            concomp[concomp.size() - 1].push_back(u[0]);
            for (auto v: adj[u[0]]) {
                if (!vis[v]) {
                    if (rep.find(v) != rep.end()) concomp[concomp.size() - 1].push_back(v);
                    vis[v] = true;
                }
            }
        }
    }
    ok = true;
    for (int i = 0; i < concomp.size(); i++) {
        if (concomp[i].size() == 2) ok = false;
    }
    for (int i = 0; i < concomp.size(); i++) {
        unordered_set<int> curcomp;
        for (int j = 0; j < concomp[i].size(); j++) {
            for (auto u: groups[group[concomp[i][j]]]) {
                curcomp.insert(u);
            }
        }
        for (auto u: curcomp) {
            for (int i = 0; i < n; i++) {
                if (curcomp.find(i) != curcomp.end()) if (p[u][i] == 0) ok = false;
                if (curcomp.find(i) == curcomp.end()) if (p[u][i] != 0) ok = false;
            }
        }
    }
    if (!ok) {
        // printf("first\n");
        return 0;
    }

	vector<vector<int>> answer(n, vector<int>(n, 0));
	for (int i = 0; i < groups.size(); i++) {
        for (int j = 1; j < groups[i].size(); j++) {
            answer[groups[i][0]][groups[i][j]] = 1;
            answer[groups[i][j]][groups[i][0]] = 1;
        }
    }
    for (int i = 0; i < concomp.size(); i++) {
        if (concomp[i].size() != 1) {
            for (int j = 0; j < concomp[i].size() - 1; j++) {
                answer[concomp[i][j]][concomp[i][j+1]] = 1;
                answer[concomp[i][j+1]][concomp[i][j]] = 1;
            }
            answer[concomp[i][concomp[i].size() - 1]][concomp[i][0]] = 1;
            answer[concomp[i][0]][concomp[i][concomp[i].size() - 1]] = 1;
        }
    }

	build(answer);
	return 1;
}

// int main() {
//     int n;
//     scanf("%d", &n);
//     vector<vector<int>> p(n, vector<int>(n));
//     for (int i = 0; i < n; i++) {
//         for (int j = 0; j < n; j++) {
//             scanf("%d", &p[i][j]);
//         }
//     }
//     printf("Return value: %d\n", construct(p));
//     return 0;
// }

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:89:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |     for (int i = 0; i < concomp.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~~
supertrees.cpp:92:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |     for (int i = 0; i < concomp.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~~
supertrees.cpp:94:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |         for (int j = 0; j < concomp[i].size(); j++) {
      |                         ~~^~~~~~~~~~~~~~~~~~~
supertrees.cpp:112:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |  for (int i = 0; i < groups.size(); i++) {
      |                  ~~^~~~~~~~~~~~~~~
supertrees.cpp:113:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |         for (int j = 1; j < groups[i].size(); j++) {
      |                         ~~^~~~~~~~~~~~~~~~~~
supertrees.cpp:118:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  118 |     for (int i = 0; i < concomp.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~~
supertrees.cpp:120:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |             for (int j = 0; j < concomp[i].size() - 1; j++) {
      |                             ~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 292 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 288 KB Output is correct
6 Correct 12 ms 1492 KB Output is correct
7 Correct 244 ms 28068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 292 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 288 KB Output is correct
6 Correct 12 ms 1492 KB Output is correct
7 Correct 244 ms 28068 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 284 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 10 ms 1224 KB Output is correct
13 Correct 223 ms 24116 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 288 KB Output is correct
16 Correct 5 ms 816 KB Output is correct
17 Correct 92 ms 12964 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 60 ms 6344 KB Output is correct
21 Correct 243 ms 24636 KB Output is correct
22 Correct 246 ms 24064 KB Output is correct
23 Correct 225 ms 26032 KB Output is correct
24 Correct 209 ms 24168 KB Output is correct
25 Correct 106 ms 10728 KB Output is correct
26 Correct 82 ms 10212 KB Output is correct
27 Correct 248 ms 27236 KB Output is correct
28 Correct 203 ms 24176 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 0 ms 292 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 292 KB Output is correct
8 Correct 10 ms 1268 KB Output is correct
9 Correct 223 ms 24084 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 1440 KB Output is correct
13 Correct 196 ms 28164 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 5 ms 784 KB Output is correct
17 Correct 116 ms 13028 KB Output is correct
18 Correct 0 ms 284 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 288 KB Output is correct
21 Correct 54 ms 6336 KB Output is correct
22 Correct 230 ms 24672 KB Output is correct
23 Correct 206 ms 24168 KB Output is correct
24 Correct 281 ms 27216 KB Output is correct
25 Correct 105 ms 10252 KB Output is correct
26 Correct 122 ms 10728 KB Output is correct
27 Correct 211 ms 24252 KB Output is correct
28 Correct 212 ms 26148 KB Output is correct
29 Correct 100 ms 10280 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 1 ms 212 KB Output is correct
4 Correct 52 ms 6264 KB Output is correct
5 Correct 225 ms 24552 KB Output is correct
6 Correct 223 ms 24036 KB Output is correct
7 Correct 232 ms 26092 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 52 ms 6264 KB Output is correct
10 Correct 207 ms 24676 KB Output is correct
11 Correct 211 ms 24236 KB Output is correct
12 Correct 211 ms 27252 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 288 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 53 ms 6284 KB Output is correct
17 Correct 212 ms 24588 KB Output is correct
18 Correct 212 ms 24684 KB Output is correct
19 Correct 220 ms 24292 KB Output is correct
20 Correct 202 ms 24256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 292 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 288 KB Output is correct
6 Correct 12 ms 1492 KB Output is correct
7 Correct 244 ms 28068 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 284 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 10 ms 1224 KB Output is correct
13 Correct 223 ms 24116 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 288 KB Output is correct
16 Correct 5 ms 816 KB Output is correct
17 Correct 92 ms 12964 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 60 ms 6344 KB Output is correct
21 Correct 243 ms 24636 KB Output is correct
22 Correct 246 ms 24064 KB Output is correct
23 Correct 225 ms 26032 KB Output is correct
24 Correct 209 ms 24168 KB Output is correct
25 Correct 106 ms 10728 KB Output is correct
26 Correct 82 ms 10212 KB Output is correct
27 Correct 248 ms 27236 KB Output is correct
28 Correct 203 ms 24176 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 0 ms 292 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 292 KB Output is correct
36 Correct 10 ms 1268 KB Output is correct
37 Correct 223 ms 24084 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 8 ms 1440 KB Output is correct
41 Correct 196 ms 28164 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 5 ms 784 KB Output is correct
45 Correct 116 ms 13028 KB Output is correct
46 Correct 0 ms 284 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 288 KB Output is correct
49 Correct 54 ms 6336 KB Output is correct
50 Correct 230 ms 24672 KB Output is correct
51 Correct 206 ms 24168 KB Output is correct
52 Correct 281 ms 27216 KB Output is correct
53 Correct 105 ms 10252 KB Output is correct
54 Correct 122 ms 10728 KB Output is correct
55 Correct 211 ms 24252 KB Output is correct
56 Correct 212 ms 26148 KB Output is correct
57 Correct 100 ms 10280 KB Output is correct
58 Correct 1 ms 288 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 5 ms 724 KB Output is correct
61 Correct 89 ms 12444 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 56 ms 6348 KB Output is correct
66 Correct 118 ms 10852 KB Output is correct
67 Correct 128 ms 10196 KB Output is correct
68 Correct 139 ms 10488 KB Output is correct
69 Correct 121 ms 10348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 292 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 288 KB Output is correct
6 Correct 12 ms 1492 KB Output is correct
7 Correct 244 ms 28068 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 284 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 10 ms 1224 KB Output is correct
13 Correct 223 ms 24116 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 288 KB Output is correct
16 Correct 5 ms 816 KB Output is correct
17 Correct 92 ms 12964 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 60 ms 6344 KB Output is correct
21 Correct 243 ms 24636 KB Output is correct
22 Correct 246 ms 24064 KB Output is correct
23 Correct 225 ms 26032 KB Output is correct
24 Correct 209 ms 24168 KB Output is correct
25 Correct 106 ms 10728 KB Output is correct
26 Correct 82 ms 10212 KB Output is correct
27 Correct 248 ms 27236 KB Output is correct
28 Correct 203 ms 24176 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 0 ms 292 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 292 KB Output is correct
36 Correct 10 ms 1268 KB Output is correct
37 Correct 223 ms 24084 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 8 ms 1440 KB Output is correct
41 Correct 196 ms 28164 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 5 ms 784 KB Output is correct
45 Correct 116 ms 13028 KB Output is correct
46 Correct 0 ms 284 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 288 KB Output is correct
49 Correct 54 ms 6336 KB Output is correct
50 Correct 230 ms 24672 KB Output is correct
51 Correct 206 ms 24168 KB Output is correct
52 Correct 281 ms 27216 KB Output is correct
53 Correct 105 ms 10252 KB Output is correct
54 Correct 122 ms 10728 KB Output is correct
55 Correct 211 ms 24252 KB Output is correct
56 Correct 212 ms 26148 KB Output is correct
57 Correct 100 ms 10280 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 52 ms 6264 KB Output is correct
62 Correct 225 ms 24552 KB Output is correct
63 Correct 223 ms 24036 KB Output is correct
64 Correct 232 ms 26092 KB Output is correct
65 Correct 0 ms 212 KB Output is correct
66 Correct 52 ms 6264 KB Output is correct
67 Correct 207 ms 24676 KB Output is correct
68 Correct 211 ms 24236 KB Output is correct
69 Correct 211 ms 27252 KB Output is correct
70 Correct 0 ms 212 KB Output is correct
71 Correct 0 ms 288 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 53 ms 6284 KB Output is correct
74 Correct 212 ms 24588 KB Output is correct
75 Correct 212 ms 24684 KB Output is correct
76 Correct 220 ms 24292 KB Output is correct
77 Correct 202 ms 24256 KB Output is correct
78 Correct 1 ms 288 KB Output is correct
79 Correct 1 ms 212 KB Output is correct
80 Correct 5 ms 724 KB Output is correct
81 Correct 89 ms 12444 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 56 ms 6348 KB Output is correct
86 Correct 118 ms 10852 KB Output is correct
87 Correct 128 ms 10196 KB Output is correct
88 Correct 139 ms 10488 KB Output is correct
89 Correct 121 ms 10348 KB Output is correct
90 Correct 1 ms 212 KB Output is correct
91 Correct 1 ms 212 KB Output is correct
92 Correct 4 ms 780 KB Output is correct
93 Correct 87 ms 11764 KB Output is correct
94 Correct 1 ms 288 KB Output is correct
95 Correct 1 ms 284 KB Output is correct
96 Correct 1 ms 212 KB Output is correct
97 Correct 29 ms 2796 KB Output is correct
98 Correct 113 ms 10700 KB Output is correct
99 Correct 118 ms 10732 KB Output is correct
100 Correct 119 ms 10480 KB Output is correct
101 Correct 109 ms 10344 KB Output is correct
102 Correct 121 ms 10264 KB Output is correct
103 Correct 154 ms 13032 KB Output is correct
104 Correct 112 ms 12988 KB Output is correct
105 Correct 120 ms 10340 KB Output is correct