Submission #812763

# Submission time Handle Problem Language Result Execution time Memory
812763 2023-08-07T10:55:34 Z KemalK Connecting Supertrees (IOI20_supertrees) C++17
40 / 100
175 ms 23940 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 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: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 j = 0; j < q.size(); j++){
      |                    ~~^~~~~~~~~~
# 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 8 ms 1108 KB Output is correct
7 Correct 170 ms 21964 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 8 ms 1108 KB Output is correct
7 Correct 170 ms 21964 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 7 ms 1144 KB Output is correct
13 Correct 151 ms 22020 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 588 KB Output is correct
17 Correct 70 ms 8072 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 41 ms 5668 KB Output is correct
21 Correct 157 ms 22036 KB Output is correct
22 Correct 172 ms 22036 KB Output is correct
23 Correct 175 ms 21956 KB Output is correct
24 Correct 153 ms 22040 KB Output is correct
25 Correct 59 ms 8076 KB Output is correct
26 Correct 66 ms 8068 KB Output is correct
27 Correct 164 ms 22032 KB Output is correct
28 Correct 154 ms 21964 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 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 7 ms 1108 KB Output is correct
9 Correct 156 ms 21972 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 7 ms 1108 KB Output is correct
13 Correct 168 ms 22032 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 70 ms 8072 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 40 ms 5768 KB Output is correct
22 Correct 158 ms 22036 KB Output is correct
23 Correct 159 ms 22000 KB Output is correct
24 Correct 169 ms 22028 KB Output is correct
25 Correct 62 ms 12052 KB Output is correct
26 Correct 59 ms 8140 KB Output is correct
27 Correct 157 ms 22004 KB Output is correct
28 Correct 169 ms 22000 KB Output is correct
29 Correct 62 ms 12048 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 39 ms 5740 KB Output is correct
5 Correct 157 ms 22012 KB Output is correct
6 Correct 158 ms 22032 KB Output is correct
7 Correct 166 ms 21964 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 40 ms 5772 KB Output is correct
10 Correct 158 ms 22036 KB Output is correct
11 Correct 157 ms 21964 KB Output is correct
12 Correct 163 ms 22020 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 300 KB Output is correct
16 Correct 40 ms 6272 KB Output is correct
17 Correct 157 ms 23940 KB Output is correct
18 Incorrect 65 ms 14088 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 8 ms 1108 KB Output is correct
7 Correct 170 ms 21964 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 7 ms 1144 KB Output is correct
13 Correct 151 ms 22020 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 588 KB Output is correct
17 Correct 70 ms 8072 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 41 ms 5668 KB Output is correct
21 Correct 157 ms 22036 KB Output is correct
22 Correct 172 ms 22036 KB Output is correct
23 Correct 175 ms 21956 KB Output is correct
24 Correct 153 ms 22040 KB Output is correct
25 Correct 59 ms 8076 KB Output is correct
26 Correct 66 ms 8068 KB Output is correct
27 Correct 164 ms 22032 KB Output is correct
28 Correct 154 ms 21964 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 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 7 ms 1108 KB Output is correct
37 Correct 156 ms 21972 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 7 ms 1108 KB Output is correct
41 Correct 168 ms 22032 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 3 ms 596 KB Output is correct
45 Correct 70 ms 8072 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 40 ms 5768 KB Output is correct
50 Correct 158 ms 22036 KB Output is correct
51 Correct 159 ms 22000 KB Output is correct
52 Correct 169 ms 22028 KB Output is correct
53 Correct 62 ms 12052 KB Output is correct
54 Correct 59 ms 8140 KB Output is correct
55 Correct 157 ms 22004 KB Output is correct
56 Correct 169 ms 22000 KB Output is correct
57 Correct 62 ms 12048 KB Output is correct
58 Correct 1 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 69 ms 8076 KB Output is correct
62 Correct 0 ms 212 KB Output is correct
63 Correct 0 ms 212 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Incorrect 18 ms 3676 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 8 ms 1108 KB Output is correct
7 Correct 170 ms 21964 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 7 ms 1144 KB Output is correct
13 Correct 151 ms 22020 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 588 KB Output is correct
17 Correct 70 ms 8072 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 41 ms 5668 KB Output is correct
21 Correct 157 ms 22036 KB Output is correct
22 Correct 172 ms 22036 KB Output is correct
23 Correct 175 ms 21956 KB Output is correct
24 Correct 153 ms 22040 KB Output is correct
25 Correct 59 ms 8076 KB Output is correct
26 Correct 66 ms 8068 KB Output is correct
27 Correct 164 ms 22032 KB Output is correct
28 Correct 154 ms 21964 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 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 7 ms 1108 KB Output is correct
37 Correct 156 ms 21972 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 7 ms 1108 KB Output is correct
41 Correct 168 ms 22032 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 3 ms 596 KB Output is correct
45 Correct 70 ms 8072 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 40 ms 5768 KB Output is correct
50 Correct 158 ms 22036 KB Output is correct
51 Correct 159 ms 22000 KB Output is correct
52 Correct 169 ms 22028 KB Output is correct
53 Correct 62 ms 12052 KB Output is correct
54 Correct 59 ms 8140 KB Output is correct
55 Correct 157 ms 22004 KB Output is correct
56 Correct 169 ms 22000 KB Output is correct
57 Correct 62 ms 12048 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 39 ms 5740 KB Output is correct
62 Correct 157 ms 22012 KB Output is correct
63 Correct 158 ms 22032 KB Output is correct
64 Correct 166 ms 21964 KB Output is correct
65 Correct 0 ms 212 KB Output is correct
66 Correct 40 ms 5772 KB Output is correct
67 Correct 158 ms 22036 KB Output is correct
68 Correct 157 ms 21964 KB Output is correct
69 Correct 163 ms 22020 KB Output is correct
70 Correct 0 ms 212 KB Output is correct
71 Correct 0 ms 212 KB Output is correct
72 Correct 1 ms 300 KB Output is correct
73 Correct 40 ms 6272 KB Output is correct
74 Correct 157 ms 23940 KB Output is correct
75 Incorrect 65 ms 14088 KB Answer gives possible 0 while actual possible 1
76 Halted 0 ms 0 KB -