답안 #532782

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
532782 2022-03-03T22:43:31 Z aryan12 슈퍼트리 잇기 (IOI20_supertrees) C++17
100 / 100
279 ms 26736 KB
#include "supertrees.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;

const int N = 1010;
int parent_component[N];
vector<int> components[N];
vector<int> g[N];
vector<bool> vis(N);
vector<vector<int> > answer;
vector<int> bruh(N, -1);
vector<int> representative(N, -1);

int Find(int x) {
	if(x == parent_component[x]) return x;
	return parent_component[x] = Find(parent_component[x]);
}

void Unite(int a, int b) {
	a = Find(a), b = Find(b);
	if(a > b) swap(a, b);
	parent_component[b] = a;
}

void dfs(int node, int total_par) {
	parent_component[node] = total_par;
	components[total_par].push_back(node);
	vis[node] = true;
	for(int i = 0; i < g[node].size(); i++) {
		if(vis[g[node][i]]) continue;
		dfs(g[node][i], total_par);
	}
}

void lmao(int node, int okbruh) {
	bruh[node] = okbruh;
	if(node != okbruh) {
		answer[node][okbruh] = 1;
		answer[okbruh][node] = 1;
	}
	vis[node] = true;
	for(int i = 0; i < g[node].size(); i++) {
		if(vis[g[node][i]]) continue;
		lmao(g[node][i], okbruh);
	}
}

int construct(vector<vector<int> > input) {
	int n = input.size();
	answer.resize(n, vector<int> (n, 0));
	for(int i = 0; i < n; i++) {
		parent_component[i] = i;
		representative[i] = i;
		vis[i] = false;
		for(int j = 0; j < n; j++) {
			answer[i][j] = 0;
		}
	}
	for(int i = 0; i < n; i++) {
		for(int j = i + 1; j < n; j++) {
			if(input[i][j] == 3) {
				return 0;
			}
			if(input[i][j] != 2) continue;
			g[i].push_back(j);
			g[j].push_back(i);
		}
	}
	for(int i = 0; i < n; i++) {
		if(!vis[i]) {
			dfs(i, i);
		}
	}
	for(int i = 0; i < n; i++) {
		//cout << "parent_component[i] = " << parent_component[i] << "\n";
		parent_component[i] = Find(parent_component[i]);
	}
	for(int i = 0; i < n; i++) {
		if(i != parent_component[i] || components[i].size() == 1) continue;
		if(components[i].size() == 2) {
			return 0;
		}
		//cout << "i = " << i << "\n";
		sort(components[i].begin(), components[i].end());
		for(int j = 0; j < components[i].size(); j++) {
			//cout << "components[i][j] = " << components[i][j] << "\n";
			for(int k = j + 1; k < components[i].size(); k++) {
				if(input[components[i][j]][components[i][k]] == 1) {
					//cout << "components[i][j] = " << components[i][j] << ", components[i][k] = " << components[i][k] << "\n";
					if(representative[components[i][j]] == components[i][j]) {
						representative[components[i][k]] = components[i][j];
					}
				}
			}
		}
		int prev = -1, first_one = -1;
		for(int j = 0; j < components[i].size(); j++) {
			//cout << "components[i][j] = " << components[i][j] << ", representative = " << representative[components[i][j]] << "\n";
			if(representative[components[i][j]] == components[i][j]) {
				if(prev == -1) {
					first_one = components[i][j];
				}
				else {
					answer[prev][components[i][j]] = 1;
					answer[components[i][j]][prev] = 1;
				}
				prev = components[i][j];
			}
			else {
				answer[components[i][j]][representative[components[i][j]]] = 1;
				answer[representative[components[i][j]]][components[i][j]] = 1;
			}
		}
		//cout << "first_one = " << first_one << ", prev = " << prev << "\n";
		//cout << "first_one = " << first_one << ", prev = " << prev << "\n";
		answer[first_one][prev] = 1;
		answer[prev][first_one] = 1;
		for(int j = 0; j < components[i].size(); j++) {
			for(int k = 0; k < components[i].size(); k++) {
				int node1 = components[i][j], node2 = components[i][k];
				if(representative[node1] == representative[node2] && input[node1][node2] != 1) {
					//cout << "same representative node1 = " << node1 << ", node2 = " << node2 << "\n";
					return 0;
				}
				if(representative[node1] != representative[node2] && input[node1][node2] != 2) {
					//cout << "diff representative node1 = " << node1 << ", node2 = " << node2 << "\n";
					//cout << representative[node1] << ", " << representative[node2] << "\n";
					return 0;
				}
			}
		}
		//cout << "hello\n";
	}
	for(int i = 0; i < n; i++) {
		g[i].clear();
		vis[i] = false;
	}
	for(int i = 0; i < n; i++) {
		for(int j = i + 1; j < n; j++) {
			if(input[i][j] != 1) {
				continue;
			}
			if(Find(i) == Find(j)) continue;
			g[i].push_back(j);
			g[j].push_back(i);
			//cout << "i = " << i << ", j = " << j << "\n";
		}
	}
	for(int i = 0; i < n; i++) {
		if(!vis[i]) {
			lmao(i, i);
		}
	}
	for(int i = 0; i < n; i++) {
		for(int j = i + 1; j < n; j++) {
			if(bruh[i] == bruh[j] && input[i][j] == 0) {
				return 0;
			}
		}
	}
	build(answer);
	return 1;
}

Compilation message

supertrees.cpp: In function 'void dfs(int, int)':
supertrees.cpp:30:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |  for(int i = 0; i < g[node].size(); i++) {
      |                 ~~^~~~~~~~~~~~~~~~
supertrees.cpp: In function 'void lmao(int, int)':
supertrees.cpp:43:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |  for(int i = 0; i < g[node].size(); i++) {
      |                 ~~^~~~~~~~~~~~~~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:86:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |   for(int j = 0; j < components[i].size(); j++) {
      |                  ~~^~~~~~~~~~~~~~~~~~~~~~
supertrees.cpp:88:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |    for(int k = j + 1; k < components[i].size(); k++) {
      |                       ~~^~~~~~~~~~~~~~~~~~~~~~
supertrees.cpp:98:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |   for(int j = 0; j < components[i].size(); j++) {
      |                  ~~^~~~~~~~~~~~~~~~~~~~~~
supertrees.cpp:119:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  119 |   for(int j = 0; j < components[i].size(); j++) {
      |                  ~~^~~~~~~~~~~~~~~~~~~~~~
supertrees.cpp:120:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |    for(int k = 0; k < components[i].size(); k++) {
      |                   ~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 12 ms 1412 KB Output is correct
7 Correct 211 ms 26148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 12 ms 1412 KB Output is correct
7 Correct 211 ms 26148 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 1 ms 236 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 8 ms 1228 KB Output is correct
13 Correct 188 ms 22084 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 6 ms 972 KB Output is correct
17 Correct 131 ms 15028 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 46 ms 5776 KB Output is correct
21 Correct 247 ms 22652 KB Output is correct
22 Correct 208 ms 22080 KB Output is correct
23 Correct 208 ms 24372 KB Output is correct
24 Correct 178 ms 22596 KB Output is correct
25 Correct 74 ms 13108 KB Output is correct
26 Correct 70 ms 12624 KB Output is correct
27 Correct 226 ms 25632 KB Output is correct
28 Correct 167 ms 22484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 9 ms 1272 KB Output is correct
9 Correct 176 ms 22584 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 8 ms 1484 KB Output is correct
13 Correct 211 ms 26736 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 5 ms 972 KB Output is correct
17 Correct 108 ms 15524 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 42 ms 6348 KB Output is correct
22 Correct 174 ms 23140 KB Output is correct
23 Correct 180 ms 22672 KB Output is correct
24 Correct 202 ms 25668 KB Output is correct
25 Correct 73 ms 12648 KB Output is correct
26 Correct 73 ms 13176 KB Output is correct
27 Correct 226 ms 22664 KB Output is correct
28 Correct 204 ms 24804 KB Output is correct
29 Correct 68 ms 12616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 44 ms 6296 KB Output is correct
5 Correct 196 ms 23108 KB Output is correct
6 Correct 172 ms 22596 KB Output is correct
7 Correct 279 ms 24612 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 42 ms 6356 KB Output is correct
10 Correct 178 ms 23148 KB Output is correct
11 Correct 181 ms 22684 KB Output is correct
12 Correct 196 ms 25656 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 46 ms 6232 KB Output is correct
17 Correct 174 ms 24616 KB Output is correct
18 Correct 181 ms 24616 KB Output is correct
19 Correct 168 ms 24232 KB Output is correct
20 Correct 183 ms 24068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 12 ms 1412 KB Output is correct
7 Correct 211 ms 26148 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 1 ms 236 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 8 ms 1228 KB Output is correct
13 Correct 188 ms 22084 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 6 ms 972 KB Output is correct
17 Correct 131 ms 15028 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 46 ms 5776 KB Output is correct
21 Correct 247 ms 22652 KB Output is correct
22 Correct 208 ms 22080 KB Output is correct
23 Correct 208 ms 24372 KB Output is correct
24 Correct 178 ms 22596 KB Output is correct
25 Correct 74 ms 13108 KB Output is correct
26 Correct 70 ms 12624 KB Output is correct
27 Correct 226 ms 25632 KB Output is correct
28 Correct 167 ms 22484 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 9 ms 1272 KB Output is correct
37 Correct 176 ms 22584 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 8 ms 1484 KB Output is correct
41 Correct 211 ms 26736 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 5 ms 972 KB Output is correct
45 Correct 108 ms 15524 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 42 ms 6348 KB Output is correct
50 Correct 174 ms 23140 KB Output is correct
51 Correct 180 ms 22672 KB Output is correct
52 Correct 202 ms 25668 KB Output is correct
53 Correct 73 ms 12648 KB Output is correct
54 Correct 73 ms 13176 KB Output is correct
55 Correct 226 ms 22664 KB Output is correct
56 Correct 204 ms 24804 KB Output is correct
57 Correct 68 ms 12616 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 1 ms 332 KB Output is correct
60 Correct 5 ms 980 KB Output is correct
61 Correct 102 ms 15068 KB Output is correct
62 Correct 1 ms 332 KB Output is correct
63 Correct 1 ms 332 KB Output is correct
64 Correct 1 ms 332 KB Output is correct
65 Correct 43 ms 6248 KB Output is correct
66 Correct 74 ms 14760 KB Output is correct
67 Correct 81 ms 14660 KB Output is correct
68 Correct 103 ms 14248 KB Output is correct
69 Correct 68 ms 14128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 12 ms 1412 KB Output is correct
7 Correct 211 ms 26148 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 1 ms 236 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 8 ms 1228 KB Output is correct
13 Correct 188 ms 22084 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 6 ms 972 KB Output is correct
17 Correct 131 ms 15028 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 46 ms 5776 KB Output is correct
21 Correct 247 ms 22652 KB Output is correct
22 Correct 208 ms 22080 KB Output is correct
23 Correct 208 ms 24372 KB Output is correct
24 Correct 178 ms 22596 KB Output is correct
25 Correct 74 ms 13108 KB Output is correct
26 Correct 70 ms 12624 KB Output is correct
27 Correct 226 ms 25632 KB Output is correct
28 Correct 167 ms 22484 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 9 ms 1272 KB Output is correct
37 Correct 176 ms 22584 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 8 ms 1484 KB Output is correct
41 Correct 211 ms 26736 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 5 ms 972 KB Output is correct
45 Correct 108 ms 15524 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 42 ms 6348 KB Output is correct
50 Correct 174 ms 23140 KB Output is correct
51 Correct 180 ms 22672 KB Output is correct
52 Correct 202 ms 25668 KB Output is correct
53 Correct 73 ms 12648 KB Output is correct
54 Correct 73 ms 13176 KB Output is correct
55 Correct 226 ms 22664 KB Output is correct
56 Correct 204 ms 24804 KB Output is correct
57 Correct 68 ms 12616 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 1 ms 332 KB Output is correct
60 Correct 0 ms 332 KB Output is correct
61 Correct 44 ms 6296 KB Output is correct
62 Correct 196 ms 23108 KB Output is correct
63 Correct 172 ms 22596 KB Output is correct
64 Correct 279 ms 24612 KB Output is correct
65 Correct 0 ms 332 KB Output is correct
66 Correct 42 ms 6356 KB Output is correct
67 Correct 178 ms 23148 KB Output is correct
68 Correct 181 ms 22684 KB Output is correct
69 Correct 196 ms 25656 KB Output is correct
70 Correct 1 ms 332 KB Output is correct
71 Correct 1 ms 332 KB Output is correct
72 Correct 1 ms 332 KB Output is correct
73 Correct 46 ms 6232 KB Output is correct
74 Correct 174 ms 24616 KB Output is correct
75 Correct 181 ms 24616 KB Output is correct
76 Correct 168 ms 24232 KB Output is correct
77 Correct 183 ms 24068 KB Output is correct
78 Correct 1 ms 332 KB Output is correct
79 Correct 1 ms 332 KB Output is correct
80 Correct 5 ms 980 KB Output is correct
81 Correct 102 ms 15068 KB Output is correct
82 Correct 1 ms 332 KB Output is correct
83 Correct 1 ms 332 KB Output is correct
84 Correct 1 ms 332 KB Output is correct
85 Correct 43 ms 6248 KB Output is correct
86 Correct 74 ms 14760 KB Output is correct
87 Correct 81 ms 14660 KB Output is correct
88 Correct 103 ms 14248 KB Output is correct
89 Correct 68 ms 14128 KB Output is correct
90 Correct 1 ms 332 KB Output is correct
91 Correct 1 ms 332 KB Output is correct
92 Correct 5 ms 892 KB Output is correct
93 Correct 90 ms 14120 KB Output is correct
94 Correct 1 ms 332 KB Output is correct
95 Correct 0 ms 332 KB Output is correct
96 Correct 1 ms 352 KB Output is correct
97 Correct 17 ms 3820 KB Output is correct
98 Correct 77 ms 14128 KB Output is correct
99 Correct 75 ms 14252 KB Output is correct
100 Correct 74 ms 14120 KB Output is correct
101 Correct 67 ms 14128 KB Output is correct
102 Correct 84 ms 14136 KB Output is correct
103 Correct 84 ms 14144 KB Output is correct
104 Correct 84 ms 14120 KB Output is correct
105 Correct 91 ms 14116 KB Output is correct