Submission #812778

# Submission time Handle Problem Language Result Execution time Memory
812778 2023-08-07T11:00:51 Z KemalK Connecting Supertrees (IOI20_supertrees) C++17
40 / 100
199 ms 22040 KB
#include "supertrees.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int pr[N];
int sz[N];
int get(int x){
	if (pr[x] == x){
		return x;
	}
	return (pr[x] = get(pr[x]));
}
void unit(int a, int b){
	a = get(a);
	b = get(b);
	if (a != b){
		if (sz[a] < sz[b]){
			swap(a, b);
		}
		pr[b] = a;
		sz[a] += sz[b];
	}
}
int get2(int x, vector<int> &pr2){
	if (pr2[x] == x){
		return x;
	}
	return (pr2[x] = get(pr2[x]));
}
void unit2(int a, int b, vector <int> &pr2, vector <int> &sz2){
	a = get2(a, pr2);
	b = get2(b, pr2);
	if (a != b){
		if (sz2[a] < sz2[b]){
			swap(a, b);
		}
		pr2[b] = a;
		sz2[a] += sz2[b];
	}
}
int construct(std::vector<std::vector<int>> p) {
	int n = p.size();
	for (int i = 0; i < n; i++){
		pr[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]){
				unit(i, j);
				// 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) == get(j)){ //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) == i){
				branch.push_back(j);
			}
		}
		vector <int> pr2(branch.size()), sz2(branch.size(), 1);
		for (int j = 0; j < branch.size(); j++){
			pr2[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){
					unit2(j, k, pr2, 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 get2(j, pr2) == get2(k, pr2)){
					return 0;
				}
			}
		}
		vector <vector <int>> forest(branch.size());
		for (int i = 0; i < branch.size(); i++){
			forest[get2(i, pr2)].push_back(branch[i]);
		}
		vector <int> q;
		for (int j = 0; j < forest.size(); j++){
			if (forest[j].size()){
				q.push_back(forest[j][0]);
			}
			for (int k = 0; k + 1 < forest[j].size(); k++){
				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 i = 0; i < q.size(); i++){
				answer[q[i]][q[(i + 1)%q.size()]] = 1;
				answer[q[(i + 1)%q.size()]][q[i]] = 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:76:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |   for (int j = 0; j < branch.size(); j++){
      |                   ~~^~~~~~~~~~~~~~~
supertrees.cpp:79:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |   for (int j = 0; j < branch.size(); j++){
      |                   ~~^~~~~~~~~~~~~~~
supertrees.cpp:80:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |    for (int k = 0; k < branch.size(); k++){
      |                    ~~^~~~~~~~~~~~~~~
supertrees.cpp:86:21: 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 < branch.size(); j++){
      |                   ~~^~~~~~~~~~~~~~~
supertrees.cpp:87:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |    for (int k = 0; k < branch.size(); k++){
      |                    ~~^~~~~~~~~~~~~~~
supertrees.cpp:94:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |   for (int i = 0; i < branch.size(); i++){
      |                   ~~^~~~~~~~~~~~~~~
supertrees.cpp:98:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |   for (int j = 0; j < forest.size(); j++){
      |                   ~~^~~~~~~~~~~~~~~
supertrees.cpp:102:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |    for (int k = 0; k + 1 < forest[j].size(); k++){
      |                    ~~~~~~^~~~~~~~~~~~~~~~~~
supertrees.cpp:111:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  111 |    for (int i = 0; i < q.size(); i++){
      |                    ~~^~~~~~~~~~
supertrees.cpp:47:6: warning: unused variable 'check' [-Wunused-variable]
   47 |  int check = 0;
      |      ^~~~~
# 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 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1108 KB Output is correct
7 Correct 164 ms 22024 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 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1108 KB Output is correct
7 Correct 164 ms 22024 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 7 ms 1132 KB Output is correct
13 Correct 151 ms 22016 KB Output is correct
14 Correct 0 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 68 ms 8068 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 39 ms 5776 KB Output is correct
21 Correct 199 ms 22020 KB Output is correct
22 Correct 156 ms 21952 KB Output is correct
23 Correct 179 ms 21988 KB Output is correct
24 Correct 156 ms 21972 KB Output is correct
25 Correct 71 ms 8076 KB Output is correct
26 Correct 57 ms 8088 KB Output is correct
27 Correct 169 ms 22024 KB Output is correct
28 Correct 153 ms 22020 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 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 6 ms 1108 KB Output is correct
9 Correct 150 ms 22024 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 7 ms 1108 KB Output is correct
13 Correct 165 ms 22032 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 600 KB Output is correct
17 Correct 71 ms 8080 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 39 ms 5692 KB Output is correct
22 Correct 161 ms 22016 KB Output is correct
23 Correct 154 ms 22040 KB Output is correct
24 Correct 161 ms 22036 KB Output is correct
25 Correct 63 ms 12036 KB Output is correct
26 Correct 58 ms 8068 KB Output is correct
27 Correct 156 ms 22024 KB Output is correct
28 Correct 163 ms 22032 KB Output is correct
29 Correct 62 ms 12044 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 43 ms 5684 KB Output is correct
5 Correct 161 ms 22012 KB Output is correct
6 Correct 154 ms 21964 KB Output is correct
7 Correct 163 ms 21944 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 43 ms 5708 KB Output is correct
10 Correct 156 ms 22036 KB Output is correct
11 Correct 159 ms 22036 KB Output is correct
12 Correct 162 ms 21936 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 39 ms 5772 KB Output is correct
17 Correct 158 ms 22028 KB Output is correct
18 Incorrect 64 ms 12056 KB Answer gives possible 0 while actual possible 1
19 Halted 0 ms 0 KB -
# 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 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1108 KB Output is correct
7 Correct 164 ms 22024 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 7 ms 1132 KB Output is correct
13 Correct 151 ms 22016 KB Output is correct
14 Correct 0 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 68 ms 8068 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 39 ms 5776 KB Output is correct
21 Correct 199 ms 22020 KB Output is correct
22 Correct 156 ms 21952 KB Output is correct
23 Correct 179 ms 21988 KB Output is correct
24 Correct 156 ms 21972 KB Output is correct
25 Correct 71 ms 8076 KB Output is correct
26 Correct 57 ms 8088 KB Output is correct
27 Correct 169 ms 22024 KB Output is correct
28 Correct 153 ms 22020 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 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 6 ms 1108 KB Output is correct
37 Correct 150 ms 22024 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 7 ms 1108 KB Output is correct
41 Correct 165 ms 22032 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 3 ms 600 KB Output is correct
45 Correct 71 ms 8080 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 39 ms 5692 KB Output is correct
50 Correct 161 ms 22016 KB Output is correct
51 Correct 154 ms 22040 KB Output is correct
52 Correct 161 ms 22036 KB Output is correct
53 Correct 63 ms 12036 KB Output is correct
54 Correct 58 ms 8068 KB Output is correct
55 Correct 156 ms 22024 KB Output is correct
56 Correct 163 ms 22032 KB Output is correct
57 Correct 62 ms 12044 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 3 ms 596 KB Output is correct
61 Correct 66 ms 8084 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 0 ms 212 KB Output is correct
64 Correct 0 ms 212 KB Output is correct
65 Incorrect 16 ms 3284 KB Answer gives possible 0 while actual possible 1
66 Halted 0 ms 0 KB -
# 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 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1108 KB Output is correct
7 Correct 164 ms 22024 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 7 ms 1132 KB Output is correct
13 Correct 151 ms 22016 KB Output is correct
14 Correct 0 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 68 ms 8068 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 39 ms 5776 KB Output is correct
21 Correct 199 ms 22020 KB Output is correct
22 Correct 156 ms 21952 KB Output is correct
23 Correct 179 ms 21988 KB Output is correct
24 Correct 156 ms 21972 KB Output is correct
25 Correct 71 ms 8076 KB Output is correct
26 Correct 57 ms 8088 KB Output is correct
27 Correct 169 ms 22024 KB Output is correct
28 Correct 153 ms 22020 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 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 6 ms 1108 KB Output is correct
37 Correct 150 ms 22024 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 7 ms 1108 KB Output is correct
41 Correct 165 ms 22032 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 3 ms 600 KB Output is correct
45 Correct 71 ms 8080 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 39 ms 5692 KB Output is correct
50 Correct 161 ms 22016 KB Output is correct
51 Correct 154 ms 22040 KB Output is correct
52 Correct 161 ms 22036 KB Output is correct
53 Correct 63 ms 12036 KB Output is correct
54 Correct 58 ms 8068 KB Output is correct
55 Correct 156 ms 22024 KB Output is correct
56 Correct 163 ms 22032 KB Output is correct
57 Correct 62 ms 12044 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 43 ms 5684 KB Output is correct
62 Correct 161 ms 22012 KB Output is correct
63 Correct 154 ms 21964 KB Output is correct
64 Correct 163 ms 21944 KB Output is correct
65 Correct 0 ms 212 KB Output is correct
66 Correct 43 ms 5708 KB Output is correct
67 Correct 156 ms 22036 KB Output is correct
68 Correct 159 ms 22036 KB Output is correct
69 Correct 162 ms 21936 KB Output is correct
70 Correct 0 ms 212 KB Output is correct
71 Correct 0 ms 212 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 39 ms 5772 KB Output is correct
74 Correct 158 ms 22028 KB Output is correct
75 Incorrect 64 ms 12056 KB Answer gives possible 0 while actual possible 1
76 Halted 0 ms 0 KB -