답안 #812805

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
812805 2023-08-07T11:07:40 Z KemalK 슈퍼트리 잇기 (IOI20_supertrees) C++17
40 / 100
193 ms 22052 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 j = 0; j < branch.size(); j++){
			forest[get2(j, pr2)].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]);
			}
			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 j = 0; j < branch.size(); j++){
      |                   ~~^~~~~~~~~~~~~~~
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++){
      |                    ~~^~~~~~~~~~
supertrees.cpp:47:6: warning: unused variable 'check' [-Wunused-variable]
   47 |  int check = 0;
      |      ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 185 ms 22012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 185 ms 22012 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 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 1124 KB Output is correct
13 Correct 162 ms 22024 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 5 ms 596 KB Output is correct
17 Correct 73 ms 8080 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 47 ms 5660 KB Output is correct
21 Correct 169 ms 21948 KB Output is correct
22 Correct 156 ms 21988 KB Output is correct
23 Correct 180 ms 21952 KB Output is correct
24 Correct 166 ms 22036 KB Output is correct
25 Correct 98 ms 8072 KB Output is correct
26 Correct 78 ms 8064 KB Output is correct
27 Correct 179 ms 22036 KB Output is correct
28 Correct 181 ms 21988 KB Output is correct
# 결과 실행 시간 메모리 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 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 10 ms 1176 KB Output is correct
9 Correct 185 ms 21996 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 1108 KB Output is correct
13 Correct 178 ms 22036 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 77 ms 8188 KB Output is correct
18 Correct 1 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 43 ms 5772 KB Output is correct
22 Correct 193 ms 22052 KB Output is correct
23 Correct 193 ms 22024 KB Output is correct
24 Correct 178 ms 22036 KB Output is correct
25 Correct 93 ms 12036 KB Output is correct
26 Correct 81 ms 8072 KB Output is correct
27 Correct 164 ms 21992 KB Output is correct
28 Correct 179 ms 21944 KB Output is correct
29 Correct 64 ms 12024 KB Output is correct
# 결과 실행 시간 메모리 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 39 ms 5696 KB Output is correct
5 Correct 156 ms 22032 KB Output is correct
6 Correct 159 ms 22032 KB Output is correct
7 Correct 165 ms 22036 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 40 ms 5696 KB Output is correct
10 Correct 157 ms 21964 KB Output is correct
11 Correct 154 ms 22032 KB Output is correct
12 Correct 166 ms 22028 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 5748 KB Output is correct
17 Correct 158 ms 22008 KB Output is correct
18 Incorrect 64 ms 12036 KB Answer gives possible 0 while actual possible 1
19 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 185 ms 22012 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 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 1124 KB Output is correct
13 Correct 162 ms 22024 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 5 ms 596 KB Output is correct
17 Correct 73 ms 8080 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 47 ms 5660 KB Output is correct
21 Correct 169 ms 21948 KB Output is correct
22 Correct 156 ms 21988 KB Output is correct
23 Correct 180 ms 21952 KB Output is correct
24 Correct 166 ms 22036 KB Output is correct
25 Correct 98 ms 8072 KB Output is correct
26 Correct 78 ms 8064 KB Output is correct
27 Correct 179 ms 22036 KB Output is correct
28 Correct 181 ms 21988 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 10 ms 1176 KB Output is correct
37 Correct 185 ms 21996 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 1108 KB Output is correct
41 Correct 178 ms 22036 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 77 ms 8188 KB Output is correct
46 Correct 1 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 43 ms 5772 KB Output is correct
50 Correct 193 ms 22052 KB Output is correct
51 Correct 193 ms 22024 KB Output is correct
52 Correct 178 ms 22036 KB Output is correct
53 Correct 93 ms 12036 KB Output is correct
54 Correct 81 ms 8072 KB Output is correct
55 Correct 164 ms 21992 KB Output is correct
56 Correct 179 ms 21944 KB Output is correct
57 Correct 64 ms 12024 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 67 ms 8080 KB Output is correct
62 Correct 0 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 3196 KB Answer gives possible 0 while actual possible 1
66 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 185 ms 22012 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 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 1124 KB Output is correct
13 Correct 162 ms 22024 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 5 ms 596 KB Output is correct
17 Correct 73 ms 8080 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 47 ms 5660 KB Output is correct
21 Correct 169 ms 21948 KB Output is correct
22 Correct 156 ms 21988 KB Output is correct
23 Correct 180 ms 21952 KB Output is correct
24 Correct 166 ms 22036 KB Output is correct
25 Correct 98 ms 8072 KB Output is correct
26 Correct 78 ms 8064 KB Output is correct
27 Correct 179 ms 22036 KB Output is correct
28 Correct 181 ms 21988 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 10 ms 1176 KB Output is correct
37 Correct 185 ms 21996 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 1108 KB Output is correct
41 Correct 178 ms 22036 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 77 ms 8188 KB Output is correct
46 Correct 1 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 43 ms 5772 KB Output is correct
50 Correct 193 ms 22052 KB Output is correct
51 Correct 193 ms 22024 KB Output is correct
52 Correct 178 ms 22036 KB Output is correct
53 Correct 93 ms 12036 KB Output is correct
54 Correct 81 ms 8072 KB Output is correct
55 Correct 164 ms 21992 KB Output is correct
56 Correct 179 ms 21944 KB Output is correct
57 Correct 64 ms 12024 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 39 ms 5696 KB Output is correct
62 Correct 156 ms 22032 KB Output is correct
63 Correct 159 ms 22032 KB Output is correct
64 Correct 165 ms 22036 KB Output is correct
65 Correct 1 ms 212 KB Output is correct
66 Correct 40 ms 5696 KB Output is correct
67 Correct 157 ms 21964 KB Output is correct
68 Correct 154 ms 22032 KB Output is correct
69 Correct 166 ms 22028 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 5748 KB Output is correct
74 Correct 158 ms 22008 KB Output is correct
75 Incorrect 64 ms 12036 KB Answer gives possible 0 while actual possible 1
76 Halted 0 ms 0 KB -