Submission #432095

# Submission time Handle Problem Language Result Execution time Memory
432095 2021-06-17T20:45:10 Z peuch Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
269 ms 24236 KB
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1010;

int rep[MAXN];
vector<int> grp[MAXN];

int rep2[MAXN];
vector<int> grp2[MAXN];

int find(int a);
void uni(int a, int b);
int find2(int a);
void uni2(int a, int b);

int construct(vector<vector<int> > p) {
	int n = p.size();
	vector<vector<int> > ans (n, vector<int> (n));
	
	for(int i = 0; i < n; i++){
		rep[i] = i;
		grp[i].push_back(i);
	}
	
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			if(p[i][j] == 0) continue;
			uni(i, j);
		}
	}
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			if(p[i][j] != 0) continue;
			if(find(i) == find(j)) return 0;
		}
	}
	
	for(int i = 0; i < n; i++){
		if(i != rep[i]) continue;
		bool flag1 = true;
		for(int j = 0; j < grp[i].size(); j++){
			for(int k = 0; k < grp[i].size(); k++){
				int a = grp[i][j], b = grp[i][k];
				if(p[a][b] == 0) return 0;
				flag1 &= p[a][b] == 1; 
			}
		}
		if(flag1){
			for(int j = 1; j < grp[i].size(); j++){
				int a = grp[i][j], b = grp[i][j - 1];
				ans[a][b] = 1;
				ans[b][a] = 1;
			}
		}
		else{
			vector<int> aux;
			for(int j = 0; j < grp[i].size(); j++){
				int a = grp[i][j];
				rep2[a] = a;
				grp2[a].push_back(a);
			}
			for(int j = 0; j < grp[i].size(); j++){
				for(int k = 0; k < grp[i].size(); k++){
					int a = grp[i][j], b = grp[i][k];
					if(p[a][b] == 2) continue;
					uni2(a, b);
				}
			}
			for(int j = 0; j < grp[i].size(); j++){
				for(int k = 0; k < grp[i].size(); k++){
					int a = grp[i][j], b = grp[i][k];
					if(p[a][b] != 2) continue;
					if(find2(a) == find2(b)) return 0;
				}
			}
			for(int j = 0; j < grp[i].size(); j++){
				int a = grp[i][j];
				if(a != rep2[a]) continue;
				aux.push_back(a);
				for(int k = 0; k < grp2[a].size(); k++){
					for(int l = 0; l < grp2[a].size(); l++){
						int b = grp2[a][k], c = grp2[a][l];
						if(p[a][b] != 1) return 0;
					}
				}
				for(int k = 1; k < grp2[a].size(); k++){
					int b = grp2[a][k], c = grp2[a][k - 1];
					ans[b][c] = 1;
					ans[c][b] = 1;
				}
			}
			if(aux.size() <= 2) return 0;
			for(int j = 1; j < aux.size(); j++){
				int b = aux[j], c = aux[j - 1];
				ans[b][c] = 1;
				ans[c][b] = 1;
			}
			ans[aux[0]][aux.back()] = 1;
			ans[aux.back()][aux[0]] = 1;
		}
	}
	
	build(ans);
	return 1;
}

int find(int a){
	if(a == rep[a]) return a;
	return rep[a] = find(rep[a]);
}

void uni(int a, int b){
	a = find(a);
	b = find(b);
	if(a == b) return;
	if(grp[a].size() < grp[b].size()) swap(a, b);
	for(int i = 0; i < grp[b].size(); i++)
		grp[a].push_back(grp[b][i]);
	grp[b].clear();
	rep[b] = a;
}

int find2(int a){
	if(a == rep2[a]) return a;
	return rep2[a] = find2(rep2[a]);
}

void uni2(int a, int b){
	a = find2(a);
	b = find2(b);
	if(a == b) return;
	if(grp2[a].size() < grp2[b].size()) swap(a, b);
	for(int i = 0; i < grp2[b].size(); i++)
		grp2[a].push_back(grp2[b][i]);
	grp2[b].clear();
	rep2[b] = a;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:43:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |   for(int j = 0; j < grp[i].size(); j++){
      |                  ~~^~~~~~~~~~~~~~~
supertrees.cpp:44:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |    for(int k = 0; k < grp[i].size(); k++){
      |                   ~~^~~~~~~~~~~~~~~
supertrees.cpp:51:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |    for(int j = 1; j < grp[i].size(); j++){
      |                   ~~^~~~~~~~~~~~~~~
supertrees.cpp:59:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |    for(int j = 0; j < grp[i].size(); j++){
      |                   ~~^~~~~~~~~~~~~~~
supertrees.cpp:64:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |    for(int j = 0; j < grp[i].size(); j++){
      |                   ~~^~~~~~~~~~~~~~~
supertrees.cpp:65:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |     for(int k = 0; k < grp[i].size(); k++){
      |                    ~~^~~~~~~~~~~~~~~
supertrees.cpp:71:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |    for(int j = 0; j < grp[i].size(); j++){
      |                   ~~^~~~~~~~~~~~~~~
supertrees.cpp:72:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |     for(int k = 0; k < grp[i].size(); k++){
      |                    ~~^~~~~~~~~~~~~~~
supertrees.cpp:78:21: 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 < grp[i].size(); j++){
      |                   ~~^~~~~~~~~~~~~~~
supertrees.cpp:82:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |     for(int k = 0; k < grp2[a].size(); k++){
      |                    ~~^~~~~~~~~~~~~~~~
supertrees.cpp:83:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |      for(int l = 0; l < grp2[a].size(); l++){
      |                     ~~^~~~~~~~~~~~~~~~
supertrees.cpp:84:27: warning: unused variable 'c' [-Wunused-variable]
   84 |       int b = grp2[a][k], c = grp2[a][l];
      |                           ^
supertrees.cpp:88:22: 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 = 1; k < grp2[a].size(); k++){
      |                    ~~^~~~~~~~~~~~~~~~
supertrees.cpp:95:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |    for(int j = 1; j < aux.size(); j++){
      |                   ~~^~~~~~~~~~~~
supertrees.cpp: In function 'void uni(int, int)':
supertrees.cpp:119:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  119 |  for(int i = 0; i < grp[b].size(); i++)
      |                 ~~^~~~~~~~~~~~~~~
supertrees.cpp: In function 'void uni2(int, int)':
supertrees.cpp:135:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  135 |  for(int i = 0; i < grp2[b].size(); i++)
      |                 ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory 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 0 ms 332 KB Output is correct
6 Correct 10 ms 1196 KB Output is correct
7 Correct 236 ms 22076 KB Output is correct
# Verdict Execution time Memory 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 0 ms 332 KB Output is correct
6 Correct 10 ms 1196 KB Output is correct
7 Correct 236 ms 22076 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 9 ms 1236 KB Output is correct
13 Correct 226 ms 22076 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 716 KB Output is correct
17 Correct 107 ms 12224 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 57 ms 5716 KB Output is correct
21 Correct 269 ms 22064 KB Output is correct
22 Correct 235 ms 22080 KB Output is correct
23 Correct 247 ms 22076 KB Output is correct
24 Correct 231 ms 22212 KB Output is correct
25 Correct 95 ms 12224 KB Output is correct
26 Correct 92 ms 12100 KB Output is correct
27 Correct 243 ms 22080 KB Output is correct
28 Correct 225 ms 22104 KB Output is correct
# Verdict Execution time Memory 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 344 KB Output is correct
8 Correct 10 ms 1228 KB Output is correct
9 Correct 240 ms 24032 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 1228 KB Output is correct
13 Correct 246 ms 24056 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 872 KB Output is correct
17 Correct 116 ms 14148 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 71 ms 6304 KB Output is correct
22 Correct 233 ms 24236 KB Output is correct
23 Correct 248 ms 24064 KB Output is correct
24 Correct 254 ms 24132 KB Output is correct
25 Correct 105 ms 14148 KB Output is correct
26 Correct 98 ms 14116 KB Output is correct
27 Correct 230 ms 24128 KB Output is correct
28 Correct 251 ms 24112 KB Output is correct
29 Correct 114 ms 14128 KB Output is correct
# Verdict Execution time Memory 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 70 ms 6280 KB Output is correct
5 Correct 244 ms 23360 KB Output is correct
6 Correct 234 ms 23364 KB Output is correct
7 Correct 250 ms 23392 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 62 ms 6248 KB Output is correct
10 Correct 237 ms 23500 KB Output is correct
11 Correct 227 ms 23364 KB Output is correct
12 Correct 252 ms 23432 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 58 ms 5812 KB Output is correct
17 Correct 240 ms 24096 KB Output is correct
18 Correct 236 ms 24004 KB Output is correct
19 Correct 235 ms 24004 KB Output is correct
20 Correct 234 ms 24132 KB Output is correct
# Verdict Execution time Memory 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 0 ms 332 KB Output is correct
6 Correct 10 ms 1196 KB Output is correct
7 Correct 236 ms 22076 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 9 ms 1236 KB Output is correct
13 Correct 226 ms 22076 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 716 KB Output is correct
17 Correct 107 ms 12224 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 57 ms 5716 KB Output is correct
21 Correct 269 ms 22064 KB Output is correct
22 Correct 235 ms 22080 KB Output is correct
23 Correct 247 ms 22076 KB Output is correct
24 Correct 231 ms 22212 KB Output is correct
25 Correct 95 ms 12224 KB Output is correct
26 Correct 92 ms 12100 KB Output is correct
27 Correct 243 ms 22080 KB Output is correct
28 Correct 225 ms 22104 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 344 KB Output is correct
36 Correct 10 ms 1228 KB Output is correct
37 Correct 240 ms 24032 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 10 ms 1228 KB Output is correct
41 Correct 246 ms 24056 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 872 KB Output is correct
45 Correct 116 ms 14148 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 71 ms 6304 KB Output is correct
50 Correct 233 ms 24236 KB Output is correct
51 Correct 248 ms 24064 KB Output is correct
52 Correct 254 ms 24132 KB Output is correct
53 Correct 105 ms 14148 KB Output is correct
54 Correct 98 ms 14116 KB Output is correct
55 Correct 230 ms 24128 KB Output is correct
56 Correct 251 ms 24112 KB Output is correct
57 Correct 114 ms 14128 KB Output is correct
58 Correct 0 ms 332 KB Output is correct
59 Correct 1 ms 332 KB Output is correct
60 Correct 5 ms 844 KB Output is correct
61 Correct 109 ms 14116 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 344 KB Output is correct
65 Correct 58 ms 6312 KB Output is correct
66 Correct 98 ms 14128 KB Output is correct
67 Correct 98 ms 14112 KB Output is correct
68 Correct 94 ms 14128 KB Output is correct
69 Correct 116 ms 14128 KB Output is correct
# Verdict Execution time Memory 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 0 ms 332 KB Output is correct
6 Correct 10 ms 1196 KB Output is correct
7 Correct 236 ms 22076 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 9 ms 1236 KB Output is correct
13 Correct 226 ms 22076 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 716 KB Output is correct
17 Correct 107 ms 12224 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 57 ms 5716 KB Output is correct
21 Correct 269 ms 22064 KB Output is correct
22 Correct 235 ms 22080 KB Output is correct
23 Correct 247 ms 22076 KB Output is correct
24 Correct 231 ms 22212 KB Output is correct
25 Correct 95 ms 12224 KB Output is correct
26 Correct 92 ms 12100 KB Output is correct
27 Correct 243 ms 22080 KB Output is correct
28 Correct 225 ms 22104 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 344 KB Output is correct
36 Correct 10 ms 1228 KB Output is correct
37 Correct 240 ms 24032 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 10 ms 1228 KB Output is correct
41 Correct 246 ms 24056 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 872 KB Output is correct
45 Correct 116 ms 14148 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 71 ms 6304 KB Output is correct
50 Correct 233 ms 24236 KB Output is correct
51 Correct 248 ms 24064 KB Output is correct
52 Correct 254 ms 24132 KB Output is correct
53 Correct 105 ms 14148 KB Output is correct
54 Correct 98 ms 14116 KB Output is correct
55 Correct 230 ms 24128 KB Output is correct
56 Correct 251 ms 24112 KB Output is correct
57 Correct 114 ms 14128 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 70 ms 6280 KB Output is correct
62 Correct 244 ms 23360 KB Output is correct
63 Correct 234 ms 23364 KB Output is correct
64 Correct 250 ms 23392 KB Output is correct
65 Correct 1 ms 332 KB Output is correct
66 Correct 62 ms 6248 KB Output is correct
67 Correct 237 ms 23500 KB Output is correct
68 Correct 227 ms 23364 KB Output is correct
69 Correct 252 ms 23432 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 58 ms 5812 KB Output is correct
74 Correct 240 ms 24096 KB Output is correct
75 Correct 236 ms 24004 KB Output is correct
76 Correct 235 ms 24004 KB Output is correct
77 Correct 234 ms 24132 KB Output is correct
78 Correct 0 ms 332 KB Output is correct
79 Correct 1 ms 332 KB Output is correct
80 Correct 5 ms 844 KB Output is correct
81 Correct 109 ms 14116 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 344 KB Output is correct
85 Correct 58 ms 6312 KB Output is correct
86 Correct 98 ms 14128 KB Output is correct
87 Correct 98 ms 14112 KB Output is correct
88 Correct 94 ms 14128 KB Output is correct
89 Correct 116 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 844 KB Output is correct
93 Correct 105 ms 14128 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 24 ms 3788 KB Output is correct
98 Correct 96 ms 14116 KB Output is correct
99 Correct 111 ms 14176 KB Output is correct
100 Correct 125 ms 14124 KB Output is correct
101 Correct 95 ms 14116 KB Output is correct
102 Correct 112 ms 14120 KB Output is correct
103 Correct 115 ms 14124 KB Output is correct
104 Correct 125 ms 14112 KB Output is correct
105 Correct 115 ms 14168 KB Output is correct