Submission #616001

# Submission time Handle Problem Language Result Execution time Memory
616001 2022-07-31T17:50:53 Z yanndev Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
244 ms 30312 KB
#include "supertrees.h"
#include <vector>
#include <iostream>
using namespace std;

const int MX = 1042;

bool vis[MX];
bool vis2[MX];

int cycleId[MX];
int rep[MX];
int cycleRep[MX];
int compId[MX];

int nxtComp = 0;
int nxtCycle = 0;

vector<int> adj[MX];
vector<int> adj2[MX];

vector<int> comp[MX];
vector<int> comp2[MX];

void dfs(int node, int r, int c) {
	vis[node] = true;
	rep[node] = r;
	compId[node] = c;
	comp[c].push_back(node);

	for (auto& x: adj[node])
		if (!vis[x])
			dfs(x, r, c);
}

void dfs2(int node, int r, int c) {
	vis2[node] = true;
	cycleRep[node] = r;
	cycleId[node] = c;
	comp2[c].push_back(node);

	for (auto& x: adj2[node])
		if (!vis2[x])
			dfs2(x, r, c);
}

int construct(std::vector<std::vector<int>> p) {
	int n = p.size();
	
	vector<vector<int>> answer;

	for (int i = 0; i < n; i++) {
		vector<int> row;
		row.resize(n);
		answer.push_back(row);
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (p[i][j] == 3)
				return 0;
			if (p[i][j] == 1 && i != j) {
				adj[i].push_back(j);
				adj[j].push_back(i);
			}
		}
	}

	for (int i = 0; i < n; i++) {
		if (!vis[i]) {
			//cout << "dfs root " << i << '\n';
			dfs(i, i, nxtComp++);
		}
	}

	for (int i = 0; i < n; i++) {
		if (rep[i] == i) {
			// add cycles
			for (int j = 0; j < n; j++) {
				if (rep[j] != i) {
					for (auto& x: comp[compId[i]]) {
						if (p[j][x] == 2) {
							//cout << "fuke " << i << ' ' << rep[j] << '\n';
							adj2[i].push_back(rep[j]);
							adj2[rep[j]].push_back(i);
						}
					}
				}
			}

			// add chain
			for (int j = 0; j + 1 < (int)comp[compId[i]].size(); j++) {
				answer[comp[compId[i]][j]][comp[compId[i]][j + 1]] = answer[comp[compId[i]][j + 1]][comp[compId[i]][j]] = 1;
			}
		}
	}

	for (int i = 0; i < n; i++) {
		if (!vis2[i]) {
			dfs2(i, i, nxtCycle++);
		}
	}

	// add cycle edges
	for (int i = 0; i < n; i++) {
		if (rep[i] == i) {
			//cout << "comp of " << i << '\n';
			for (auto& x: comp[compId[i]]) {
				//cout << x << '\n';
				cycleId[x] = cycleId[i];
			}
		}

		if (cycleRep[i] == i) {
			int sz = (int)comp2[cycleId[i]].size();
			if (sz == 2)
				return 0;
			for (int j = 0; sz > 1 && j < sz; j++) {
				answer[comp2[cycleId[i]][j]][comp2[cycleId[i]][(j + 1) % sz]] = 1;
				answer[comp2[cycleId[i]][(j + 1) % sz]][comp2[cycleId[i]][j]] = 1;
				//cout << "cycle edge from " << comp2[cycleId[i]][j] << ' ' << comp2[cycleId[i]][j + 1] << '\n';
			}
		}
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (i != j) {
				if (p[i][j] == 0 && cycleId[i] == cycleId[j])
					return 0;
				if (p[i][j] == 1 && rep[i] != rep[j])
					return 0;
				if (p[i][j] == 2 && !((cycleId[i] == cycleId[j]) && (rep[i] != rep[j])))
					return 0;
			}
		}
	}

	build(answer);
	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 1 ms 340 KB Output is correct
6 Correct 9 ms 1748 KB Output is correct
7 Correct 187 ms 30312 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 1 ms 340 KB Output is correct
6 Correct 9 ms 1748 KB Output is correct
7 Correct 187 ms 30312 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 8 ms 1256 KB Output is correct
13 Correct 179 ms 22148 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 4 ms 1108 KB Output is correct
17 Correct 90 ms 17896 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 46 ms 5976 KB Output is correct
21 Correct 178 ms 23156 KB Output is correct
22 Correct 193 ms 22184 KB Output is correct
23 Correct 202 ms 26240 KB Output is correct
24 Correct 173 ms 22248 KB Output is correct
25 Correct 75 ms 13288 KB Output is correct
26 Correct 71 ms 12236 KB Output is correct
27 Correct 207 ms 28408 KB Output is correct
28 Correct 177 ms 22152 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 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 7 ms 1236 KB Output is correct
9 Correct 182 ms 22304 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 8 ms 1748 KB Output is correct
13 Correct 213 ms 30296 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 5 ms 1072 KB Output is correct
17 Correct 99 ms 17896 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 45 ms 5976 KB Output is correct
22 Correct 195 ms 23260 KB Output is correct
23 Correct 188 ms 22140 KB Output is correct
24 Correct 197 ms 28400 KB Output is correct
25 Correct 72 ms 12276 KB Output is correct
26 Correct 88 ms 13292 KB Output is correct
27 Correct 169 ms 22248 KB Output is correct
28 Correct 199 ms 26328 KB Output is correct
29 Correct 70 ms 12264 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 46 ms 5968 KB Output is correct
5 Correct 198 ms 23144 KB Output is correct
6 Correct 184 ms 22156 KB Output is correct
7 Correct 212 ms 26288 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 45 ms 5908 KB Output is correct
10 Correct 178 ms 23192 KB Output is correct
11 Correct 206 ms 22280 KB Output is correct
12 Correct 208 ms 28408 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 416 KB Output is correct
16 Correct 45 ms 5912 KB Output is correct
17 Correct 244 ms 23276 KB Output is correct
18 Correct 206 ms 23284 KB Output is correct
19 Correct 207 ms 22504 KB Output is correct
20 Correct 216 ms 22192 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 1 ms 340 KB Output is correct
6 Correct 9 ms 1748 KB Output is correct
7 Correct 187 ms 30312 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 8 ms 1256 KB Output is correct
13 Correct 179 ms 22148 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 4 ms 1108 KB Output is correct
17 Correct 90 ms 17896 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 46 ms 5976 KB Output is correct
21 Correct 178 ms 23156 KB Output is correct
22 Correct 193 ms 22184 KB Output is correct
23 Correct 202 ms 26240 KB Output is correct
24 Correct 173 ms 22248 KB Output is correct
25 Correct 75 ms 13288 KB Output is correct
26 Correct 71 ms 12236 KB Output is correct
27 Correct 207 ms 28408 KB Output is correct
28 Correct 177 ms 22152 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 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 7 ms 1236 KB Output is correct
37 Correct 182 ms 22304 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 8 ms 1748 KB Output is correct
41 Correct 213 ms 30296 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 0 ms 340 KB Output is correct
44 Correct 5 ms 1072 KB Output is correct
45 Correct 99 ms 17896 KB Output is correct
46 Correct 0 ms 340 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 0 ms 340 KB Output is correct
49 Correct 45 ms 5976 KB Output is correct
50 Correct 195 ms 23260 KB Output is correct
51 Correct 188 ms 22140 KB Output is correct
52 Correct 197 ms 28400 KB Output is correct
53 Correct 72 ms 12276 KB Output is correct
54 Correct 88 ms 13292 KB Output is correct
55 Correct 169 ms 22248 KB Output is correct
56 Correct 199 ms 26328 KB Output is correct
57 Correct 70 ms 12264 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 5 ms 1096 KB Output is correct
61 Correct 110 ms 16740 KB Output is correct
62 Correct 1 ms 340 KB Output is correct
63 Correct 0 ms 340 KB Output is correct
64 Correct 1 ms 340 KB Output is correct
65 Correct 53 ms 5904 KB Output is correct
66 Correct 82 ms 13544 KB Output is correct
67 Correct 78 ms 13300 KB Output is correct
68 Correct 75 ms 12652 KB Output is correct
69 Correct 81 ms 12276 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 1 ms 340 KB Output is correct
6 Correct 9 ms 1748 KB Output is correct
7 Correct 187 ms 30312 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 8 ms 1256 KB Output is correct
13 Correct 179 ms 22148 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 4 ms 1108 KB Output is correct
17 Correct 90 ms 17896 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 46 ms 5976 KB Output is correct
21 Correct 178 ms 23156 KB Output is correct
22 Correct 193 ms 22184 KB Output is correct
23 Correct 202 ms 26240 KB Output is correct
24 Correct 173 ms 22248 KB Output is correct
25 Correct 75 ms 13288 KB Output is correct
26 Correct 71 ms 12236 KB Output is correct
27 Correct 207 ms 28408 KB Output is correct
28 Correct 177 ms 22152 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 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 7 ms 1236 KB Output is correct
37 Correct 182 ms 22304 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 8 ms 1748 KB Output is correct
41 Correct 213 ms 30296 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 0 ms 340 KB Output is correct
44 Correct 5 ms 1072 KB Output is correct
45 Correct 99 ms 17896 KB Output is correct
46 Correct 0 ms 340 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 0 ms 340 KB Output is correct
49 Correct 45 ms 5976 KB Output is correct
50 Correct 195 ms 23260 KB Output is correct
51 Correct 188 ms 22140 KB Output is correct
52 Correct 197 ms 28400 KB Output is correct
53 Correct 72 ms 12276 KB Output is correct
54 Correct 88 ms 13292 KB Output is correct
55 Correct 169 ms 22248 KB Output is correct
56 Correct 199 ms 26328 KB Output is correct
57 Correct 70 ms 12264 KB Output is correct
58 Correct 0 ms 340 KB Output is correct
59 Correct 0 ms 340 KB Output is correct
60 Correct 0 ms 340 KB Output is correct
61 Correct 46 ms 5968 KB Output is correct
62 Correct 198 ms 23144 KB Output is correct
63 Correct 184 ms 22156 KB Output is correct
64 Correct 212 ms 26288 KB Output is correct
65 Correct 0 ms 340 KB Output is correct
66 Correct 45 ms 5908 KB Output is correct
67 Correct 178 ms 23192 KB Output is correct
68 Correct 206 ms 22280 KB Output is correct
69 Correct 208 ms 28408 KB Output is correct
70 Correct 0 ms 340 KB Output is correct
71 Correct 1 ms 340 KB Output is correct
72 Correct 1 ms 416 KB Output is correct
73 Correct 45 ms 5912 KB Output is correct
74 Correct 244 ms 23276 KB Output is correct
75 Correct 206 ms 23284 KB Output is correct
76 Correct 207 ms 22504 KB Output is correct
77 Correct 216 ms 22192 KB Output is correct
78 Correct 1 ms 340 KB Output is correct
79 Correct 1 ms 340 KB Output is correct
80 Correct 5 ms 1096 KB Output is correct
81 Correct 110 ms 16740 KB Output is correct
82 Correct 1 ms 340 KB Output is correct
83 Correct 0 ms 340 KB Output is correct
84 Correct 1 ms 340 KB Output is correct
85 Correct 53 ms 5904 KB Output is correct
86 Correct 82 ms 13544 KB Output is correct
87 Correct 78 ms 13300 KB Output is correct
88 Correct 75 ms 12652 KB Output is correct
89 Correct 81 ms 12276 KB Output is correct
90 Correct 0 ms 340 KB Output is correct
91 Correct 1 ms 352 KB Output is correct
92 Correct 4 ms 852 KB Output is correct
93 Correct 91 ms 12132 KB Output is correct
94 Correct 1 ms 340 KB Output is correct
95 Correct 1 ms 340 KB Output is correct
96 Correct 1 ms 340 KB Output is correct
97 Correct 22 ms 3796 KB Output is correct
98 Correct 92 ms 14168 KB Output is correct
99 Correct 104 ms 14172 KB Output is correct
100 Correct 80 ms 14180 KB Output is correct
101 Correct 80 ms 14168 KB Output is correct
102 Correct 101 ms 14164 KB Output is correct
103 Correct 70 ms 14180 KB Output is correct
104 Correct 77 ms 14168 KB Output is correct
105 Correct 100 ms 14168 KB Output is correct