Submission #541847

# Submission time Handle Problem Language Result Execution time Memory
541847 2022-03-24T15:22:25 Z LucaDantas Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
208 ms 24204 KB
#include "supertrees.h"
#include <vector>
using namespace std;

constexpr int maxn = 1010;

vector<vector<int>> ans;
int n;

struct DSU {
	int pai[maxn], peso[maxn];
	DSU() { for(int i = 0; i < maxn; i++) pai[i] = i, peso[i] = 1; }
	void init(const vector<int>& v) { for(const int& x : v) pai[x] = x, peso[x] = 1; }
	int find(int x) { return pai[x] == x ? x : pai[x] = find(pai[x]); }
	void join(int a, int b, bool ans_yes) {
		a = find(a), b = find(b);
		if(a == b) return;
		if(peso[a] < peso[b])
			swap(a, b);
		if(ans_yes)
			ans[a][b] = ans[b][a] = 1;
		pai[b] = a;
		peso[a] += peso[b];
	}
} dsu;

vector<int> comp[maxn], comp2[maxn];

int connected_component(const vector<int>& ativos, const std::vector<std::vector<int>>& p) {
	dsu.init(ativos);
	for(int i : ativos) {
		for(int j : ativos) {
			if(p[i][j] == 1) dsu.join(i, j, 1);
		}
	}

	vector<int> chains;
	for(int i : ativos) {
		if(dsu.find(i) == i) chains.push_back(i);
		comp2[dsu.find(i)].push_back(i);
	}

	for(int i : ativos)
		for(int x : comp2[i])
			for(int y : comp2[i])
				if(p[x][y] != 1) return 0;
	
	if(chains.size() == 1) return 1;
	if(chains.size() == 2) return 0;
	// senao tenho q fazer o ciclo
	int last = chains.back();
	for(int x : chains)
		ans[x][last] = ans[last][x] = 1, last = x; // junto todas as cabecas em um ciclo

	return 1;
}

int construct(std::vector<std::vector<int>> p) {
	n = p.size();
	ans.assign(n, vector<int>(n, 0));

	for(int i = 0; i < n; i++) {
		for(int j = 0; j < n; j++) {
			if(p[i][j] != 0) dsu.join(i, j, 0);
			if(p[i][j] == 3) return 0;
		}
	}

	for(int i = 0; i < n; i++)
		comp[dsu.find(i)].push_back(i);

	for(int i = 0; i < n; i++) {
		for(int x : comp[i])
			for(int y : comp[i])
				if(p[x][y] == 0) return 0;
		if(comp[i].size() && connected_component(comp[i], p) == 0) return 0;
	}

	build(ans);
	return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 8 ms 1164 KB Output is correct
7 Correct 188 ms 22064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 8 ms 1164 KB Output is correct
7 Correct 188 ms 22064 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 8 ms 1288 KB Output is correct
13 Correct 184 ms 23444 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 4 ms 836 KB Output is correct
17 Correct 117 ms 13572 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 47 ms 6236 KB Output is correct
21 Correct 191 ms 23644 KB Output is correct
22 Correct 208 ms 23652 KB Output is correct
23 Correct 194 ms 23628 KB Output is correct
24 Correct 186 ms 23604 KB Output is correct
25 Correct 82 ms 14128 KB Output is correct
26 Correct 81 ms 14112 KB Output is correct
27 Correct 194 ms 23712 KB Output is correct
28 Correct 184 ms 23592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 336 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 10 ms 1284 KB Output is correct
9 Correct 179 ms 24100 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 8 ms 1356 KB Output is correct
13 Correct 185 ms 23976 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 5 ms 852 KB Output is correct
17 Correct 91 ms 14116 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 44 ms 6256 KB Output is correct
22 Correct 183 ms 23984 KB Output is correct
23 Correct 184 ms 23988 KB Output is correct
24 Correct 184 ms 23984 KB Output is correct
25 Correct 81 ms 14132 KB Output is correct
26 Correct 85 ms 14128 KB Output is correct
27 Correct 177 ms 23976 KB Output is correct
28 Correct 186 ms 24012 KB Output is correct
29 Correct 87 ms 14180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 45 ms 6268 KB Output is correct
5 Correct 189 ms 24168 KB Output is correct
6 Correct 180 ms 23968 KB Output is correct
7 Correct 188 ms 24056 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 44 ms 6304 KB Output is correct
10 Correct 179 ms 23968 KB Output is correct
11 Correct 179 ms 23972 KB Output is correct
12 Correct 181 ms 24028 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 48 ms 6312 KB Output is correct
17 Correct 183 ms 23972 KB Output is correct
18 Correct 185 ms 24204 KB Output is correct
19 Correct 180 ms 23972 KB Output is correct
20 Correct 182 ms 24076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 8 ms 1164 KB Output is correct
7 Correct 188 ms 22064 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 8 ms 1288 KB Output is correct
13 Correct 184 ms 23444 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 4 ms 836 KB Output is correct
17 Correct 117 ms 13572 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 47 ms 6236 KB Output is correct
21 Correct 191 ms 23644 KB Output is correct
22 Correct 208 ms 23652 KB Output is correct
23 Correct 194 ms 23628 KB Output is correct
24 Correct 186 ms 23604 KB Output is correct
25 Correct 82 ms 14128 KB Output is correct
26 Correct 81 ms 14112 KB Output is correct
27 Correct 194 ms 23712 KB Output is correct
28 Correct 184 ms 23592 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 0 ms 336 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 10 ms 1284 KB Output is correct
37 Correct 179 ms 24100 KB Output is correct
38 Correct 0 ms 340 KB Output is correct
39 Correct 0 ms 340 KB Output is correct
40 Correct 8 ms 1356 KB Output is correct
41 Correct 185 ms 23976 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 0 ms 344 KB Output is correct
44 Correct 5 ms 852 KB Output is correct
45 Correct 91 ms 14116 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 0 ms 340 KB Output is correct
49 Correct 44 ms 6256 KB Output is correct
50 Correct 183 ms 23984 KB Output is correct
51 Correct 184 ms 23988 KB Output is correct
52 Correct 184 ms 23984 KB Output is correct
53 Correct 81 ms 14132 KB Output is correct
54 Correct 85 ms 14128 KB Output is correct
55 Correct 177 ms 23976 KB Output is correct
56 Correct 186 ms 24012 KB Output is correct
57 Correct 87 ms 14180 KB Output is correct
58 Correct 0 ms 464 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 4 ms 852 KB Output is correct
61 Correct 89 ms 14116 KB Output is correct
62 Correct 1 ms 340 KB Output is correct
63 Correct 1 ms 348 KB Output is correct
64 Correct 1 ms 340 KB Output is correct
65 Correct 48 ms 6304 KB Output is correct
66 Correct 85 ms 14124 KB Output is correct
67 Correct 86 ms 14120 KB Output is correct
68 Correct 83 ms 14136 KB Output is correct
69 Correct 83 ms 14120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 8 ms 1164 KB Output is correct
7 Correct 188 ms 22064 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 8 ms 1288 KB Output is correct
13 Correct 184 ms 23444 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 4 ms 836 KB Output is correct
17 Correct 117 ms 13572 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 47 ms 6236 KB Output is correct
21 Correct 191 ms 23644 KB Output is correct
22 Correct 208 ms 23652 KB Output is correct
23 Correct 194 ms 23628 KB Output is correct
24 Correct 186 ms 23604 KB Output is correct
25 Correct 82 ms 14128 KB Output is correct
26 Correct 81 ms 14112 KB Output is correct
27 Correct 194 ms 23712 KB Output is correct
28 Correct 184 ms 23592 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 0 ms 336 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 10 ms 1284 KB Output is correct
37 Correct 179 ms 24100 KB Output is correct
38 Correct 0 ms 340 KB Output is correct
39 Correct 0 ms 340 KB Output is correct
40 Correct 8 ms 1356 KB Output is correct
41 Correct 185 ms 23976 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 0 ms 344 KB Output is correct
44 Correct 5 ms 852 KB Output is correct
45 Correct 91 ms 14116 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 0 ms 340 KB Output is correct
49 Correct 44 ms 6256 KB Output is correct
50 Correct 183 ms 23984 KB Output is correct
51 Correct 184 ms 23988 KB Output is correct
52 Correct 184 ms 23984 KB Output is correct
53 Correct 81 ms 14132 KB Output is correct
54 Correct 85 ms 14128 KB Output is correct
55 Correct 177 ms 23976 KB Output is correct
56 Correct 186 ms 24012 KB Output is correct
57 Correct 87 ms 14180 KB Output is correct
58 Correct 0 ms 340 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 45 ms 6268 KB Output is correct
62 Correct 189 ms 24168 KB Output is correct
63 Correct 180 ms 23968 KB Output is correct
64 Correct 188 ms 24056 KB Output is correct
65 Correct 0 ms 340 KB Output is correct
66 Correct 44 ms 6304 KB Output is correct
67 Correct 179 ms 23968 KB Output is correct
68 Correct 179 ms 23972 KB Output is correct
69 Correct 181 ms 24028 KB Output is correct
70 Correct 1 ms 340 KB Output is correct
71 Correct 1 ms 340 KB Output is correct
72 Correct 0 ms 340 KB Output is correct
73 Correct 48 ms 6312 KB Output is correct
74 Correct 183 ms 23972 KB Output is correct
75 Correct 185 ms 24204 KB Output is correct
76 Correct 180 ms 23972 KB Output is correct
77 Correct 182 ms 24076 KB Output is correct
78 Correct 0 ms 464 KB Output is correct
79 Correct 1 ms 340 KB Output is correct
80 Correct 4 ms 852 KB Output is correct
81 Correct 89 ms 14116 KB Output is correct
82 Correct 1 ms 340 KB Output is correct
83 Correct 1 ms 348 KB Output is correct
84 Correct 1 ms 340 KB Output is correct
85 Correct 48 ms 6304 KB Output is correct
86 Correct 85 ms 14124 KB Output is correct
87 Correct 86 ms 14120 KB Output is correct
88 Correct 83 ms 14136 KB Output is correct
89 Correct 83 ms 14120 KB Output is correct
90 Correct 1 ms 340 KB Output is correct
91 Correct 1 ms 340 KB Output is correct
92 Correct 4 ms 852 KB Output is correct
93 Correct 85 ms 14040 KB Output is correct
94 Correct 1 ms 340 KB Output is correct
95 Correct 0 ms 340 KB Output is correct
96 Correct 1 ms 340 KB Output is correct
97 Correct 21 ms 3752 KB Output is correct
98 Correct 83 ms 14120 KB Output is correct
99 Correct 82 ms 14084 KB Output is correct
100 Correct 83 ms 14116 KB Output is correct
101 Correct 82 ms 14112 KB Output is correct
102 Correct 79 ms 14120 KB Output is correct
103 Correct 80 ms 14028 KB Output is correct
104 Correct 82 ms 14128 KB Output is correct
105 Correct 89 ms 14116 KB Output is correct