답안 #428453

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
428453 2021-06-15T11:57:06 Z alireza_kaviani 슈퍼트리 잇기 (IOI20_supertrees) C++17
100 / 100
291 ms 30136 KB
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1010;
int n , par[MAXN] , sz[MAXN] , mark[MAXN] , adj[MAXN][MAXN] , ans[MAXN][MAXN];
vector<int> vec , comp[MAXN];

int Find(int v){
	return (par[v] == -1 ? v : par[v] = Find(par[v]));
}

void Union(int v , int u){
	v = Find(v); u = Find(u);
	if(v == u)	return;
	if(sz[v] < sz[u])	swap(v , u);
	par[u] = v;
	sz[v] += sz[u];
}

void DFS(int v){
	mark[v] = 1; vec.push_back(v);
	for(int u = 0 ; u < n ; u++){
		if(adj[v][u] > 0 && mark[u] == 0)	DFS(u);
	}
}

int solve(vector<int> v){
	vector<int> vec;
	for(int i : v){
		if(Find(i) == i){
			vec.push_back(i);
		}
	}
	int m = vec.size();
	if(m == 2)	return 0;
	for(int i = 0 ; i < m ; i++){
		int v = vec[i] , u = vec[(i + 1) % m];
		if(v != u)	ans[v][u] = ans[u][v] = 1;
	}
	int flag2 = 0 , flag3 = 0;
	for(int i : v){
		for(int j : v){
			if(adj[i][j] == 2)	flag2 = 1;
			if(adj[i][j] == 3)	flag3 = 1;
		}
	}
	if(flag2 + flag3 == 0)	return 1;
	if(flag2 && flag3)	return 0;
	if(flag2)	return 1;
	if(m <= 3)	return 0;
	ans[vec[0]][vec[2]] = ans[vec[2]][vec[0]] = 1;
	return 1;
}

int construct(vector<vector<int>> p) {
	fill(par , par + MAXN , -1);
	fill(sz , sz + MAXN , 1);
	n = p.size();
	vector<vector<int>> answer;
	for (int i = 0; i < n; i++) {
		vector<int> row(n , 0);
		answer.push_back(row);
		for(int j = 0 ; j < n ; j++){
			adj[i][j] = p[i][j];
			if(adj[i][j] == 1)	Union(i , j);
			if(adj[i][j] == 3)	return 0;
		}
	}
	for(int i = 0 ; i < n ; i++){
		for(int j = 0 ; j < n ; j++){
			if(Find(i) == Find(j) && adj[i][j] != 1)	return 0;
		}
		comp[Find(i)].push_back(i);
	}
	for(int i = 0 ; i < n ; i++){
		if(comp[i].size() == 0)	continue;
		for(int j = 0 ; j + 1 < comp[i].size() ; j++){
			if(comp[i][j] == i){
				swap(comp[i][j] , comp[i][comp[i].size() - 1]);
			}
		}
		for(int j = 0 ; j + 1 < comp[i].size() ; j++){
			int v = comp[i][j] , u = comp[i][j + 1];
			ans[v][u] = ans[u][v] = 1;
		}
	}
	for(int i = 0 ; i < n ; i++){
		if(!mark[i]){
			vec.clear();
			DFS(i);
			for(int j : vec){
				for(int k : vec){
					if(adj[j][k] == 0)	return 0;
				}
			}
			if(solve(vec) == 0)	return 0;
		}
	}
	for(int i = 0 ; i < n ; i++){
		for(int j = 0 ; j < n ; j++){
			answer[i][j] = ans[i][j];
		}
	}
	build(answer);
	return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:78:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |   for(int j = 0 ; j + 1 < comp[i].size() ; j++){
      |                   ~~~~~~^~~~~~~~~~~~~~~~
supertrees.cpp:83:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |   for(int j = 0 ; j + 1 < comp[i].size() ; j++){
      |                   ~~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 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 11 ms 2796 KB Output is correct
7 Correct 243 ms 30020 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 11 ms 2796 KB Output is correct
7 Correct 243 ms 30020 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 10 ms 1996 KB Output is correct
13 Correct 248 ms 25964 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 1612 KB Output is correct
17 Correct 130 ms 16108 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 65 ms 9632 KB Output is correct
21 Correct 241 ms 29980 KB Output is correct
22 Correct 238 ms 29472 KB Output is correct
23 Correct 258 ms 29980 KB Output is correct
24 Correct 235 ms 26284 KB Output is correct
25 Correct 101 ms 16068 KB Output is correct
26 Correct 94 ms 16044 KB Output is correct
27 Correct 265 ms 29964 KB Output is correct
28 Correct 242 ms 26372 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 11 ms 2048 KB Output is correct
9 Correct 228 ms 26024 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 11 ms 2768 KB Output is correct
13 Correct 235 ms 30000 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 7 ms 1612 KB Output is correct
17 Correct 140 ms 16172 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 60 ms 9668 KB Output is correct
22 Correct 236 ms 29996 KB Output is correct
23 Correct 231 ms 29984 KB Output is correct
24 Correct 257 ms 30020 KB Output is correct
25 Correct 94 ms 16300 KB Output is correct
26 Correct 104 ms 16936 KB Output is correct
27 Correct 229 ms 29864 KB Output is correct
28 Correct 244 ms 30020 KB Output is correct
29 Correct 96 ms 16268 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 68 ms 9636 KB Output is correct
5 Correct 254 ms 30136 KB Output is correct
6 Correct 238 ms 29552 KB Output is correct
7 Correct 263 ms 29996 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 60 ms 9616 KB Output is correct
10 Correct 236 ms 29996 KB Output is correct
11 Correct 228 ms 29964 KB Output is correct
12 Correct 239 ms 30044 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 61 ms 9556 KB Output is correct
17 Correct 259 ms 29996 KB Output is correct
18 Correct 291 ms 29744 KB Output is correct
19 Correct 234 ms 29852 KB Output is correct
20 Correct 227 ms 26308 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 11 ms 2796 KB Output is correct
7 Correct 243 ms 30020 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 10 ms 1996 KB Output is correct
13 Correct 248 ms 25964 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 1612 KB Output is correct
17 Correct 130 ms 16108 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 65 ms 9632 KB Output is correct
21 Correct 241 ms 29980 KB Output is correct
22 Correct 238 ms 29472 KB Output is correct
23 Correct 258 ms 29980 KB Output is correct
24 Correct 235 ms 26284 KB Output is correct
25 Correct 101 ms 16068 KB Output is correct
26 Correct 94 ms 16044 KB Output is correct
27 Correct 265 ms 29964 KB Output is correct
28 Correct 242 ms 26372 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 11 ms 2048 KB Output is correct
37 Correct 228 ms 26024 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 11 ms 2768 KB Output is correct
41 Correct 235 ms 30000 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 7 ms 1612 KB Output is correct
45 Correct 140 ms 16172 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 332 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 60 ms 9668 KB Output is correct
50 Correct 236 ms 29996 KB Output is correct
51 Correct 231 ms 29984 KB Output is correct
52 Correct 257 ms 30020 KB Output is correct
53 Correct 94 ms 16300 KB Output is correct
54 Correct 104 ms 16936 KB Output is correct
55 Correct 229 ms 29864 KB Output is correct
56 Correct 244 ms 30020 KB Output is correct
57 Correct 96 ms 16268 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 1 ms 332 KB Output is correct
60 Correct 8 ms 1612 KB Output is correct
61 Correct 114 ms 16068 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 59 ms 9624 KB Output is correct
66 Correct 99 ms 16560 KB Output is correct
67 Correct 113 ms 16068 KB Output is correct
68 Correct 117 ms 18460 KB Output is correct
69 Correct 96 ms 16288 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 11 ms 2796 KB Output is correct
7 Correct 243 ms 30020 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 10 ms 1996 KB Output is correct
13 Correct 248 ms 25964 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 1612 KB Output is correct
17 Correct 130 ms 16108 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 65 ms 9632 KB Output is correct
21 Correct 241 ms 29980 KB Output is correct
22 Correct 238 ms 29472 KB Output is correct
23 Correct 258 ms 29980 KB Output is correct
24 Correct 235 ms 26284 KB Output is correct
25 Correct 101 ms 16068 KB Output is correct
26 Correct 94 ms 16044 KB Output is correct
27 Correct 265 ms 29964 KB Output is correct
28 Correct 242 ms 26372 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 11 ms 2048 KB Output is correct
37 Correct 228 ms 26024 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 11 ms 2768 KB Output is correct
41 Correct 235 ms 30000 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 7 ms 1612 KB Output is correct
45 Correct 140 ms 16172 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 332 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 60 ms 9668 KB Output is correct
50 Correct 236 ms 29996 KB Output is correct
51 Correct 231 ms 29984 KB Output is correct
52 Correct 257 ms 30020 KB Output is correct
53 Correct 94 ms 16300 KB Output is correct
54 Correct 104 ms 16936 KB Output is correct
55 Correct 229 ms 29864 KB Output is correct
56 Correct 244 ms 30020 KB Output is correct
57 Correct 96 ms 16268 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 1 ms 332 KB Output is correct
60 Correct 1 ms 332 KB Output is correct
61 Correct 68 ms 9636 KB Output is correct
62 Correct 254 ms 30136 KB Output is correct
63 Correct 238 ms 29552 KB Output is correct
64 Correct 263 ms 29996 KB Output is correct
65 Correct 1 ms 332 KB Output is correct
66 Correct 60 ms 9616 KB Output is correct
67 Correct 236 ms 29996 KB Output is correct
68 Correct 228 ms 29964 KB Output is correct
69 Correct 239 ms 30044 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 61 ms 9556 KB Output is correct
74 Correct 259 ms 29996 KB Output is correct
75 Correct 291 ms 29744 KB Output is correct
76 Correct 234 ms 29852 KB Output is correct
77 Correct 227 ms 26308 KB Output is correct
78 Correct 1 ms 332 KB Output is correct
79 Correct 1 ms 332 KB Output is correct
80 Correct 8 ms 1612 KB Output is correct
81 Correct 114 ms 16068 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 59 ms 9624 KB Output is correct
86 Correct 99 ms 16560 KB Output is correct
87 Correct 113 ms 16068 KB Output is correct
88 Correct 117 ms 18460 KB Output is correct
89 Correct 96 ms 16288 KB Output is correct
90 Correct 1 ms 332 KB Output is correct
91 Correct 1 ms 332 KB Output is correct
92 Correct 4 ms 588 KB Output is correct
93 Correct 103 ms 8132 KB Output is correct
94 Correct 1 ms 332 KB Output is correct
95 Correct 1 ms 332 KB Output is correct
96 Correct 1 ms 332 KB Output is correct
97 Correct 23 ms 2476 KB Output is correct
98 Correct 90 ms 8620 KB Output is correct
99 Correct 90 ms 9388 KB Output is correct
100 Correct 91 ms 8772 KB Output is correct
101 Correct 94 ms 10796 KB Output is correct
102 Correct 86 ms 8220 KB Output is correct
103 Correct 119 ms 10184 KB Output is correct
104 Correct 92 ms 10140 KB Output is correct
105 Correct 95 ms 10116 KB Output is correct