답안 #568469

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
568469 2022-05-25T14:05:31 Z Jomnoi 슈퍼트리 잇기 (IOI20_supertrees) C++17
100 / 100
978 ms 26848 KB
#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;

const int MAX_N = 1e3 + 5;

int n;
vector <int> graph[MAX_N];
bool visited[MAX_N];
vector <vector <int>> answer;
int cmp[MAX_N];
int parent[MAX_N];
vector <int> vertex[MAX_N];

int root(int u) {
	if(u == parent[u]) {
		return u;
	}
	return parent[u] = root(parent[u]);
}

void dfs(int u) {
	visited[u] = true;
	for(auto v : graph[u]) {
		if(visited[v] == false) {
			cmp[v] = cmp[u];
			answer[u][v] = answer[v][u] = 1;
			dfs(v);
		}
	}
}

int solve(vector <int> nodes, vector <vector <int>> p) {
	bool two = false;
	
	for(auto u : nodes) {
		for(auto v : nodes) {
			if(u < v and p[u][v] == 1) {
				graph[u].push_back(v);
				graph[v].push_back(u);
			}
		}
	}

	for(auto u : nodes) {
		if(visited[u] == false) {
			visited[u] = true;
			cmp[u] = u;
			dfs(u);
		}
	}

	for(auto u : nodes) {
		parent[u] = u;
	}
	for(auto u : nodes) {
		for(auto v : nodes) {
			if(u < v and p[u][v] == 2) {
				two = true;
				parent[root(cmp[v])] = root(cmp[u]);
			}
		}
	}

	if(two == true) {
		vector <int> cycle;
		for(auto u : nodes) {
			if(cmp[u] == u) {
				cycle.push_back(u);
			}
		}
		if(two == true and cycle.size() <= 2) {
			return 0;
		}

		int pr = cycle.back();
		for(auto v : cycle) {
			answer[v][pr] = answer[pr][v] = 1;
			pr = v;
		}
	}
	return 1;
}

int construct(vector <vector <int>> p) {
	n = p.size();
	fill(cmp, cmp + n, -1);
	answer.resize(n, vector <int> (n, 0));

	iota(parent, parent + n, 0);
	for(int i = 0; i < n; i++) {
		for(int j = i + 1; j < n; j++) {
			if(p[i][j] == 3) {
				return 0;
			}
			if(p[i][j] != 0) {
				parent[root(j)] = root(i);
			}
		}
	}
	for(int i = 0; i < n; i++) {
		for(int j = i + 1; j < n; j++) {
			if(p[i][j] == 0 and root(i) == root(j)) {
				return 0;
			}
		}
	}

	vector <int> components;
	for(int i = 0; i < n; i++) {
		vertex[root(i)].push_back(i);
		if(i == root(i)) {
			components.push_back(i);
		}
	}
	for(auto c : components) {
		if(solve(vertex[c], p) == 0) {
			return 0;
		}
	}

	build(answer);
	return 1;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 10 ms 1492 KB Output is correct
7 Correct 207 ms 26848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 10 ms 1492 KB Output is correct
7 Correct 207 ms 26848 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 20 ms 1220 KB Output is correct
13 Correct 978 ms 22708 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 852 KB Output is correct
17 Correct 92 ms 12728 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 58 ms 6320 KB Output is correct
21 Correct 228 ms 23276 KB Output is correct
22 Correct 494 ms 22688 KB Output is correct
23 Correct 211 ms 24828 KB Output is correct
24 Correct 891 ms 22812 KB Output is correct
25 Correct 92 ms 12796 KB Output is correct
26 Correct 86 ms 12824 KB Output is correct
27 Correct 237 ms 25788 KB Output is correct
28 Correct 837 ms 22712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 276 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 352 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 16 ms 1260 KB Output is correct
9 Correct 884 ms 23096 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 10 ms 1240 KB Output is correct
13 Correct 207 ms 23108 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 880 KB Output is correct
17 Correct 87 ms 13432 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 57 ms 6220 KB Output is correct
22 Correct 195 ms 23100 KB Output is correct
23 Correct 583 ms 23168 KB Output is correct
24 Correct 191 ms 23088 KB Output is correct
25 Correct 375 ms 17424 KB Output is correct
26 Correct 78 ms 13440 KB Output is correct
27 Correct 567 ms 23172 KB Output is correct
28 Correct 202 ms 23092 KB Output is correct
29 Correct 394 ms 17456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 58 ms 6336 KB Output is correct
5 Correct 187 ms 23172 KB Output is correct
6 Correct 411 ms 22860 KB Output is correct
7 Correct 229 ms 24776 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 60 ms 6348 KB Output is correct
10 Correct 189 ms 22708 KB Output is correct
11 Correct 512 ms 22740 KB Output is correct
12 Correct 211 ms 22804 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 60 ms 6216 KB Output is correct
17 Correct 203 ms 22700 KB Output is correct
18 Correct 203 ms 22756 KB Output is correct
19 Correct 261 ms 22720 KB Output is correct
20 Correct 713 ms 22732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 10 ms 1492 KB Output is correct
7 Correct 207 ms 26848 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 20 ms 1220 KB Output is correct
13 Correct 978 ms 22708 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 852 KB Output is correct
17 Correct 92 ms 12728 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 58 ms 6320 KB Output is correct
21 Correct 228 ms 23276 KB Output is correct
22 Correct 494 ms 22688 KB Output is correct
23 Correct 211 ms 24828 KB Output is correct
24 Correct 891 ms 22812 KB Output is correct
25 Correct 92 ms 12796 KB Output is correct
26 Correct 86 ms 12824 KB Output is correct
27 Correct 237 ms 25788 KB Output is correct
28 Correct 837 ms 22712 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 276 KB Output is correct
31 Correct 1 ms 344 KB Output is correct
32 Correct 1 ms 352 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 16 ms 1260 KB Output is correct
37 Correct 884 ms 23096 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 10 ms 1240 KB Output is correct
41 Correct 207 ms 23108 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 4 ms 880 KB Output is correct
45 Correct 87 ms 13432 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 57 ms 6220 KB Output is correct
50 Correct 195 ms 23100 KB Output is correct
51 Correct 583 ms 23168 KB Output is correct
52 Correct 191 ms 23088 KB Output is correct
53 Correct 375 ms 17424 KB Output is correct
54 Correct 78 ms 13440 KB Output is correct
55 Correct 567 ms 23172 KB Output is correct
56 Correct 202 ms 23092 KB Output is correct
57 Correct 394 ms 17456 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 4 ms 852 KB Output is correct
61 Correct 88 ms 13204 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 68 ms 6224 KB Output is correct
66 Correct 84 ms 13180 KB Output is correct
67 Correct 75 ms 13168 KB Output is correct
68 Correct 87 ms 13268 KB Output is correct
69 Correct 101 ms 17092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 10 ms 1492 KB Output is correct
7 Correct 207 ms 26848 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 20 ms 1220 KB Output is correct
13 Correct 978 ms 22708 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 852 KB Output is correct
17 Correct 92 ms 12728 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 58 ms 6320 KB Output is correct
21 Correct 228 ms 23276 KB Output is correct
22 Correct 494 ms 22688 KB Output is correct
23 Correct 211 ms 24828 KB Output is correct
24 Correct 891 ms 22812 KB Output is correct
25 Correct 92 ms 12796 KB Output is correct
26 Correct 86 ms 12824 KB Output is correct
27 Correct 237 ms 25788 KB Output is correct
28 Correct 837 ms 22712 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 276 KB Output is correct
31 Correct 1 ms 344 KB Output is correct
32 Correct 1 ms 352 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 16 ms 1260 KB Output is correct
37 Correct 884 ms 23096 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 10 ms 1240 KB Output is correct
41 Correct 207 ms 23108 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 4 ms 880 KB Output is correct
45 Correct 87 ms 13432 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 57 ms 6220 KB Output is correct
50 Correct 195 ms 23100 KB Output is correct
51 Correct 583 ms 23168 KB Output is correct
52 Correct 191 ms 23088 KB Output is correct
53 Correct 375 ms 17424 KB Output is correct
54 Correct 78 ms 13440 KB Output is correct
55 Correct 567 ms 23172 KB Output is correct
56 Correct 202 ms 23092 KB Output is correct
57 Correct 394 ms 17456 KB Output is correct
58 Correct 1 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 58 ms 6336 KB Output is correct
62 Correct 187 ms 23172 KB Output is correct
63 Correct 411 ms 22860 KB Output is correct
64 Correct 229 ms 24776 KB Output is correct
65 Correct 1 ms 340 KB Output is correct
66 Correct 60 ms 6348 KB Output is correct
67 Correct 189 ms 22708 KB Output is correct
68 Correct 512 ms 22740 KB Output is correct
69 Correct 211 ms 22804 KB Output is correct
70 Correct 1 ms 340 KB Output is correct
71 Correct 1 ms 340 KB Output is correct
72 Correct 1 ms 340 KB Output is correct
73 Correct 60 ms 6216 KB Output is correct
74 Correct 203 ms 22700 KB Output is correct
75 Correct 203 ms 22756 KB Output is correct
76 Correct 261 ms 22720 KB Output is correct
77 Correct 713 ms 22732 KB Output is correct
78 Correct 1 ms 340 KB Output is correct
79 Correct 1 ms 340 KB Output is correct
80 Correct 4 ms 852 KB Output is correct
81 Correct 88 ms 13204 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 68 ms 6224 KB Output is correct
86 Correct 84 ms 13180 KB Output is correct
87 Correct 75 ms 13168 KB Output is correct
88 Correct 87 ms 13268 KB Output is correct
89 Correct 101 ms 17092 KB Output is correct
90 Correct 1 ms 340 KB Output is correct
91 Correct 1 ms 348 KB Output is correct
92 Correct 4 ms 852 KB Output is correct
93 Correct 84 ms 13192 KB Output is correct
94 Correct 1 ms 356 KB Output is correct
95 Correct 1 ms 352 KB Output is correct
96 Correct 1 ms 340 KB Output is correct
97 Correct 22 ms 3788 KB Output is correct
98 Correct 84 ms 12744 KB Output is correct
99 Correct 79 ms 12728 KB Output is correct
100 Correct 74 ms 12748 KB Output is correct
101 Correct 89 ms 12736 KB Output is correct
102 Correct 78 ms 12748 KB Output is correct
103 Correct 74 ms 14120 KB Output is correct
104 Correct 71 ms 14120 KB Output is correct
105 Correct 79 ms 14120 KB Output is correct