Submission #760586

# Submission time Handle Problem Language Result Execution time Memory
760586 2023-06-17T21:46:08 Z raysh07 Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
159 ms 27164 KB
#include "supertrees.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;

const int N = 1001;
int root[N];
vector <int> adj[N];
bool vis[N];
vector <int> comp;

int find(int x){
    if (root[x] == x) return x;
    return root[x] = find(root[x]);
}

void unite(int x, int y){
    x = find(x); y = find(y);
    
    if (x == y) return;
    root[x] = y;
}

void dfs(int u){
    vis[u] = true;
    comp.push_back(u);
    
    for (auto v : adj[u]){
        if (!vis[v]) dfs(v);
    }
}

int construct(vector<vector<int>> p) {
	int n = p.size();
	vector<vector<int>> ans(n, vector<int>(n, 0));
	
	for (int i = 0; i < n; i++) root[i] = i;
	
	for (int i = 0; i < n; i++){
	    for (int j = i + 1; j < n; j++){
	        if (p[i][j] == 1) unite(i, j);
	        if (p[i][j] == 3) return 0;
	    }
	}
	
	for (int i = 0; i < n; i++){
	    for (int j = i + 1; j < n; j++){
	        if (find(i) == find(j) && p[i][j] == 0) return 0;
	    }
	}
	
	for (int i = 0; i < n; i++){
	    if (find(i) != i){
	        ans[i][find(i)] = ans[find(i)][i] = 1;
	    }
	}
	
	for (int i = 0; i < n; i++){
	    for (int j = i + 1; j < n; j++){
	        if (p[i][j] == 2 && find(i) == i && find(j) == j) {
	            adj[i].push_back(j);
	            adj[j].push_back(i);
	        }
	    }
	}
	
	for (int i = 0; i < n; i++){
	    if (vis[i] || find(i) != i) continue;
	    
	    comp.clear();
	    dfs(i);
	    
	    if (comp.size() == 2) return 0;
	    
	    for (auto x : comp){
	        for (auto y : comp){
	            if (x != y && p[x][y] != 2){
	                return 0;
	            }
	        }
	    }
	    
	    for (int j = 0; j < comp.size() - 1; j++){
	        ans[comp[j]][comp[j + 1]] = ans[comp[j + 1]][comp[j]] = 1;
	    }
	    if (comp.size() != 1)
	    ans[comp[0]][comp.back()] = ans[comp.back()][comp[0]] = 1;
	}
	build(ans);
	return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:83:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |      for (int j = 0; j < comp.size() - 1; j++){
      |                      ~~^~~~~~~~~~~~~~~~~
# 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 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 6 ms 1108 KB Output is correct
7 Correct 156 ms 22048 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 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 6 ms 1108 KB Output is correct
7 Correct 156 ms 22048 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 6 ms 1124 KB Output is correct
13 Correct 150 ms 21972 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 748 KB Output is correct
17 Correct 69 ms 12060 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 38 ms 5788 KB Output is correct
21 Correct 149 ms 22060 KB Output is correct
22 Correct 154 ms 22032 KB Output is correct
23 Correct 153 ms 22048 KB Output is correct
24 Correct 143 ms 22052 KB Output is correct
25 Correct 64 ms 12064 KB Output is correct
26 Correct 63 ms 12064 KB Output is correct
27 Correct 154 ms 22060 KB Output is correct
28 Correct 153 ms 21964 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 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 1 ms 212 KB Output is correct
8 Correct 6 ms 1108 KB Output is correct
9 Correct 154 ms 22020 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 1436 KB Output is correct
13 Correct 158 ms 26148 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 980 KB Output is correct
17 Correct 77 ms 16920 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 324 KB Output is correct
21 Correct 38 ms 6308 KB Output is correct
22 Correct 151 ms 24508 KB Output is correct
23 Correct 153 ms 23972 KB Output is correct
24 Correct 157 ms 27164 KB Output is correct
25 Correct 66 ms 14112 KB Output is correct
26 Correct 67 ms 14616 KB Output is correct
27 Correct 150 ms 23964 KB Output is correct
28 Correct 158 ms 26064 KB Output is correct
29 Correct 66 ms 14092 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 41 ms 5692 KB Output is correct
5 Correct 152 ms 22044 KB Output is correct
6 Correct 150 ms 22052 KB Output is correct
7 Correct 159 ms 22048 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 41 ms 5792 KB Output is correct
10 Correct 152 ms 22576 KB Output is correct
11 Correct 154 ms 22036 KB Output is correct
12 Correct 157 ms 25116 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 38 ms 5776 KB Output is correct
17 Correct 152 ms 22564 KB Output is correct
18 Correct 150 ms 22220 KB Output is correct
19 Correct 159 ms 22176 KB Output is correct
20 Correct 150 ms 22016 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 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 6 ms 1108 KB Output is correct
7 Correct 156 ms 22048 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 6 ms 1124 KB Output is correct
13 Correct 150 ms 21972 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 748 KB Output is correct
17 Correct 69 ms 12060 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 38 ms 5788 KB Output is correct
21 Correct 149 ms 22060 KB Output is correct
22 Correct 154 ms 22032 KB Output is correct
23 Correct 153 ms 22048 KB Output is correct
24 Correct 143 ms 22052 KB Output is correct
25 Correct 64 ms 12064 KB Output is correct
26 Correct 63 ms 12064 KB Output is correct
27 Correct 154 ms 22060 KB Output is correct
28 Correct 153 ms 21964 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 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 1 ms 212 KB Output is correct
36 Correct 6 ms 1108 KB Output is correct
37 Correct 154 ms 22020 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 1436 KB Output is correct
41 Correct 158 ms 26148 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 4 ms 980 KB Output is correct
45 Correct 77 ms 16920 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 1 ms 324 KB Output is correct
49 Correct 38 ms 6308 KB Output is correct
50 Correct 151 ms 24508 KB Output is correct
51 Correct 153 ms 23972 KB Output is correct
52 Correct 157 ms 27164 KB Output is correct
53 Correct 66 ms 14112 KB Output is correct
54 Correct 67 ms 14616 KB Output is correct
55 Correct 150 ms 23964 KB Output is correct
56 Correct 158 ms 26064 KB Output is correct
57 Correct 66 ms 14092 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 3 ms 852 KB Output is correct
61 Correct 69 ms 14004 KB Output is correct
62 Correct 0 ms 212 KB Output is correct
63 Correct 0 ms 328 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Correct 38 ms 6284 KB Output is correct
66 Correct 68 ms 14612 KB Output is correct
67 Correct 66 ms 14092 KB Output is correct
68 Correct 66 ms 14156 KB Output is correct
69 Correct 66 ms 14096 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 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 6 ms 1108 KB Output is correct
7 Correct 156 ms 22048 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 6 ms 1124 KB Output is correct
13 Correct 150 ms 21972 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 748 KB Output is correct
17 Correct 69 ms 12060 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 38 ms 5788 KB Output is correct
21 Correct 149 ms 22060 KB Output is correct
22 Correct 154 ms 22032 KB Output is correct
23 Correct 153 ms 22048 KB Output is correct
24 Correct 143 ms 22052 KB Output is correct
25 Correct 64 ms 12064 KB Output is correct
26 Correct 63 ms 12064 KB Output is correct
27 Correct 154 ms 22060 KB Output is correct
28 Correct 153 ms 21964 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 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 1 ms 212 KB Output is correct
36 Correct 6 ms 1108 KB Output is correct
37 Correct 154 ms 22020 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 1436 KB Output is correct
41 Correct 158 ms 26148 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 4 ms 980 KB Output is correct
45 Correct 77 ms 16920 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 1 ms 324 KB Output is correct
49 Correct 38 ms 6308 KB Output is correct
50 Correct 151 ms 24508 KB Output is correct
51 Correct 153 ms 23972 KB Output is correct
52 Correct 157 ms 27164 KB Output is correct
53 Correct 66 ms 14112 KB Output is correct
54 Correct 67 ms 14616 KB Output is correct
55 Correct 150 ms 23964 KB Output is correct
56 Correct 158 ms 26064 KB Output is correct
57 Correct 66 ms 14092 KB Output is correct
58 Correct 0 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 41 ms 5692 KB Output is correct
62 Correct 152 ms 22044 KB Output is correct
63 Correct 150 ms 22052 KB Output is correct
64 Correct 159 ms 22048 KB Output is correct
65 Correct 0 ms 212 KB Output is correct
66 Correct 41 ms 5792 KB Output is correct
67 Correct 152 ms 22576 KB Output is correct
68 Correct 154 ms 22036 KB Output is correct
69 Correct 157 ms 25116 KB Output is correct
70 Correct 1 ms 212 KB Output is correct
71 Correct 0 ms 212 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 38 ms 5776 KB Output is correct
74 Correct 152 ms 22564 KB Output is correct
75 Correct 150 ms 22220 KB Output is correct
76 Correct 159 ms 22176 KB Output is correct
77 Correct 150 ms 22016 KB Output is correct
78 Correct 1 ms 212 KB Output is correct
79 Correct 0 ms 212 KB Output is correct
80 Correct 3 ms 852 KB Output is correct
81 Correct 69 ms 14004 KB Output is correct
82 Correct 0 ms 212 KB Output is correct
83 Correct 0 ms 328 KB Output is correct
84 Correct 1 ms 212 KB Output is correct
85 Correct 38 ms 6284 KB Output is correct
86 Correct 68 ms 14612 KB Output is correct
87 Correct 66 ms 14092 KB Output is correct
88 Correct 66 ms 14156 KB Output is correct
89 Correct 66 ms 14096 KB Output is correct
90 Correct 1 ms 328 KB Output is correct
91 Correct 0 ms 212 KB Output is correct
92 Correct 3 ms 852 KB Output is correct
93 Correct 66 ms 14096 KB Output is correct
94 Correct 1 ms 212 KB Output is correct
95 Correct 1 ms 212 KB Output is correct
96 Correct 1 ms 212 KB Output is correct
97 Correct 16 ms 3736 KB Output is correct
98 Correct 65 ms 14112 KB Output is correct
99 Correct 64 ms 14108 KB Output is correct
100 Correct 64 ms 14092 KB Output is correct
101 Correct 70 ms 14096 KB Output is correct
102 Correct 64 ms 14092 KB Output is correct
103 Correct 65 ms 14104 KB Output is correct
104 Correct 65 ms 14092 KB Output is correct
105 Correct 67 ms 14092 KB Output is correct