답안 #567644

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
567644 2022-05-23T23:17:20 Z pere_gil 슈퍼트리 잇기 (IOI20_supertrees) C++17
100 / 100
239 ms 24056 KB
#include "supertrees.h"
#include "bits/stdc++.h"
using namespace std;

#define vvi vector<vector<int> >

void init(int n, vector<int> &p){
	for(int i=0;i<n;i++) p[i]=i;
}
int find_set(int n, vector<int> &p){
	return (n==p[n]) ? n : p[n]=find_set(p[n],p);
}
bool same_set(int a, int b, vector<int> &p){
	return find_set(a,p)==find_set(b,p);
}
void union_set(int a, int b, vector<int> &p){
	if(!same_set(a,b,p)) p[find_set(a,p)]=find_set(b,p);
}

void join_1(vector<int> &g, vvi &res){
	for(int i=1;i<g.size();i++)
		res[g[i-1]][g[i]]=res[g[i]][g[i-1]]=1;
}

bool join_2(vector<int> &g, vvi &res){
	if(g.size()==1) return true;
	if(g.size()<3) return false;
	for(int i=0;i<g.size();i++){
		int j=(i+1)%g.size();
		res[g[i]][g[j]]=res[g[j]][g[i]]=1;
	}
	return true;
}

bool make_group(int n, vector<int> &g, vvi &v, vvi &res){
	vector<int> p(n);
	init(n,p);

	unordered_set<int> mid;
	for(int i=0;i<g.size();i++){
		int aux=0;
		for(int j=0;j<g.size();j++)
			if(v[g[i]][g[j]]==1) union_set(g[i],g[j],p);
			else aux++;
		if(aux==g.size()-1) mid.insert(g[i]);
	}
	vvi one(n);
	for(int i=0;i<g.size();i++){
		if(mid.find(g[i])==mid.end())
			one[find_set(g[i],p)].push_back(g[i]);
		if(g[i]==find_set(g[i],p)) mid.insert(g[i]);
	}
	vector<int> v_mid(mid.begin(),mid.end());

	for(int i=0;i<n;i++)
		if(one[i].size()) join_1(one[i],res);
	return join_2(v_mid,res);
}

int construct(vvi v){
	int n=v.size();

	vector<int> p(n);
	init(n,p);
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			if(v[i][j]) union_set(i,j,p);

    vvi g(n);
	for(int i=0;i<n;i++)
		g[find_set(i,p)].push_back(i);

	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			if((same_set(i,j,p) && v[i][j]==0) || v[i][j]==3) return 0;

	vvi res(n,vector<int> (n,0));
	for(int i=0;i<n;i++)
		if(g[i].size()>0)
			if(!make_group(n,g[i],v,res)) return 0;

	build(res);
	return 1;
}

Compilation message

supertrees.cpp: In function 'void join_1(std::vector<int>&, std::vector<std::vector<int> >&)':
supertrees.cpp:21:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |  for(int i=1;i<g.size();i++)
      |              ~^~~~~~~~~
supertrees.cpp: In function 'bool join_2(std::vector<int>&, std::vector<std::vector<int> >&)':
supertrees.cpp:28:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |  for(int i=0;i<g.size();i++){
      |              ~^~~~~~~~~
supertrees.cpp: In function 'bool make_group(int, std::vector<int>&, std::vector<std::vector<int> >&, std::vector<std::vector<int> >&)':
supertrees.cpp:40:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |  for(int i=0;i<g.size();i++){
      |              ~^~~~~~~~~
supertrees.cpp:42:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |   for(int j=0;j<g.size();j++)
      |               ~^~~~~~~~~
supertrees.cpp:45:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |   if(aux==g.size()-1) mid.insert(g[i]);
      |      ~~~^~~~~~~~~~~~
supertrees.cpp:48:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |  for(int i=0;i<g.size();i++){
      |              ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 8 ms 1208 KB Output is correct
7 Correct 176 ms 23996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 8 ms 1208 KB Output is correct
7 Correct 176 ms 23996 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 300 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 11 ms 1260 KB Output is correct
13 Correct 199 ms 24020 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 692 KB Output is correct
17 Correct 105 ms 10112 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 43 ms 6168 KB Output is correct
21 Correct 177 ms 23924 KB Output is correct
22 Correct 184 ms 24056 KB Output is correct
23 Correct 187 ms 23928 KB Output is correct
24 Correct 176 ms 23932 KB Output is correct
25 Correct 78 ms 10096 KB Output is correct
26 Correct 76 ms 10112 KB Output is correct
27 Correct 206 ms 23900 KB Output is correct
28 Correct 178 ms 24012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 8 ms 1192 KB Output is correct
9 Correct 175 ms 24052 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 9 ms 1204 KB Output is correct
13 Correct 203 ms 24028 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 700 KB Output is correct
17 Correct 81 ms 10112 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 308 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 49 ms 6200 KB Output is correct
22 Correct 170 ms 24000 KB Output is correct
23 Correct 190 ms 23936 KB Output is correct
24 Correct 194 ms 24048 KB Output is correct
25 Correct 86 ms 14156 KB Output is correct
26 Correct 72 ms 10096 KB Output is correct
27 Correct 166 ms 23936 KB Output is correct
28 Correct 239 ms 24012 KB Output is correct
29 Correct 73 ms 14156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 43 ms 6168 KB Output is correct
5 Correct 198 ms 24056 KB Output is correct
6 Correct 174 ms 23932 KB Output is correct
7 Correct 190 ms 23924 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 50 ms 6224 KB Output is correct
10 Correct 175 ms 23936 KB Output is correct
11 Correct 208 ms 23980 KB Output is correct
12 Correct 186 ms 24052 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 296 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 44 ms 6156 KB Output is correct
17 Correct 170 ms 24052 KB Output is correct
18 Correct 217 ms 23924 KB Output is correct
19 Correct 178 ms 23932 KB Output is correct
20 Correct 171 ms 24012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 8 ms 1208 KB Output is correct
7 Correct 176 ms 23996 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 300 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 11 ms 1260 KB Output is correct
13 Correct 199 ms 24020 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 692 KB Output is correct
17 Correct 105 ms 10112 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 43 ms 6168 KB Output is correct
21 Correct 177 ms 23924 KB Output is correct
22 Correct 184 ms 24056 KB Output is correct
23 Correct 187 ms 23928 KB Output is correct
24 Correct 176 ms 23932 KB Output is correct
25 Correct 78 ms 10096 KB Output is correct
26 Correct 76 ms 10112 KB Output is correct
27 Correct 206 ms 23900 KB Output is correct
28 Correct 178 ms 24012 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 300 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 8 ms 1192 KB Output is correct
37 Correct 175 ms 24052 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 9 ms 1204 KB Output is correct
41 Correct 203 ms 24028 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 4 ms 700 KB Output is correct
45 Correct 81 ms 10112 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 308 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 49 ms 6200 KB Output is correct
50 Correct 170 ms 24000 KB Output is correct
51 Correct 190 ms 23936 KB Output is correct
52 Correct 194 ms 24048 KB Output is correct
53 Correct 86 ms 14156 KB Output is correct
54 Correct 72 ms 10096 KB Output is correct
55 Correct 166 ms 23936 KB Output is correct
56 Correct 239 ms 24012 KB Output is correct
57 Correct 73 ms 14156 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 1 ms 296 KB Output is correct
60 Correct 4 ms 596 KB Output is correct
61 Correct 85 ms 10148 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 1 ms 212 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Correct 44 ms 6256 KB Output is correct
66 Correct 71 ms 10112 KB Output is correct
67 Correct 70 ms 10100 KB Output is correct
68 Correct 68 ms 10104 KB Output is correct
69 Correct 71 ms 14064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 8 ms 1208 KB Output is correct
7 Correct 176 ms 23996 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 300 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 11 ms 1260 KB Output is correct
13 Correct 199 ms 24020 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 692 KB Output is correct
17 Correct 105 ms 10112 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 43 ms 6168 KB Output is correct
21 Correct 177 ms 23924 KB Output is correct
22 Correct 184 ms 24056 KB Output is correct
23 Correct 187 ms 23928 KB Output is correct
24 Correct 176 ms 23932 KB Output is correct
25 Correct 78 ms 10096 KB Output is correct
26 Correct 76 ms 10112 KB Output is correct
27 Correct 206 ms 23900 KB Output is correct
28 Correct 178 ms 24012 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 300 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 8 ms 1192 KB Output is correct
37 Correct 175 ms 24052 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 9 ms 1204 KB Output is correct
41 Correct 203 ms 24028 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 4 ms 700 KB Output is correct
45 Correct 81 ms 10112 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 308 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 49 ms 6200 KB Output is correct
50 Correct 170 ms 24000 KB Output is correct
51 Correct 190 ms 23936 KB Output is correct
52 Correct 194 ms 24048 KB Output is correct
53 Correct 86 ms 14156 KB Output is correct
54 Correct 72 ms 10096 KB Output is correct
55 Correct 166 ms 23936 KB Output is correct
56 Correct 239 ms 24012 KB Output is correct
57 Correct 73 ms 14156 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 43 ms 6168 KB Output is correct
62 Correct 198 ms 24056 KB Output is correct
63 Correct 174 ms 23932 KB Output is correct
64 Correct 190 ms 23924 KB Output is correct
65 Correct 1 ms 212 KB Output is correct
66 Correct 50 ms 6224 KB Output is correct
67 Correct 175 ms 23936 KB Output is correct
68 Correct 208 ms 23980 KB Output is correct
69 Correct 186 ms 24052 KB Output is correct
70 Correct 0 ms 212 KB Output is correct
71 Correct 0 ms 296 KB Output is correct
72 Correct 1 ms 212 KB Output is correct
73 Correct 44 ms 6156 KB Output is correct
74 Correct 170 ms 24052 KB Output is correct
75 Correct 217 ms 23924 KB Output is correct
76 Correct 178 ms 23932 KB Output is correct
77 Correct 171 ms 24012 KB Output is correct
78 Correct 1 ms 212 KB Output is correct
79 Correct 1 ms 296 KB Output is correct
80 Correct 4 ms 596 KB Output is correct
81 Correct 85 ms 10148 KB Output is correct
82 Correct 1 ms 212 KB Output is correct
83 Correct 1 ms 212 KB Output is correct
84 Correct 1 ms 212 KB Output is correct
85 Correct 44 ms 6256 KB Output is correct
86 Correct 71 ms 10112 KB Output is correct
87 Correct 70 ms 10100 KB Output is correct
88 Correct 68 ms 10104 KB Output is correct
89 Correct 71 ms 14064 KB Output is correct
90 Correct 1 ms 212 KB Output is correct
91 Correct 1 ms 296 KB Output is correct
92 Correct 4 ms 672 KB Output is correct
93 Correct 91 ms 10100 KB Output is correct
94 Correct 1 ms 212 KB Output is correct
95 Correct 1 ms 304 KB Output is correct
96 Correct 1 ms 300 KB Output is correct
97 Correct 18 ms 2680 KB Output is correct
98 Correct 70 ms 10108 KB Output is correct
99 Correct 73 ms 10100 KB Output is correct
100 Correct 74 ms 10096 KB Output is correct
101 Correct 75 ms 10100 KB Output is correct
102 Correct 72 ms 10108 KB Output is correct
103 Correct 77 ms 10144 KB Output is correct
104 Correct 83 ms 10152 KB Output is correct
105 Correct 90 ms 10104 KB Output is correct