Submission #463786

# Submission time Handle Problem Language Result Execution time Memory
463786 2021-08-11T18:18:05 Z armand Connecting Supertrees (IOI20_supertrees) C++14
100 / 100
267 ms 27920 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++) {
      |                ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory 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 0 ms 204 KB Output is correct
6 Correct 10 ms 1868 KB Output is correct
7 Correct 254 ms 25928 KB Output is correct
# Verdict Execution time Memory 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 0 ms 204 KB Output is correct
6 Correct 10 ms 1868 KB Output is correct
7 Correct 254 ms 25928 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 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 13 ms 1868 KB Output is correct
13 Correct 225 ms 25992 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 8 ms 1484 KB Output is correct
17 Correct 116 ms 15996 KB Output is correct
18 Correct 0 ms 296 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 75 ms 8176 KB Output is correct
21 Correct 243 ms 25868 KB Output is correct
22 Correct 225 ms 25956 KB Output is correct
23 Correct 262 ms 27296 KB Output is correct
24 Correct 226 ms 27848 KB Output is correct
25 Correct 159 ms 16364 KB Output is correct
26 Correct 97 ms 16548 KB Output is correct
27 Correct 267 ms 26000 KB Output is correct
28 Correct 234 ms 25856 KB Output is correct
# Verdict Execution time Memory 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 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 296 KB Output is correct
8 Correct 11 ms 1972 KB Output is correct
9 Correct 230 ms 27904 KB Output is correct
10 Correct 1 ms 296 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 10 ms 2048 KB Output is correct
13 Correct 231 ms 26964 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 5 ms 1612 KB Output is correct
17 Correct 155 ms 16256 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 61 ms 7708 KB Output is correct
22 Correct 250 ms 27716 KB Output is correct
23 Correct 238 ms 25864 KB Output is correct
24 Correct 241 ms 27896 KB Output is correct
25 Correct 99 ms 16636 KB Output is correct
26 Correct 97 ms 15996 KB Output is correct
27 Correct 226 ms 25940 KB Output is correct
28 Correct 266 ms 26060 KB Output is correct
29 Correct 107 ms 15996 KB Output is correct
# Verdict Execution time Memory 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 61 ms 7656 KB Output is correct
5 Correct 245 ms 25872 KB Output is correct
6 Correct 225 ms 26324 KB Output is correct
7 Correct 257 ms 25924 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 57 ms 7804 KB Output is correct
10 Correct 242 ms 27900 KB Output is correct
11 Correct 241 ms 27812 KB Output is correct
12 Correct 236 ms 25992 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 57 ms 7660 KB Output is correct
17 Correct 231 ms 27876 KB Output is correct
18 Correct 242 ms 27920 KB Output is correct
19 Correct 267 ms 25844 KB Output is correct
20 Correct 231 ms 27896 KB Output is correct
# Verdict Execution time Memory 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 0 ms 204 KB Output is correct
6 Correct 10 ms 1868 KB Output is correct
7 Correct 254 ms 25928 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 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 13 ms 1868 KB Output is correct
13 Correct 225 ms 25992 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 8 ms 1484 KB Output is correct
17 Correct 116 ms 15996 KB Output is correct
18 Correct 0 ms 296 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 75 ms 8176 KB Output is correct
21 Correct 243 ms 25868 KB Output is correct
22 Correct 225 ms 25956 KB Output is correct
23 Correct 262 ms 27296 KB Output is correct
24 Correct 226 ms 27848 KB Output is correct
25 Correct 159 ms 16364 KB Output is correct
26 Correct 97 ms 16548 KB Output is correct
27 Correct 267 ms 26000 KB Output is correct
28 Correct 234 ms 25856 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 1 ms 204 KB Output is correct
33 Correct 0 ms 204 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 0 ms 296 KB Output is correct
36 Correct 11 ms 1972 KB Output is correct
37 Correct 230 ms 27904 KB Output is correct
38 Correct 1 ms 296 KB Output is correct
39 Correct 0 ms 332 KB Output is correct
40 Correct 10 ms 2048 KB Output is correct
41 Correct 231 ms 26964 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 0 ms 332 KB Output is correct
44 Correct 5 ms 1612 KB Output is correct
45 Correct 155 ms 16256 KB Output is correct
46 Correct 0 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 0 ms 204 KB Output is correct
49 Correct 61 ms 7708 KB Output is correct
50 Correct 250 ms 27716 KB Output is correct
51 Correct 238 ms 25864 KB Output is correct
52 Correct 241 ms 27896 KB Output is correct
53 Correct 99 ms 16636 KB Output is correct
54 Correct 97 ms 15996 KB Output is correct
55 Correct 226 ms 25940 KB Output is correct
56 Correct 266 ms 26060 KB Output is correct
57 Correct 107 ms 15996 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 0 ms 332 KB Output is correct
60 Correct 5 ms 1612 KB Output is correct
61 Correct 114 ms 18040 KB Output is correct
62 Correct 0 ms 204 KB Output is correct
63 Correct 0 ms 332 KB Output is correct
64 Correct 1 ms 332 KB Output is correct
65 Correct 57 ms 7620 KB Output is correct
66 Correct 104 ms 16692 KB Output is correct
67 Correct 101 ms 17904 KB Output is correct
68 Correct 98 ms 17908 KB Output is correct
69 Correct 102 ms 16056 KB Output is correct
# Verdict Execution time Memory 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 0 ms 204 KB Output is correct
6 Correct 10 ms 1868 KB Output is correct
7 Correct 254 ms 25928 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 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 13 ms 1868 KB Output is correct
13 Correct 225 ms 25992 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 8 ms 1484 KB Output is correct
17 Correct 116 ms 15996 KB Output is correct
18 Correct 0 ms 296 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 75 ms 8176 KB Output is correct
21 Correct 243 ms 25868 KB Output is correct
22 Correct 225 ms 25956 KB Output is correct
23 Correct 262 ms 27296 KB Output is correct
24 Correct 226 ms 27848 KB Output is correct
25 Correct 159 ms 16364 KB Output is correct
26 Correct 97 ms 16548 KB Output is correct
27 Correct 267 ms 26000 KB Output is correct
28 Correct 234 ms 25856 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 1 ms 204 KB Output is correct
33 Correct 0 ms 204 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 0 ms 296 KB Output is correct
36 Correct 11 ms 1972 KB Output is correct
37 Correct 230 ms 27904 KB Output is correct
38 Correct 1 ms 296 KB Output is correct
39 Correct 0 ms 332 KB Output is correct
40 Correct 10 ms 2048 KB Output is correct
41 Correct 231 ms 26964 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 0 ms 332 KB Output is correct
44 Correct 5 ms 1612 KB Output is correct
45 Correct 155 ms 16256 KB Output is correct
46 Correct 0 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 0 ms 204 KB Output is correct
49 Correct 61 ms 7708 KB Output is correct
50 Correct 250 ms 27716 KB Output is correct
51 Correct 238 ms 25864 KB Output is correct
52 Correct 241 ms 27896 KB Output is correct
53 Correct 99 ms 16636 KB Output is correct
54 Correct 97 ms 15996 KB Output is correct
55 Correct 226 ms 25940 KB Output is correct
56 Correct 266 ms 26060 KB Output is correct
57 Correct 107 ms 15996 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 61 ms 7656 KB Output is correct
62 Correct 245 ms 25872 KB Output is correct
63 Correct 225 ms 26324 KB Output is correct
64 Correct 257 ms 25924 KB Output is correct
65 Correct 0 ms 204 KB Output is correct
66 Correct 57 ms 7804 KB Output is correct
67 Correct 242 ms 27900 KB Output is correct
68 Correct 241 ms 27812 KB Output is correct
69 Correct 236 ms 25992 KB Output is correct
70 Correct 0 ms 204 KB Output is correct
71 Correct 0 ms 332 KB Output is correct
72 Correct 1 ms 332 KB Output is correct
73 Correct 57 ms 7660 KB Output is correct
74 Correct 231 ms 27876 KB Output is correct
75 Correct 242 ms 27920 KB Output is correct
76 Correct 267 ms 25844 KB Output is correct
77 Correct 231 ms 27896 KB Output is correct
78 Correct 1 ms 204 KB Output is correct
79 Correct 0 ms 332 KB Output is correct
80 Correct 5 ms 1612 KB Output is correct
81 Correct 114 ms 18040 KB Output is correct
82 Correct 0 ms 204 KB Output is correct
83 Correct 0 ms 332 KB Output is correct
84 Correct 1 ms 332 KB Output is correct
85 Correct 57 ms 7620 KB Output is correct
86 Correct 104 ms 16692 KB Output is correct
87 Correct 101 ms 17904 KB Output is correct
88 Correct 98 ms 17908 KB Output is correct
89 Correct 102 ms 16056 KB Output is correct
90 Correct 1 ms 204 KB Output is correct
91 Correct 0 ms 204 KB Output is correct
92 Correct 5 ms 588 KB Output is correct
93 Correct 92 ms 8064 KB Output is correct
94 Correct 1 ms 256 KB Output is correct
95 Correct 0 ms 204 KB Output is correct
96 Correct 1 ms 204 KB Output is correct
97 Correct 36 ms 2292 KB Output is correct
98 Correct 112 ms 8352 KB Output is correct
99 Correct 110 ms 8704 KB Output is correct
100 Correct 89 ms 10356 KB Output is correct
101 Correct 92 ms 9468 KB Output is correct
102 Correct 152 ms 10096 KB Output is correct
103 Correct 94 ms 10104 KB Output is correct
104 Correct 89 ms 10104 KB Output is correct
105 Correct 127 ms 8072 KB Output is correct