Submission #812880

# Submission time Handle Problem Language Result Execution time Memory
812880 2023-08-07T11:34:30 Z KemalK Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
178 ms 24012 KB
#include "supertrees.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
int get(int x, vector<int> &root)
{
    if (x == root[x]) return x;
    return root[x] = get(root[x], root);
}
void unit(int a, int b, vector<int> &root, vector<int> &sz){
	a = get(a, root);
	b = get(b, root);
	if (a != b){
		if (sz[a] < sz[b]){
			swap(a, b);
		}
		root[b] = a;
		sz[a] += sz[b];
	}
}
int construct(std::vector<std::vector<int>> p) {
	int n = p.size();
	vector<int> root(n), sz(n);
	for (int i = 0; i < n; i++) root[i] = i, sz[i] = 1;
	for (int i = 0; i < n; i++){
		root[i] = i; sz[i] = 1;
	}
	int check = 0;
	for (int i = 0; i < n; i++){
		for (int j = 0; j < n; j++){
			if (p[i][j] == 3){
				return 0;
			}
			if (p[i][j]){
				unit(i, j, root, sz);
				// check = max(check, p[i][j]);
			}
		}
	}
	for (int i = 0; i < n; i++){
		for (int j = 0; j < n; j++){
			if (p[i][j] == 0 and get(i, root) == get(j, root)){ //impossible
				return 0;
			}
		}
	}
	vector <vector<int>> answer(n, vector<int>(n));
	// if(!check){
	// 	build(answer);
	// 	return 1;	
	// }
	for (int i = 0; i < n; i++){
		vector <int> branch;
		for (int j = 0; j < n; j++){
			if (get(j, root) == i){
				branch.push_back(j);
			}
		}
		vector <int> root2(branch.size()), sz2(branch.size(), 1);
		for (int j = 0; j < branch.size(); j++){
			root2[j] = j;
		}
		for (int j = 0; j < branch.size(); j++){
			for (int k = 0; k < branch.size(); k++){
				if (p[branch[j]][branch[k]] == 1){
					unit(j, k, root2, sz2);
				}
			}
		}
		for (int j = 0; j < branch.size(); j++){
			for (int k = 0; k < branch.size(); k++){
				if (p[branch[j]][branch[k]] == 2 and get(j, root2) == get(k, root2)){
					return 0;
				}
			}
		}
		vector <vector <int>> forest(branch.size());
		for (int j = 0; j < branch.size(); j++){
			forest[get(j, root2)].push_back(branch[j]);
		}
		vector <int> q;
		for (int j = 0; j < forest.size(); j++){
			if (forest[j].size()){
				q.push_back(forest[j][0]);
				// cout << "node: " << forest[j][0] << "\n";
				// cout << forest[j].size() << "\n";
			}
			for (int k = 0; k + 1 < forest[j].size(); k++){
				// cout << forest[j][k] << " " << forest[j][k + 1] << "\n";
				answer[forest[j][k]][forest[j][k + 1]] = 1;
				answer[forest[j][k + 1]][forest[j][k]] = 1;
			}
		}
		if (q.size() == 2){
			return 0;
		}	
		if (q.size() > 2){
			for (int j = 0; j < q.size(); j++){
				answer[q[j]][q[(j + 1)%q.size()]] = 1;
				answer[q[(j + 1)%q.size()]][q[j]] = 1;
			}
		}
		// for (int j = 0; j + 1 < branch.size(); j++){
		// 	answer[branch[j]][branch[j + 1]] = 1;
		// 	answer[branch[j + 1]][branch[j]] = 1;
		// }
		// if (check == 2){
		// 	if(branch.size() == 2){
		// 		return 0;
		// 	}
		// 	if (branch.size() > 2){
		// 		answer[branch[0]][branch.back()] = 1;
		// 		answer[branch.back()][branch[0]] = 1;
		// 	}
		// }
	}
	build(answer);
	return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:60:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |   for (int j = 0; j < branch.size(); j++){
      |                   ~~^~~~~~~~~~~~~~~
supertrees.cpp:63:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |   for (int j = 0; j < branch.size(); j++){
      |                   ~~^~~~~~~~~~~~~~~
supertrees.cpp:64:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |    for (int k = 0; k < branch.size(); k++){
      |                    ~~^~~~~~~~~~~~~~~
supertrees.cpp:70:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |   for (int j = 0; j < branch.size(); j++){
      |                   ~~^~~~~~~~~~~~~~~
supertrees.cpp:71:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |    for (int k = 0; k < branch.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 < branch.size(); j++){
      |                   ~~^~~~~~~~~~~~~~~
supertrees.cpp:82:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |   for (int j = 0; j < forest.size(); j++){
      |                   ~~^~~~~~~~~~~~~~~
supertrees.cpp:88:26: 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 = 0; k + 1 < forest[j].size(); k++){
      |                    ~~~~~~^~~~~~~~~~~~~~~~~~
supertrees.cpp:98:22: 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 < q.size(); j++){
      |                    ~~^~~~~~~~~~
supertrees.cpp:28:6: warning: unused variable 'check' [-Wunused-variable]
   28 |  int check = 0;
      |      ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 300 KB Output is correct
6 Correct 7 ms 1236 KB Output is correct
7 Correct 161 ms 22220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 300 KB Output is correct
6 Correct 7 ms 1236 KB Output is correct
7 Correct 161 ms 22220 KB Output is correct
8 Correct 0 ms 304 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 300 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 7 ms 1236 KB Output is correct
13 Correct 149 ms 22248 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 596 KB Output is correct
17 Correct 69 ms 8412 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 39 ms 6020 KB Output is correct
21 Correct 157 ms 22272 KB Output is correct
22 Correct 160 ms 22280 KB Output is correct
23 Correct 166 ms 22228 KB Output is correct
24 Correct 156 ms 22240 KB Output is correct
25 Correct 62 ms 8328 KB Output is correct
26 Correct 57 ms 8320 KB Output is correct
27 Correct 161 ms 22288 KB Output is correct
28 Correct 147 ms 22220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 300 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 300 KB Output is correct
8 Correct 6 ms 1236 KB Output is correct
9 Correct 152 ms 22272 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 7 ms 1204 KB Output is correct
13 Correct 165 ms 22276 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 696 KB Output is correct
17 Correct 67 ms 8332 KB Output is correct
18 Correct 0 ms 300 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 300 KB Output is correct
21 Correct 39 ms 6020 KB Output is correct
22 Correct 152 ms 22276 KB Output is correct
23 Correct 156 ms 22468 KB Output is correct
24 Correct 157 ms 22276 KB Output is correct
25 Correct 62 ms 12340 KB Output is correct
26 Correct 58 ms 8324 KB Output is correct
27 Correct 156 ms 22396 KB Output is correct
28 Correct 164 ms 22204 KB Output is correct
29 Correct 62 ms 12292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 40 ms 5964 KB Output is correct
5 Correct 153 ms 22228 KB Output is correct
6 Correct 153 ms 22256 KB Output is correct
7 Correct 178 ms 22276 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 39 ms 5964 KB Output is correct
10 Correct 160 ms 22348 KB Output is correct
11 Correct 150 ms 22304 KB Output is correct
12 Correct 160 ms 22240 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 39 ms 6004 KB Output is correct
17 Correct 153 ms 22288 KB Output is correct
18 Correct 162 ms 22276 KB Output is correct
19 Correct 151 ms 24012 KB Output is correct
20 Correct 150 ms 23956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 300 KB Output is correct
6 Correct 7 ms 1236 KB Output is correct
7 Correct 161 ms 22220 KB Output is correct
8 Correct 0 ms 304 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 300 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 7 ms 1236 KB Output is correct
13 Correct 149 ms 22248 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 596 KB Output is correct
17 Correct 69 ms 8412 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 39 ms 6020 KB Output is correct
21 Correct 157 ms 22272 KB Output is correct
22 Correct 160 ms 22280 KB Output is correct
23 Correct 166 ms 22228 KB Output is correct
24 Correct 156 ms 22240 KB Output is correct
25 Correct 62 ms 8328 KB Output is correct
26 Correct 57 ms 8320 KB Output is correct
27 Correct 161 ms 22288 KB Output is correct
28 Correct 147 ms 22220 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 300 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 300 KB Output is correct
36 Correct 6 ms 1236 KB Output is correct
37 Correct 152 ms 22272 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 7 ms 1204 KB Output is correct
41 Correct 165 ms 22276 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 4 ms 696 KB Output is correct
45 Correct 67 ms 8332 KB Output is correct
46 Correct 0 ms 300 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 300 KB Output is correct
49 Correct 39 ms 6020 KB Output is correct
50 Correct 152 ms 22276 KB Output is correct
51 Correct 156 ms 22468 KB Output is correct
52 Correct 157 ms 22276 KB Output is correct
53 Correct 62 ms 12340 KB Output is correct
54 Correct 58 ms 8324 KB Output is correct
55 Correct 156 ms 22396 KB Output is correct
56 Correct 164 ms 22204 KB Output is correct
57 Correct 62 ms 12292 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 0 ms 272 KB Output is correct
60 Correct 3 ms 596 KB Output is correct
61 Correct 67 ms 8332 KB Output is correct
62 Correct 0 ms 212 KB Output is correct
63 Correct 1 ms 212 KB Output is correct
64 Correct 1 ms 300 KB Output is correct
65 Correct 38 ms 5940 KB Output is correct
66 Correct 58 ms 10108 KB Output is correct
67 Correct 61 ms 10188 KB Output is correct
68 Correct 57 ms 10100 KB Output is correct
69 Correct 61 ms 14080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 300 KB Output is correct
6 Correct 7 ms 1236 KB Output is correct
7 Correct 161 ms 22220 KB Output is correct
8 Correct 0 ms 304 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 300 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 7 ms 1236 KB Output is correct
13 Correct 149 ms 22248 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 596 KB Output is correct
17 Correct 69 ms 8412 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 39 ms 6020 KB Output is correct
21 Correct 157 ms 22272 KB Output is correct
22 Correct 160 ms 22280 KB Output is correct
23 Correct 166 ms 22228 KB Output is correct
24 Correct 156 ms 22240 KB Output is correct
25 Correct 62 ms 8328 KB Output is correct
26 Correct 57 ms 8320 KB Output is correct
27 Correct 161 ms 22288 KB Output is correct
28 Correct 147 ms 22220 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 300 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 300 KB Output is correct
36 Correct 6 ms 1236 KB Output is correct
37 Correct 152 ms 22272 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 7 ms 1204 KB Output is correct
41 Correct 165 ms 22276 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 4 ms 696 KB Output is correct
45 Correct 67 ms 8332 KB Output is correct
46 Correct 0 ms 300 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 300 KB Output is correct
49 Correct 39 ms 6020 KB Output is correct
50 Correct 152 ms 22276 KB Output is correct
51 Correct 156 ms 22468 KB Output is correct
52 Correct 157 ms 22276 KB Output is correct
53 Correct 62 ms 12340 KB Output is correct
54 Correct 58 ms 8324 KB Output is correct
55 Correct 156 ms 22396 KB Output is correct
56 Correct 164 ms 22204 KB Output is correct
57 Correct 62 ms 12292 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 40 ms 5964 KB Output is correct
62 Correct 153 ms 22228 KB Output is correct
63 Correct 153 ms 22256 KB Output is correct
64 Correct 178 ms 22276 KB Output is correct
65 Correct 0 ms 212 KB Output is correct
66 Correct 39 ms 5964 KB Output is correct
67 Correct 160 ms 22348 KB Output is correct
68 Correct 150 ms 22304 KB Output is correct
69 Correct 160 ms 22240 KB Output is correct
70 Correct 1 ms 212 KB Output is correct
71 Correct 0 ms 212 KB Output is correct
72 Correct 1 ms 212 KB Output is correct
73 Correct 39 ms 6004 KB Output is correct
74 Correct 153 ms 22288 KB Output is correct
75 Correct 162 ms 22276 KB Output is correct
76 Correct 151 ms 24012 KB Output is correct
77 Correct 150 ms 23956 KB Output is correct
78 Correct 1 ms 212 KB Output is correct
79 Correct 0 ms 272 KB Output is correct
80 Correct 3 ms 596 KB Output is correct
81 Correct 67 ms 8332 KB Output is correct
82 Correct 0 ms 212 KB Output is correct
83 Correct 1 ms 212 KB Output is correct
84 Correct 1 ms 300 KB Output is correct
85 Correct 38 ms 5940 KB Output is correct
86 Correct 58 ms 10108 KB Output is correct
87 Correct 61 ms 10188 KB Output is correct
88 Correct 57 ms 10100 KB Output is correct
89 Correct 61 ms 14080 KB Output is correct
90 Correct 0 ms 212 KB Output is correct
91 Correct 0 ms 212 KB Output is correct
92 Correct 3 ms 696 KB Output is correct
93 Correct 59 ms 10064 KB Output is correct
94 Correct 1 ms 296 KB Output is correct
95 Correct 0 ms 212 KB Output is correct
96 Correct 0 ms 308 KB Output is correct
97 Correct 14 ms 2740 KB Output is correct
98 Correct 61 ms 10096 KB Output is correct
99 Correct 63 ms 10100 KB Output is correct
100 Correct 58 ms 10104 KB Output is correct
101 Correct 60 ms 10096 KB Output is correct
102 Correct 56 ms 10064 KB Output is correct
103 Correct 56 ms 10112 KB Output is correct
104 Correct 60 ms 10060 KB Output is correct
105 Correct 59 ms 10112 KB Output is correct