Submission #609650

# Submission time Handle Problem Language Result Execution time Memory
609650 2022-07-27T18:48:49 Z Tigryonochekk Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
359 ms 79000 KB
#include <iostream>
#include "supertrees.h"
#include <vector>
#include <set>
using namespace std;
const int N = 1002;

vector<vector<int>> p;
vector<vector<int>> answer;
set<int> dsu[N];
vector<int> ds[N];
int com[N];

void dfs(int v) {
	for (auto to : dsu[v]) {
		if (com[to]) continue;
		com[to] = com[v];
		dfs(to);
	}
}

void connect(int a, int b) {
	if (a == b) return;
	answer[a][b] = 1;
	answer[b][a] = 1;
}

bool solve(vector<int> v) {
	int n = v.size();
	vector<bool> f(n);
	fill(f.begin(), f.end(), true);
	for (int i = 0; i < n; i++) {
		for (int j = i + 1; j < n; j++) {
			if (p[v[i]][v[j]] == 1) {
				f[i] = 0;
				f[j] = 0;
			}
		}
	}
	vector<int> u(n);
	int c = 0;
	int first = -1, last = -1;
	for (int i = 0; i < n; i++) {
		if (f[i]) {
			u[i] = c;
			if (first < 0) {
				first = i;
				last = i;
			}
			connect(v[last], v[i]);
			last = i;
		}
	}
	for (int i = 0; i < n; i++) {
		if (!f[i] && !u[i]) {
			u[i] = ++c;
			if (first < 0) {
				first = i;
				last = i;
			}
			connect(v[last], v[i]);
			last = i;
			for (int j = 0; j < n; j++) {
				if (j == i) continue;
				if (p[v[i]][v[j]] == 1) {
					connect(v[i], v[j]);
					u[j] = c;
				}
			}
		}
	}
	if (answer[v[last]][v[first]]) return 0;
	connect(v[last], v[first]);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (p[v[i]][v[j]] == 1 && u[i] != u[j]) 
				return 0;
			if(p[v[i]][v[j]] == 2 && (u[i] && u[i] == u[j]))
				return 0;
		}
	}
	return 1;
}

int construct(vector<vector<int>> pp) {
	p = pp;
	int n = p.size();
	answer.resize(n);
	for (int i = 0; i < n; i++) {
		answer[i].resize(n);
	}
	bool noBitches = false; //suta
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (j == i) continue;
			if (p[i][j]) {
				dsu[i].insert(j);
				ds[i].push_back(j);
			}
			if (p[i][j] == 3) {
				noBitches = true;
			}
		}
	}
	if (noBitches) {
		return 0;
	}
	int k = 1;
	for (int i = 0; i < n; i++) {
		if (!com[i]) {
			com[i] = k++;
			dfs(i);
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (j == i) continue;
			if (com[i] == com[j] && dsu[i].find(j) == dsu[i].end()) return 0;
			if (com[i] != com[j] && dsu[i].find(j) != dsu[i].end()) return 0;
		}
	}
	for (int i = 0; i < n; i++) {
		if (com[i]) {
			if (dsu[i].size() == 0) continue;
			vector<int> c;
			c.push_back(i);
			com[i] = 0;
			for (int j : dsu[i]) {
				c.push_back(j);
				com[j] = 0;
			}
			if (!solve(c))
				return 0;
		}
	}
	build(answer);
	return 1;
}

/*
3 
1 0 0 
0 1 1 
0 1 1 

4 
1 0 0 0 
0 1 2 2 
0 2 1 2 
0 2 2 1 

3 
1 0 0 
0 1 2 
0 2 1 

4 
1 1 2 2 
1 1 2 2 
2 2 1 2 
2 2 2 1 

8  
1 2 2 2 2 2 0 0 
2 1 2 2 2 2 0 0 
2 2 1 2 1 2 0 0 
2 2 2 1 2 1 0 0 
2 2 1 2 1 2 0 0 
2 2 2 1 2 1 0 0 
0 0 0 0 0 0 1 1 
0 0 0 0 0 0 1 1 
*/
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 376 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 13 ms 3540 KB Output is correct
7 Correct 359 ms 79000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 376 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 13 ms 3540 KB Output is correct
7 Correct 359 ms 79000 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 376 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 7 ms 1508 KB Output is correct
13 Correct 192 ms 27988 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 7 ms 2016 KB Output is correct
17 Correct 166 ms 44000 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 0 ms 372 KB Output is correct
20 Correct 49 ms 7624 KB Output is correct
21 Correct 222 ms 33332 KB Output is correct
22 Correct 177 ms 28140 KB Output is correct
23 Correct 290 ms 53440 KB Output is correct
24 Correct 194 ms 28096 KB Output is correct
25 Correct 91 ms 23244 KB Output is correct
26 Correct 76 ms 18116 KB Output is correct
27 Correct 320 ms 54476 KB Output is correct
28 Correct 170 ms 28100 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 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 8 ms 1492 KB Output is correct
9 Correct 178 ms 28024 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 14 ms 3516 KB Output is correct
13 Correct 352 ms 78980 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 6 ms 2132 KB Output is correct
17 Correct 169 ms 44120 KB Output is correct
18 Correct 1 ms 372 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 380 KB Output is correct
21 Correct 49 ms 7620 KB Output is correct
22 Correct 208 ms 33112 KB Output is correct
23 Correct 178 ms 28252 KB Output is correct
24 Correct 292 ms 54488 KB Output is correct
25 Correct 69 ms 18120 KB Output is correct
26 Correct 83 ms 23248 KB Output is correct
27 Correct 180 ms 28284 KB Output is correct
28 Correct 288 ms 53424 KB Output is correct
29 Correct 69 ms 18120 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 46 ms 7612 KB Output is correct
5 Correct 208 ms 33236 KB Output is correct
6 Correct 189 ms 28108 KB Output is correct
7 Correct 302 ms 53456 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 49 ms 7716 KB Output is correct
10 Correct 202 ms 33148 KB Output is correct
11 Correct 178 ms 28232 KB Output is correct
12 Correct 287 ms 54516 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 45 ms 7472 KB Output is correct
17 Correct 201 ms 33220 KB Output is correct
18 Correct 203 ms 33136 KB Output is correct
19 Correct 190 ms 29760 KB Output is correct
20 Correct 171 ms 28040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 376 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 13 ms 3540 KB Output is correct
7 Correct 359 ms 79000 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 376 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 7 ms 1508 KB Output is correct
13 Correct 192 ms 27988 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 7 ms 2016 KB Output is correct
17 Correct 166 ms 44000 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 0 ms 372 KB Output is correct
20 Correct 49 ms 7624 KB Output is correct
21 Correct 222 ms 33332 KB Output is correct
22 Correct 177 ms 28140 KB Output is correct
23 Correct 290 ms 53440 KB Output is correct
24 Correct 194 ms 28096 KB Output is correct
25 Correct 91 ms 23244 KB Output is correct
26 Correct 76 ms 18116 KB Output is correct
27 Correct 320 ms 54476 KB Output is correct
28 Correct 170 ms 28100 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 0 ms 340 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 8 ms 1492 KB Output is correct
37 Correct 178 ms 28024 KB Output is correct
38 Correct 0 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 14 ms 3516 KB Output is correct
41 Correct 352 ms 78980 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 6 ms 2132 KB Output is correct
45 Correct 169 ms 44120 KB Output is correct
46 Correct 1 ms 372 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 380 KB Output is correct
49 Correct 49 ms 7620 KB Output is correct
50 Correct 208 ms 33112 KB Output is correct
51 Correct 178 ms 28252 KB Output is correct
52 Correct 292 ms 54488 KB Output is correct
53 Correct 69 ms 18120 KB Output is correct
54 Correct 83 ms 23248 KB Output is correct
55 Correct 180 ms 28284 KB Output is correct
56 Correct 288 ms 53424 KB Output is correct
57 Correct 69 ms 18120 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 7 ms 2516 KB Output is correct
61 Correct 183 ms 53456 KB Output is correct
62 Correct 1 ms 340 KB Output is correct
63 Correct 1 ms 340 KB Output is correct
64 Correct 1 ms 340 KB Output is correct
65 Correct 45 ms 7432 KB Output is correct
66 Correct 88 ms 23368 KB Output is correct
67 Correct 95 ms 23308 KB Output is correct
68 Correct 74 ms 19792 KB Output is correct
69 Correct 72 ms 18112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 376 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 13 ms 3540 KB Output is correct
7 Correct 359 ms 79000 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 376 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 7 ms 1508 KB Output is correct
13 Correct 192 ms 27988 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 7 ms 2016 KB Output is correct
17 Correct 166 ms 44000 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 0 ms 372 KB Output is correct
20 Correct 49 ms 7624 KB Output is correct
21 Correct 222 ms 33332 KB Output is correct
22 Correct 177 ms 28140 KB Output is correct
23 Correct 290 ms 53440 KB Output is correct
24 Correct 194 ms 28096 KB Output is correct
25 Correct 91 ms 23244 KB Output is correct
26 Correct 76 ms 18116 KB Output is correct
27 Correct 320 ms 54476 KB Output is correct
28 Correct 170 ms 28100 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 0 ms 340 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 8 ms 1492 KB Output is correct
37 Correct 178 ms 28024 KB Output is correct
38 Correct 0 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 14 ms 3516 KB Output is correct
41 Correct 352 ms 78980 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 6 ms 2132 KB Output is correct
45 Correct 169 ms 44120 KB Output is correct
46 Correct 1 ms 372 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 380 KB Output is correct
49 Correct 49 ms 7620 KB Output is correct
50 Correct 208 ms 33112 KB Output is correct
51 Correct 178 ms 28252 KB Output is correct
52 Correct 292 ms 54488 KB Output is correct
53 Correct 69 ms 18120 KB Output is correct
54 Correct 83 ms 23248 KB Output is correct
55 Correct 180 ms 28284 KB Output is correct
56 Correct 288 ms 53424 KB Output is correct
57 Correct 69 ms 18120 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 46 ms 7612 KB Output is correct
62 Correct 208 ms 33236 KB Output is correct
63 Correct 189 ms 28108 KB Output is correct
64 Correct 302 ms 53456 KB Output is correct
65 Correct 0 ms 340 KB Output is correct
66 Correct 49 ms 7716 KB Output is correct
67 Correct 202 ms 33148 KB Output is correct
68 Correct 178 ms 28232 KB Output is correct
69 Correct 287 ms 54516 KB Output is correct
70 Correct 1 ms 340 KB Output is correct
71 Correct 0 ms 340 KB Output is correct
72 Correct 1 ms 340 KB Output is correct
73 Correct 45 ms 7472 KB Output is correct
74 Correct 201 ms 33220 KB Output is correct
75 Correct 203 ms 33136 KB Output is correct
76 Correct 190 ms 29760 KB Output is correct
77 Correct 171 ms 28040 KB Output is correct
78 Correct 1 ms 340 KB Output is correct
79 Correct 1 ms 340 KB Output is correct
80 Correct 7 ms 2516 KB Output is correct
81 Correct 183 ms 53456 KB Output is correct
82 Correct 1 ms 340 KB Output is correct
83 Correct 1 ms 340 KB Output is correct
84 Correct 1 ms 340 KB Output is correct
85 Correct 45 ms 7432 KB Output is correct
86 Correct 88 ms 23368 KB Output is correct
87 Correct 95 ms 23308 KB Output is correct
88 Correct 74 ms 19792 KB Output is correct
89 Correct 72 ms 18112 KB Output is correct
90 Correct 1 ms 376 KB Output is correct
91 Correct 1 ms 340 KB Output is correct
92 Correct 8 ms 2664 KB Output is correct
93 Correct 206 ms 57272 KB Output is correct
94 Correct 0 ms 340 KB Output is correct
95 Correct 0 ms 340 KB Output is correct
96 Correct 0 ms 468 KB Output is correct
97 Correct 18 ms 4940 KB Output is correct
98 Correct 91 ms 23236 KB Output is correct
99 Correct 83 ms 23244 KB Output is correct
100 Correct 72 ms 19792 KB Output is correct
101 Correct 72 ms 18120 KB Output is correct
102 Correct 207 ms 68948 KB Output is correct
103 Correct 208 ms 69112 KB Output is correct
104 Correct 206 ms 69068 KB Output is correct
105 Correct 155 ms 43976 KB Output is correct