답안 #426197

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
426197 2021-06-13T15:08:16 Z erekle 슈퍼트리 잇기 (IOI20_supertrees) C++17
100 / 100
331 ms 47564 KB
#include "supertrees.h"
#include <bits/stdc++.h>

using namespace std;

// DSU
int root(int i, vector<int> &parent) {
	return parent[i] < 0 ? i : (parent[i] = root(parent[i], parent));
}
bool unite(int i, int j, vector<int> &parent) {
	i = root(i, parent), j = root(j, parent);
	if (i == j) return false;
	if (parent[i] < parent[j]) swap(i, j);
	parent[j] += parent[i];
	parent[i] = j;
	return true;
}

int construct(vector<vector<int>> p) {
	int n = p.size();
	vector<vector<int>> answer(n, vector<int>(n));
	
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (p[i][j] == 3) return 0;
		}
	}

	vector<int> parent(n, -1);
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (p[i][j]) unite(i, j, parent);
		}
	}
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (!p[i][j] && root(i, parent) == root(j, parent)) return 0;
		}
	}

	vector<int> parent2(n, -1);
	// now within components
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (root(i, parent) != root(j, parent)) continue;
			if (p[i][j] == 1) unite(i, j, parent2);
		}
	}

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (root(i, parent2) == root(j, parent2) && p[i][j] != 1) return 0;
		}
	}

	auto connect = [&](int i, int j) {answer[i][j] = answer[j][i] = 1;};
	
	vector<vector<vector<int>>> g(n, vector<vector<int>>(n));
	for (int i = 0; i < n; ++i) { // chains
		int x = root(i, parent), y = root(i, parent2);
		if (!g[x][y].empty()) connect(g[x][y].back(), i);
		g[x][y].push_back(i);
	}

	for (int x = 0; x < n; ++x) { // cycle using endpoints of chains
		vector<int> ys;
		for (int y = 0; y < n; ++y) {
			if (!g[x][y].empty()) ys.push_back(y);
		}
		int m = ys.size();
		if (m <= 1) continue;
		if (m == 2) return 0;
		for (int i = 0; i < m; ++i) connect(g[x][ys[i]][0], g[x][ys[(i+1)%m]][0]);
	}

	build(answer);
	return 1;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 12 ms 2180 KB Output is correct
7 Correct 287 ms 47532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 12 ms 2180 KB Output is correct
7 Correct 287 ms 47532 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 288 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 11 ms 2160 KB Output is correct
13 Correct 254 ms 47464 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 7 ms 844 KB Output is correct
17 Correct 122 ms 14188 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 292 KB Output is correct
20 Correct 71 ms 12100 KB Output is correct
21 Correct 331 ms 47488 KB Output is correct
22 Correct 293 ms 47492 KB Output is correct
23 Correct 331 ms 47524 KB Output is correct
24 Correct 263 ms 47508 KB Output is correct
25 Correct 102 ms 14020 KB Output is correct
26 Correct 111 ms 14020 KB Output is correct
27 Correct 301 ms 47496 KB Output is correct
28 Correct 303 ms 47468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 288 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 12 ms 2200 KB Output is correct
9 Correct 284 ms 47556 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 11 ms 2140 KB Output is correct
13 Correct 267 ms 47520 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 844 KB Output is correct
17 Correct 112 ms 14020 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 71 ms 12052 KB Output is correct
22 Correct 280 ms 47476 KB Output is correct
23 Correct 267 ms 47468 KB Output is correct
24 Correct 291 ms 47468 KB Output is correct
25 Correct 145 ms 37572 KB Output is correct
26 Correct 100 ms 14060 KB Output is correct
27 Correct 303 ms 47532 KB Output is correct
28 Correct 286 ms 47556 KB Output is correct
29 Correct 128 ms 37576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 292 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 72 ms 12136 KB Output is correct
5 Correct 286 ms 47472 KB Output is correct
6 Correct 291 ms 47552 KB Output is correct
7 Correct 297 ms 47428 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 86 ms 12104 KB Output is correct
10 Correct 277 ms 47476 KB Output is correct
11 Correct 274 ms 47540 KB Output is correct
12 Correct 285 ms 47468 KB Output is correct
13 Correct 1 ms 328 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 72 ms 12104 KB Output is correct
17 Correct 281 ms 47556 KB Output is correct
18 Correct 323 ms 47464 KB Output is correct
19 Correct 274 ms 47448 KB Output is correct
20 Correct 288 ms 47564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 12 ms 2180 KB Output is correct
7 Correct 287 ms 47532 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 288 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 11 ms 2160 KB Output is correct
13 Correct 254 ms 47464 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 7 ms 844 KB Output is correct
17 Correct 122 ms 14188 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 292 KB Output is correct
20 Correct 71 ms 12100 KB Output is correct
21 Correct 331 ms 47488 KB Output is correct
22 Correct 293 ms 47492 KB Output is correct
23 Correct 331 ms 47524 KB Output is correct
24 Correct 263 ms 47508 KB Output is correct
25 Correct 102 ms 14020 KB Output is correct
26 Correct 111 ms 14020 KB Output is correct
27 Correct 301 ms 47496 KB Output is correct
28 Correct 303 ms 47468 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 288 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 12 ms 2200 KB Output is correct
37 Correct 284 ms 47556 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 11 ms 2140 KB Output is correct
41 Correct 267 ms 47520 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 5 ms 844 KB Output is correct
45 Correct 112 ms 14020 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 71 ms 12052 KB Output is correct
50 Correct 280 ms 47476 KB Output is correct
51 Correct 267 ms 47468 KB Output is correct
52 Correct 291 ms 47468 KB Output is correct
53 Correct 145 ms 37572 KB Output is correct
54 Correct 100 ms 14060 KB Output is correct
55 Correct 303 ms 47532 KB Output is correct
56 Correct 286 ms 47556 KB Output is correct
57 Correct 128 ms 37576 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 5 ms 844 KB Output is correct
61 Correct 111 ms 14056 KB Output is correct
62 Correct 1 ms 204 KB Output is correct
63 Correct 1 ms 204 KB Output is correct
64 Correct 1 ms 204 KB Output is correct
65 Correct 69 ms 12144 KB Output is correct
66 Correct 102 ms 14060 KB Output is correct
67 Correct 110 ms 14020 KB Output is correct
68 Correct 101 ms 14060 KB Output is correct
69 Correct 121 ms 37624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 12 ms 2180 KB Output is correct
7 Correct 287 ms 47532 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 288 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 11 ms 2160 KB Output is correct
13 Correct 254 ms 47464 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 7 ms 844 KB Output is correct
17 Correct 122 ms 14188 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 292 KB Output is correct
20 Correct 71 ms 12100 KB Output is correct
21 Correct 331 ms 47488 KB Output is correct
22 Correct 293 ms 47492 KB Output is correct
23 Correct 331 ms 47524 KB Output is correct
24 Correct 263 ms 47508 KB Output is correct
25 Correct 102 ms 14020 KB Output is correct
26 Correct 111 ms 14020 KB Output is correct
27 Correct 301 ms 47496 KB Output is correct
28 Correct 303 ms 47468 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 288 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 12 ms 2200 KB Output is correct
37 Correct 284 ms 47556 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 11 ms 2140 KB Output is correct
41 Correct 267 ms 47520 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 5 ms 844 KB Output is correct
45 Correct 112 ms 14020 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 71 ms 12052 KB Output is correct
50 Correct 280 ms 47476 KB Output is correct
51 Correct 267 ms 47468 KB Output is correct
52 Correct 291 ms 47468 KB Output is correct
53 Correct 145 ms 37572 KB Output is correct
54 Correct 100 ms 14060 KB Output is correct
55 Correct 303 ms 47532 KB Output is correct
56 Correct 286 ms 47556 KB Output is correct
57 Correct 128 ms 37576 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 1 ms 292 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 72 ms 12136 KB Output is correct
62 Correct 286 ms 47472 KB Output is correct
63 Correct 291 ms 47552 KB Output is correct
64 Correct 297 ms 47428 KB Output is correct
65 Correct 1 ms 204 KB Output is correct
66 Correct 86 ms 12104 KB Output is correct
67 Correct 277 ms 47476 KB Output is correct
68 Correct 274 ms 47540 KB Output is correct
69 Correct 285 ms 47468 KB Output is correct
70 Correct 1 ms 328 KB Output is correct
71 Correct 1 ms 204 KB Output is correct
72 Correct 1 ms 204 KB Output is correct
73 Correct 72 ms 12104 KB Output is correct
74 Correct 281 ms 47556 KB Output is correct
75 Correct 323 ms 47464 KB Output is correct
76 Correct 274 ms 47448 KB Output is correct
77 Correct 288 ms 47564 KB Output is correct
78 Correct 1 ms 204 KB Output is correct
79 Correct 1 ms 204 KB Output is correct
80 Correct 5 ms 844 KB Output is correct
81 Correct 111 ms 14056 KB Output is correct
82 Correct 1 ms 204 KB Output is correct
83 Correct 1 ms 204 KB Output is correct
84 Correct 1 ms 204 KB Output is correct
85 Correct 69 ms 12144 KB Output is correct
86 Correct 102 ms 14060 KB Output is correct
87 Correct 110 ms 14020 KB Output is correct
88 Correct 101 ms 14060 KB Output is correct
89 Correct 121 ms 37624 KB Output is correct
90 Correct 1 ms 204 KB Output is correct
91 Correct 1 ms 204 KB Output is correct
92 Correct 5 ms 844 KB Output is correct
93 Correct 104 ms 14060 KB Output is correct
94 Correct 1 ms 292 KB Output is correct
95 Correct 1 ms 204 KB Output is correct
96 Correct 1 ms 204 KB Output is correct
97 Correct 25 ms 3780 KB Output is correct
98 Correct 97 ms 14064 KB Output is correct
99 Correct 96 ms 14060 KB Output is correct
100 Correct 117 ms 14148 KB Output is correct
101 Correct 94 ms 14060 KB Output is correct
102 Correct 94 ms 14020 KB Output is correct
103 Correct 100 ms 14056 KB Output is correct
104 Correct 95 ms 14064 KB Output is correct
105 Correct 128 ms 14020 KB Output is correct