Submission #531889

# Submission time Handle Problem Language Result Execution time Memory
531889 2022-03-01T19:37:19 Z EqualTurtle Connecting Supertrees (IOI20_supertrees) C++14
100 / 100
228 ms 22212 KB
#include <bits/stdc++.h>
#include "supertrees.h"
#define v2d vector <vector <int> >
using namespace std;

constexpr int MAXN = 1e3 + 7;

int siz[MAXN];
int repl[MAXN];
int repl2[MAXN];
int repr[MAXN];
int rep[MAXN];
int type[MAXN];

void debug(int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << i << " " << rep[i] << " " << repl[i] << " " << repl2[i] << " " << repr[i] << " " << type[i] << " " << siz[i] << "\n";
	}
	cout << "-----------------------------\n";
}

int Find(int a){
	return rep[a] == a ? a : rep[a] = Find(rep[a]);
}

void Union(int a, int b, bool how){
	if (Find(a) == Find(b))
		return;
	if (how){
		repl2[Find(b)] = min(repl2[Find(a)], min(repl2[Find(b)], max(repl[Find(b)], repl[Find(a)])));
		repl[Find(b)] = min(repl[Find(a)], repl[Find(b)]);
		repr[Find(b)] = max(repr[Find(b)], repr[Find(a)]);
		siz[Find(b)] += siz[Find(a)];
		rep[Find(a)] = Find(b);
	}
	rep[Find(a)] = Find(b);
}


int construct(v2d p) 
{
	int n = p.size();
	v2d answer;
	for (int i = 0; i < n; i++){
		vector<int> cv;
		for (int j = 0; j < n; j++)
			cv.push_back(0);
		answer.push_back(cv);
	}
	
	for (int i = 0; i < n; i++){
		rep[i] = i;
		repl[i] = i;
		repl2[i] = MAXN;
		repr[i] = i;
		siz[i] = 1;
	}

	//debug(n);

	for (int i = 0; i < n; i++){
		if (p[i][i] != 1)
			return 0;
		for (int j = i + 1; j < n; j++)
		{
			if (p[i][j] != p[j][i] || p[i][j] == 3)
				return 0;

			if (p[i][j] == 1){
				if (p[i] != p[j])
					return 0;
				if (Find(i) != Find(j)){
					Union(i, j, false);
					answer[i][j] = 1;
					answer[j][i] = 1;
				}
			}
		}
	}

	//debug(n);

	for (int i = 0; i < n; i++){
		if (i != Find(i))
			continue;
		for (int j = i + 1; j < n; j++)
		{
			if (j != Find(j))
				continue;
			if (p[i][j] == 2 || p[i][j] == 3)
			{
				if (type[i] == 0)
					type[i] = p[i][j];
				else if (type[i] != p[i][j])
					return 0;

				if (type[j] == 0)
					type[j] = p[i][j];
				else if (type[j] != p[i][j])
					return 0;
				
				for (int k = 0; k < n; k++)
				{
					if (p[i][k] == 0){
						if (p[j][k] != 0)
							return 0;
					}
					else if (p[i][k] == 1){
						if (p[j][k] != p[i][j])
							return 0;
					}
					else{
						if (p[j][k] != p[i][j] && p[j][k] != 1)
							return 0;
					}
				}

				if (Find(i) != Find(j)){
					answer[repr[Find(i)]][repr[Find(j)]] = 1;
					answer[repr[Find(j)]][repr[Find(i)]] = 1;
					Union(i, j, true);
				}
			}
		}
	}

	//debug(n);

	for (int i = 0; i < n; i++){
		if (type[Find(i)] != 0)
		{		
			if (siz[Find(i)] < 3)
				return 0;
			answer[repr[Find(i)]][repl[Find(i)]] = 1;
			answer[repl[Find(i)]][repr[Find(i)]] = 1;

			if (type[Find(i)] == 3)
			{
				if (siz[Find(i)] < 4)
					return 0;
				answer[repl2[Find(i)]][repr[Find(i)]] = 1;
				answer[repr[Find(i)]][repl2[Find(i)]] = 1;
			}

			type[Find(i)] = 0;
		}
	}
	
	//debug(n);
	build(answer);
	return 1;
}
# Verdict Execution time Memory 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 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 8 ms 1100 KB Output is correct
7 Correct 228 ms 22012 KB Output is correct
# Verdict Execution time Memory 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 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 8 ms 1100 KB Output is correct
7 Correct 228 ms 22012 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 0 ms 204 KB Output is correct
12 Correct 7 ms 1100 KB Output is correct
13 Correct 177 ms 22024 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 3 ms 716 KB Output is correct
17 Correct 75 ms 12160 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 45 ms 5760 KB Output is correct
21 Correct 179 ms 21956 KB Output is correct
22 Correct 173 ms 22012 KB Output is correct
23 Correct 206 ms 22028 KB Output is correct
24 Correct 183 ms 22084 KB Output is correct
25 Correct 70 ms 12164 KB Output is correct
26 Correct 67 ms 12164 KB Output is correct
27 Correct 211 ms 22024 KB Output is correct
28 Correct 172 ms 21956 KB Output is correct
# Verdict Execution time Memory 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 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 284 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 7 ms 1228 KB Output is correct
9 Correct 175 ms 22160 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 7 ms 1100 KB Output is correct
13 Correct 177 ms 22204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 4 ms 716 KB Output is correct
17 Correct 77 ms 12184 KB Output is correct
18 Correct 0 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 47 ms 5828 KB Output is correct
22 Correct 175 ms 22184 KB Output is correct
23 Correct 170 ms 22192 KB Output is correct
24 Correct 182 ms 22164 KB Output is correct
25 Correct 70 ms 12184 KB Output is correct
26 Correct 75 ms 12300 KB Output is correct
27 Correct 174 ms 22172 KB Output is correct
28 Correct 183 ms 22212 KB Output is correct
29 Correct 71 ms 12176 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 1 ms 204 KB Output is correct
4 Correct 44 ms 5680 KB Output is correct
5 Correct 183 ms 21988 KB Output is correct
6 Correct 177 ms 22072 KB Output is correct
7 Correct 207 ms 22012 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 43 ms 5700 KB Output is correct
10 Correct 177 ms 22024 KB Output is correct
11 Correct 181 ms 22152 KB Output is correct
12 Correct 183 ms 22024 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 46 ms 5760 KB Output is correct
17 Correct 216 ms 22016 KB Output is correct
18 Correct 169 ms 21960 KB Output is correct
19 Correct 172 ms 21956 KB Output is correct
20 Correct 172 ms 21956 KB Output is correct
# Verdict Execution time Memory 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 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 8 ms 1100 KB Output is correct
7 Correct 228 ms 22012 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 0 ms 204 KB Output is correct
12 Correct 7 ms 1100 KB Output is correct
13 Correct 177 ms 22024 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 3 ms 716 KB Output is correct
17 Correct 75 ms 12160 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 45 ms 5760 KB Output is correct
21 Correct 179 ms 21956 KB Output is correct
22 Correct 173 ms 22012 KB Output is correct
23 Correct 206 ms 22028 KB Output is correct
24 Correct 183 ms 22084 KB Output is correct
25 Correct 70 ms 12164 KB Output is correct
26 Correct 67 ms 12164 KB Output is correct
27 Correct 211 ms 22024 KB Output is correct
28 Correct 172 ms 21956 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 0 ms 204 KB Output is correct
31 Correct 0 ms 204 KB Output is correct
32 Correct 0 ms 204 KB Output is correct
33 Correct 0 ms 204 KB Output is correct
34 Correct 1 ms 284 KB Output is correct
35 Correct 0 ms 204 KB Output is correct
36 Correct 7 ms 1228 KB Output is correct
37 Correct 175 ms 22160 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 7 ms 1100 KB Output is correct
41 Correct 177 ms 22204 KB Output is correct
42 Correct 0 ms 204 KB Output is correct
43 Correct 0 ms 204 KB Output is correct
44 Correct 4 ms 716 KB Output is correct
45 Correct 77 ms 12184 KB Output is correct
46 Correct 0 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 47 ms 5828 KB Output is correct
50 Correct 175 ms 22184 KB Output is correct
51 Correct 170 ms 22192 KB Output is correct
52 Correct 182 ms 22164 KB Output is correct
53 Correct 70 ms 12184 KB Output is correct
54 Correct 75 ms 12300 KB Output is correct
55 Correct 174 ms 22172 KB Output is correct
56 Correct 183 ms 22212 KB Output is correct
57 Correct 71 ms 12176 KB Output is correct
58 Correct 0 ms 204 KB Output is correct
59 Correct 0 ms 204 KB Output is correct
60 Correct 4 ms 716 KB Output is correct
61 Correct 76 ms 12204 KB Output is correct
62 Correct 0 ms 204 KB Output is correct
63 Correct 0 ms 204 KB Output is correct
64 Correct 1 ms 204 KB Output is correct
65 Correct 54 ms 5816 KB Output is correct
66 Correct 73 ms 12176 KB Output is correct
67 Correct 73 ms 12288 KB Output is correct
68 Correct 69 ms 12288 KB Output is correct
69 Correct 70 ms 12196 KB Output is correct
# Verdict Execution time Memory 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 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 8 ms 1100 KB Output is correct
7 Correct 228 ms 22012 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 0 ms 204 KB Output is correct
12 Correct 7 ms 1100 KB Output is correct
13 Correct 177 ms 22024 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 3 ms 716 KB Output is correct
17 Correct 75 ms 12160 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 45 ms 5760 KB Output is correct
21 Correct 179 ms 21956 KB Output is correct
22 Correct 173 ms 22012 KB Output is correct
23 Correct 206 ms 22028 KB Output is correct
24 Correct 183 ms 22084 KB Output is correct
25 Correct 70 ms 12164 KB Output is correct
26 Correct 67 ms 12164 KB Output is correct
27 Correct 211 ms 22024 KB Output is correct
28 Correct 172 ms 21956 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 0 ms 204 KB Output is correct
31 Correct 0 ms 204 KB Output is correct
32 Correct 0 ms 204 KB Output is correct
33 Correct 0 ms 204 KB Output is correct
34 Correct 1 ms 284 KB Output is correct
35 Correct 0 ms 204 KB Output is correct
36 Correct 7 ms 1228 KB Output is correct
37 Correct 175 ms 22160 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 7 ms 1100 KB Output is correct
41 Correct 177 ms 22204 KB Output is correct
42 Correct 0 ms 204 KB Output is correct
43 Correct 0 ms 204 KB Output is correct
44 Correct 4 ms 716 KB Output is correct
45 Correct 77 ms 12184 KB Output is correct
46 Correct 0 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 47 ms 5828 KB Output is correct
50 Correct 175 ms 22184 KB Output is correct
51 Correct 170 ms 22192 KB Output is correct
52 Correct 182 ms 22164 KB Output is correct
53 Correct 70 ms 12184 KB Output is correct
54 Correct 75 ms 12300 KB Output is correct
55 Correct 174 ms 22172 KB Output is correct
56 Correct 183 ms 22212 KB Output is correct
57 Correct 71 ms 12176 KB Output is correct
58 Correct 0 ms 204 KB Output is correct
59 Correct 0 ms 204 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 44 ms 5680 KB Output is correct
62 Correct 183 ms 21988 KB Output is correct
63 Correct 177 ms 22072 KB Output is correct
64 Correct 207 ms 22012 KB Output is correct
65 Correct 0 ms 204 KB Output is correct
66 Correct 43 ms 5700 KB Output is correct
67 Correct 177 ms 22024 KB Output is correct
68 Correct 181 ms 22152 KB Output is correct
69 Correct 183 ms 22024 KB Output is correct
70 Correct 0 ms 204 KB Output is correct
71 Correct 0 ms 204 KB Output is correct
72 Correct 0 ms 204 KB Output is correct
73 Correct 46 ms 5760 KB Output is correct
74 Correct 216 ms 22016 KB Output is correct
75 Correct 169 ms 21960 KB Output is correct
76 Correct 172 ms 21956 KB Output is correct
77 Correct 172 ms 21956 KB Output is correct
78 Correct 0 ms 204 KB Output is correct
79 Correct 0 ms 204 KB Output is correct
80 Correct 4 ms 716 KB Output is correct
81 Correct 76 ms 12204 KB Output is correct
82 Correct 0 ms 204 KB Output is correct
83 Correct 0 ms 204 KB Output is correct
84 Correct 1 ms 204 KB Output is correct
85 Correct 54 ms 5816 KB Output is correct
86 Correct 73 ms 12176 KB Output is correct
87 Correct 73 ms 12288 KB Output is correct
88 Correct 69 ms 12288 KB Output is correct
89 Correct 70 ms 12196 KB Output is correct
90 Correct 0 ms 204 KB Output is correct
91 Correct 0 ms 204 KB Output is correct
92 Correct 4 ms 720 KB Output is correct
93 Correct 74 ms 12288 KB Output is correct
94 Correct 1 ms 204 KB Output is correct
95 Correct 0 ms 280 KB Output is correct
96 Correct 0 ms 204 KB Output is correct
97 Correct 23 ms 3344 KB Output is correct
98 Correct 71 ms 12252 KB Output is correct
99 Correct 69 ms 12284 KB Output is correct
100 Correct 80 ms 12288 KB Output is correct
101 Correct 73 ms 12176 KB Output is correct
102 Correct 83 ms 12228 KB Output is correct
103 Correct 80 ms 14124 KB Output is correct
104 Correct 76 ms 14060 KB Output is correct
105 Correct 82 ms 14064 KB Output is correct