답안 #463787

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
463787 2021-08-11T18:18:05 Z armand 슈퍼트리 잇기 (IOI20_supertrees) C++14
100 / 100
259 ms 27992 KB
#include "supertrees.h"
#include <vector>
#include <queue>

int pp[1001][1001];
int n;
std::vector<std::vector<int>> answer;

bool all1()
{
	int i, j;
	for (i = 0; i < n; i++)
		for (j = 0; j < n; j++)
			if (pp[i][j] != 1)
				return false;
	return true;
}

bool all01()
{
	int i, j;
	for (i = 0; i < n; i++)
		for (j = 0; j < n; j++)
			if (pp[i][j] > 1)
				return false;
	return true;
}

bool all02()
{
	int i, j;
	for (i = 0; i < n; i++)
		for (j = 0; j < n; j++) {
			if (i == j)
				continue;
			if (pp[i][j] != 0 && pp[i][j] != 2)
				return false;
		}
	return true;
}

void solve1()
{
	int i;
	for (i = 0; i < n - 1; i++) {
		answer[i][i + 1] = 1;
		answer[i + 1][i] = 1;
	}
}

int color[1001];
int nodes[1001];
int nn;
bool flag2, flag3;
int nc;
void dfs(int u, int prnt)
{
	int j;
	for(j=0; j<n; j++)
		if (pp[u][j] && !color[j]) {
			nodes[nn++] = j;
			color[j] = nc;
			dfs(j, u);
		}
}

void solve2()
{
	int i, j;
	for (i = 0; i < n; i++) {
		if (!color[i]) {
			nn = 0;
			nc++;
			color[i] = nc;
			nodes[nn++] = i;
			dfs(i, -1);
			for (j = 1; j < nn; j++) {
				answer[nodes[j]][nodes[j - 1]] = 1;
				answer[nodes[j - 1]][nodes[j]] = 1;
			}
		}
	}
	for (i = 0; i < n; i++)
		for (j = 0; j < n; j++) {
			if (i == j)
				continue;
			if (color[i] == color[j] && !pp[i][j])
				flag2 = false;
		}
}

void solve3()
{
	int i, j;
	for (i = 0; i < n; i++) {
		if (!color[i]) {
			nn = 0;
			nc++;
			color[i] = nc;
			nodes[nn++] = i;
			dfs(i, -1);
			if (nn > 2) {
				for (j = 1; j < nn; j++) {
					answer[nodes[j]][nodes[j - 1]] = 1;
					answer[nodes[j - 1]][nodes[j]] = 1;
				}
				answer[nodes[0]][nodes[nn - 1]] = 1;
				answer[nodes[nn - 1]][nodes[0]] = 1;
			}
			else if (nn == 2)
				flag3 = false;
		}
	}
	for (i = 0; i < n; i++)
		for (j = 0; j < n; j++) {
			if (i == j)
				continue;
			if (color[i] == color[j] && !pp[i][j])
				flag3 = false;
		}
}

bool flag4;

void solve4()
{
	int i, j, k;
	for (i = 0; i < n; i++) {
		if (!color[i]) {
			nn = 0;
			nc++;
			color[i] = nc;
			nodes[nn++] = i;
			dfs(i, -1);
			if (nn == 1)
				continue;
			std::vector<int> mark;
			std::vector<int> cicle;
			mark.resize(nn);
			for (j = 0; j < nn; j++)
				mark[j] = 0;
			std::queue<int> q;
			for(j=0; j<nn; j++)
				if (mark[j] == 0) {
					q.push(nodes[j]);
					mark[j] = true;
					std::vector<int> line;
					while (!q.empty())
					{
						int x = q.front();
						q.pop();
						for(k=0; k<nn; k++)
							if (pp[x][nodes[k]] == 1 && !mark[k]) {
								line.push_back(nodes[k]);
								mark[k] = true;
								q.push(nodes[k]);
							}
					}
					if (line.size() > 0) {
						line.push_back(nodes[j]);
						cicle.push_back(nodes[j]);
						for (k = 1; k < line.size(); ++k) {
							answer[line[k]][line[k - 1]] = 1;
							answer[line[k - 1]][line[k]] = 1;
						}
					}
					else
						cicle.push_back(nodes[j]);
				}	
			for (j = 1; j < cicle.size(); j++) {
				answer[cicle[j]][cicle[j - 1]] = 1;
				answer[cicle[j - 1]][cicle[j]] = 1;
			}
			if (cicle.size() > 2) {
				answer[cicle[0]][cicle[cicle.size() - 1]] = 1;
				answer[cicle[cicle.size() - 1]][cicle[0]] = 1;
			}
			else if (cicle.size() == 2)
				flag4 = false;
		}
	}
	for (i = 0; i < n; i++)
		for (j = 0; j < n; j++) {
			if (i == j)
				continue;
			if (color[i] == color[j] && !pp[i][j])
				flag4 = false;
		}
}

int construct(std::vector<std::vector<int>> p)
{
	n = p.size();
	int i, j;
	for (i = 0; i < n; i++)
		for (j = 0; j < n; j++) {
			pp[i][j] = p[i][j];
			if (p[i][j] == 3)
				return 0;
		}
	for (int i = 0; i < n; i++) {
		std::vector<int> row;
		row.resize(n);
		answer.push_back(row);
	}
	if (all1())
		solve1();
	else if (all01()) {
		flag2 = true;
		solve2();
		if (flag2) {
			build(answer);
			return 1;
		}
		return 0;
	}
	else if (all02()) {
		flag3 = true;
		solve3();
		if (flag3) {
			build(answer);
			return 1;
		}
		return 0;
	}
	else {
		flag4 = true;
		solve4();
		if (flag4) {
			build(answer);
			return 1;
		}
		return 0;
	}
	build(answer);
	return 1;
}

Compilation message

supertrees.cpp: In function 'void solve4()':
supertrees.cpp:162:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  162 |       for (k = 1; k < line.size(); ++k) {
      |                   ~~^~~~~~~~~~~~~
supertrees.cpp:170:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  170 |    for (j = 1; j < cicle.size(); j++) {
      |                ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 10 ms 1944 KB Output is correct
7 Correct 254 ms 27900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 10 ms 1944 KB Output is correct
7 Correct 254 ms 27900 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 292 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 11 ms 1948 KB Output is correct
13 Correct 249 ms 27884 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 5 ms 1584 KB Output is correct
17 Correct 120 ms 18040 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 73 ms 7680 KB Output is correct
21 Correct 239 ms 27884 KB Output is correct
22 Correct 231 ms 27932 KB Output is correct
23 Correct 244 ms 26552 KB Output is correct
24 Correct 239 ms 25964 KB Output is correct
25 Correct 164 ms 17604 KB Output is correct
26 Correct 97 ms 17416 KB Output is correct
27 Correct 239 ms 27864 KB Output is correct
28 Correct 242 ms 27928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 10 ms 1860 KB Output is correct
9 Correct 242 ms 26012 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 10 ms 1932 KB Output is correct
13 Correct 224 ms 26888 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 5 ms 1592 KB Output is correct
17 Correct 157 ms 17776 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 61 ms 8108 KB Output is correct
22 Correct 237 ms 26008 KB Output is correct
23 Correct 225 ms 27872 KB Output is correct
24 Correct 237 ms 25896 KB Output is correct
25 Correct 104 ms 17348 KB Output is correct
26 Correct 100 ms 17900 KB Output is correct
27 Correct 231 ms 27908 KB Output is correct
28 Correct 253 ms 27808 KB Output is correct
29 Correct 96 ms 17968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 64 ms 8184 KB Output is correct
5 Correct 226 ms 27916 KB Output is correct
6 Correct 227 ms 27496 KB Output is correct
7 Correct 241 ms 27992 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 75 ms 8036 KB Output is correct
10 Correct 243 ms 25852 KB Output is correct
11 Correct 227 ms 26000 KB Output is correct
12 Correct 235 ms 27900 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 0 ms 300 KB Output is correct
16 Correct 58 ms 8180 KB Output is correct
17 Correct 239 ms 25948 KB Output is correct
18 Correct 248 ms 25988 KB Output is correct
19 Correct 259 ms 27876 KB Output is correct
20 Correct 229 ms 25956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 10 ms 1944 KB Output is correct
7 Correct 254 ms 27900 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 292 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 11 ms 1948 KB Output is correct
13 Correct 249 ms 27884 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 5 ms 1584 KB Output is correct
17 Correct 120 ms 18040 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 73 ms 7680 KB Output is correct
21 Correct 239 ms 27884 KB Output is correct
22 Correct 231 ms 27932 KB Output is correct
23 Correct 244 ms 26552 KB Output is correct
24 Correct 239 ms 25964 KB Output is correct
25 Correct 164 ms 17604 KB Output is correct
26 Correct 97 ms 17416 KB Output is correct
27 Correct 239 ms 27864 KB Output is correct
28 Correct 242 ms 27928 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 0 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 10 ms 1860 KB Output is correct
37 Correct 242 ms 26012 KB Output is correct
38 Correct 0 ms 204 KB Output is correct
39 Correct 0 ms 332 KB Output is correct
40 Correct 10 ms 1932 KB Output is correct
41 Correct 224 ms 26888 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 5 ms 1592 KB Output is correct
45 Correct 157 ms 17776 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 0 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 61 ms 8108 KB Output is correct
50 Correct 237 ms 26008 KB Output is correct
51 Correct 225 ms 27872 KB Output is correct
52 Correct 237 ms 25896 KB Output is correct
53 Correct 104 ms 17348 KB Output is correct
54 Correct 100 ms 17900 KB Output is correct
55 Correct 231 ms 27908 KB Output is correct
56 Correct 253 ms 27808 KB Output is correct
57 Correct 96 ms 17968 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 1 ms 296 KB Output is correct
60 Correct 5 ms 1528 KB Output is correct
61 Correct 122 ms 16000 KB Output is correct
62 Correct 1 ms 204 KB Output is correct
63 Correct 1 ms 332 KB Output is correct
64 Correct 1 ms 332 KB Output is correct
65 Correct 61 ms 8176 KB Output is correct
66 Correct 128 ms 17272 KB Output is correct
67 Correct 98 ms 15996 KB Output is correct
68 Correct 95 ms 16008 KB Output is correct
69 Correct 101 ms 17900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 10 ms 1944 KB Output is correct
7 Correct 254 ms 27900 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 292 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 11 ms 1948 KB Output is correct
13 Correct 249 ms 27884 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 5 ms 1584 KB Output is correct
17 Correct 120 ms 18040 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 73 ms 7680 KB Output is correct
21 Correct 239 ms 27884 KB Output is correct
22 Correct 231 ms 27932 KB Output is correct
23 Correct 244 ms 26552 KB Output is correct
24 Correct 239 ms 25964 KB Output is correct
25 Correct 164 ms 17604 KB Output is correct
26 Correct 97 ms 17416 KB Output is correct
27 Correct 239 ms 27864 KB Output is correct
28 Correct 242 ms 27928 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 0 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 10 ms 1860 KB Output is correct
37 Correct 242 ms 26012 KB Output is correct
38 Correct 0 ms 204 KB Output is correct
39 Correct 0 ms 332 KB Output is correct
40 Correct 10 ms 1932 KB Output is correct
41 Correct 224 ms 26888 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 5 ms 1592 KB Output is correct
45 Correct 157 ms 17776 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 0 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 61 ms 8108 KB Output is correct
50 Correct 237 ms 26008 KB Output is correct
51 Correct 225 ms 27872 KB Output is correct
52 Correct 237 ms 25896 KB Output is correct
53 Correct 104 ms 17348 KB Output is correct
54 Correct 100 ms 17900 KB Output is correct
55 Correct 231 ms 27908 KB Output is correct
56 Correct 253 ms 27808 KB Output is correct
57 Correct 96 ms 17968 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 0 ms 204 KB Output is correct
60 Correct 0 ms 204 KB Output is correct
61 Correct 64 ms 8184 KB Output is correct
62 Correct 226 ms 27916 KB Output is correct
63 Correct 227 ms 27496 KB Output is correct
64 Correct 241 ms 27992 KB Output is correct
65 Correct 1 ms 204 KB Output is correct
66 Correct 75 ms 8036 KB Output is correct
67 Correct 243 ms 25852 KB Output is correct
68 Correct 227 ms 26000 KB Output is correct
69 Correct 235 ms 27900 KB Output is correct
70 Correct 1 ms 204 KB Output is correct
71 Correct 1 ms 332 KB Output is correct
72 Correct 0 ms 300 KB Output is correct
73 Correct 58 ms 8180 KB Output is correct
74 Correct 239 ms 25948 KB Output is correct
75 Correct 248 ms 25988 KB Output is correct
76 Correct 259 ms 27876 KB Output is correct
77 Correct 229 ms 25956 KB Output is correct
78 Correct 1 ms 204 KB Output is correct
79 Correct 1 ms 296 KB Output is correct
80 Correct 5 ms 1528 KB Output is correct
81 Correct 122 ms 16000 KB Output is correct
82 Correct 1 ms 204 KB Output is correct
83 Correct 1 ms 332 KB Output is correct
84 Correct 1 ms 332 KB Output is correct
85 Correct 61 ms 8176 KB Output is correct
86 Correct 128 ms 17272 KB Output is correct
87 Correct 98 ms 15996 KB Output is correct
88 Correct 95 ms 16008 KB Output is correct
89 Correct 101 ms 17900 KB Output is correct
90 Correct 0 ms 204 KB Output is correct
91 Correct 0 ms 204 KB Output is correct
92 Correct 6 ms 676 KB Output is correct
93 Correct 93 ms 10100 KB Output is correct
94 Correct 0 ms 204 KB Output is correct
95 Correct 1 ms 204 KB Output is correct
96 Correct 1 ms 204 KB Output is correct
97 Correct 38 ms 2804 KB Output is correct
98 Correct 97 ms 10236 KB Output is correct
99 Correct 91 ms 10620 KB Output is correct
100 Correct 101 ms 8460 KB Output is correct
101 Correct 90 ms 11460 KB Output is correct
102 Correct 148 ms 8076 KB Output is correct
103 Correct 94 ms 8060 KB Output is correct
104 Correct 106 ms 8060 KB Output is correct
105 Correct 128 ms 10096 KB Output is correct