Submission #822988

# Submission time Handle Problem Language Result Execution time Memory
822988 2023-08-12T06:33:19 Z ymm Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
244 ms 24180 KB
#include "supertrees.h"
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (r); ++x)
typedef long long ll;
using namespace std;

vector<vector<int>> ans;
int n;

struct dsu {
	vector<int> par;
	vector<vector<int>> nds;
	void init(int n) { 
		par.assign(n, -1);
		nds.assign(n, vector<int>());
		Loop (i,0,n)
			nds[i].push_back(i);
	}
	int rt(int v) { return par[v] == -1? v: (par[v] = rt(par[v])); }
	bool same(int v, int u) { return rt(v) == rt(u); }
	bool unite(int v, int u) {
		v = rt(v);
		u = rt(u);
		if (v == u)
			return 0;
		if (nds[v].size() < nds[u].size())
			swap(v, u);
		par[u] = v;
		nds[v].insert(nds[v].end(), nds[u].begin(), nds[u].end());
		vector<int> tmp;
		tmp.swap(nds[u]);
		return 1;
	}
};

int construct(std::vector<std::vector<int>> p) {
	n = p.size();
	ans.assign(n, vector<int>(n, 0));
	Loop (i,0,n) Loop (j,i+1,n) {
		if (p[i][j] == 3)
			return 0;
	}
	dsu dsu1;
	dsu1.init(n);
	Loop (i,0,n) Loop (j,i+1,n) {
		if (p[i][j] == 1 && dsu1.unite(i, j))
			ans[i][j] = ans[j][i] = 1;
	}
	Loop (i,0,n) Loop (j,i+1,n) {
		if (p[i][j] != 1 && dsu1.same(i, j))
			return 0;
	}
	dsu dsu2;
	dsu2.init(n);
	Loop (i,0,n) Loop (j,i+1,n) {
		if (p[i][j] != 2)
			continue;
		if (dsu1.same(i, j))
			return 0;
		int x = dsu1.rt(i);
		int y = dsu1.rt(j);
		dsu2.unite(x, y);
	}
	Loop (i,0,n) Loop (j,i+1,n) {
		int x = dsu1.rt(i);
		int y = dsu1.rt(j);
		if (p[i][j] == 0 && dsu2.same(x, y))
			return 0;
	}
	Loop (i,0,n) {
		if (dsu2.par[i] != -1)
			continue;
		int sz = dsu2.nds[i].size();
		if (sz <= 1)
			continue;
		if (sz == 2)
			return 0;
		Loop (j,0,sz) {
			int x = dsu2.nds[i][j];
			int y = dsu2.nds[i][(j+1)%sz];
			ans[x][y] = ans[y][x] = 1;
		}
	}
	build(ans);
	return 1;
}
# 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 7 ms 1188 KB Output is correct
7 Correct 164 ms 22144 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 7 ms 1188 KB Output is correct
7 Correct 164 ms 22144 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 0 ms 212 KB Output is correct
12 Correct 7 ms 1108 KB Output is correct
13 Correct 156 ms 22036 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 752 KB Output is correct
17 Correct 69 ms 12160 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 43 ms 5740 KB Output is correct
21 Correct 155 ms 22100 KB Output is correct
22 Correct 172 ms 22068 KB Output is correct
23 Correct 163 ms 22044 KB Output is correct
24 Correct 156 ms 22044 KB Output is correct
25 Correct 63 ms 12176 KB Output is correct
26 Correct 67 ms 12168 KB Output is correct
27 Correct 173 ms 22056 KB Output is correct
28 Correct 155 ms 22092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 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 300 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 7 ms 1236 KB Output is correct
9 Correct 154 ms 24180 KB Output is correct
10 Correct 1 ms 300 KB Output is correct
11 Correct 0 ms 292 KB Output is correct
12 Correct 7 ms 1176 KB Output is correct
13 Correct 158 ms 24080 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 796 KB Output is correct
17 Correct 73 ms 14112 KB Output is correct
18 Correct 0 ms 300 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 300 KB Output is correct
21 Correct 44 ms 6240 KB Output is correct
22 Correct 191 ms 24096 KB Output is correct
23 Correct 177 ms 24052 KB Output is correct
24 Correct 166 ms 23976 KB Output is correct
25 Correct 80 ms 14196 KB Output is correct
26 Correct 74 ms 14148 KB Output is correct
27 Correct 163 ms 24108 KB Output is correct
28 Correct 164 ms 24052 KB Output is correct
29 Correct 65 ms 14080 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 41 ms 5808 KB Output is correct
5 Correct 158 ms 22020 KB Output is correct
6 Correct 155 ms 22136 KB Output is correct
7 Correct 169 ms 22112 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 42 ms 5752 KB Output is correct
10 Correct 157 ms 22032 KB Output is correct
11 Correct 162 ms 22164 KB Output is correct
12 Correct 161 ms 22036 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 212 KB Output is correct
16 Correct 44 ms 5708 KB Output is correct
17 Correct 161 ms 22060 KB Output is correct
18 Correct 181 ms 22148 KB Output is correct
19 Correct 244 ms 22044 KB Output is correct
20 Correct 154 ms 22092 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 7 ms 1188 KB Output is correct
7 Correct 164 ms 22144 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 0 ms 212 KB Output is correct
12 Correct 7 ms 1108 KB Output is correct
13 Correct 156 ms 22036 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 752 KB Output is correct
17 Correct 69 ms 12160 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 43 ms 5740 KB Output is correct
21 Correct 155 ms 22100 KB Output is correct
22 Correct 172 ms 22068 KB Output is correct
23 Correct 163 ms 22044 KB Output is correct
24 Correct 156 ms 22044 KB Output is correct
25 Correct 63 ms 12176 KB Output is correct
26 Correct 67 ms 12168 KB Output is correct
27 Correct 173 ms 22056 KB Output is correct
28 Correct 155 ms 22092 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 1 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 300 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 7 ms 1236 KB Output is correct
37 Correct 154 ms 24180 KB Output is correct
38 Correct 1 ms 300 KB Output is correct
39 Correct 0 ms 292 KB Output is correct
40 Correct 7 ms 1176 KB Output is correct
41 Correct 158 ms 24080 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 796 KB Output is correct
45 Correct 73 ms 14112 KB Output is correct
46 Correct 0 ms 300 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 300 KB Output is correct
49 Correct 44 ms 6240 KB Output is correct
50 Correct 191 ms 24096 KB Output is correct
51 Correct 177 ms 24052 KB Output is correct
52 Correct 166 ms 23976 KB Output is correct
53 Correct 80 ms 14196 KB Output is correct
54 Correct 74 ms 14148 KB Output is correct
55 Correct 163 ms 24108 KB Output is correct
56 Correct 164 ms 24052 KB Output is correct
57 Correct 65 ms 14080 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 3 ms 828 KB Output is correct
61 Correct 68 ms 14112 KB Output is correct
62 Correct 1 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 Correct 44 ms 6264 KB Output is correct
66 Correct 63 ms 14156 KB Output is correct
67 Correct 66 ms 14064 KB Output is correct
68 Correct 65 ms 14192 KB Output is correct
69 Correct 64 ms 14200 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 7 ms 1188 KB Output is correct
7 Correct 164 ms 22144 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 0 ms 212 KB Output is correct
12 Correct 7 ms 1108 KB Output is correct
13 Correct 156 ms 22036 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 752 KB Output is correct
17 Correct 69 ms 12160 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 43 ms 5740 KB Output is correct
21 Correct 155 ms 22100 KB Output is correct
22 Correct 172 ms 22068 KB Output is correct
23 Correct 163 ms 22044 KB Output is correct
24 Correct 156 ms 22044 KB Output is correct
25 Correct 63 ms 12176 KB Output is correct
26 Correct 67 ms 12168 KB Output is correct
27 Correct 173 ms 22056 KB Output is correct
28 Correct 155 ms 22092 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 1 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 300 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 7 ms 1236 KB Output is correct
37 Correct 154 ms 24180 KB Output is correct
38 Correct 1 ms 300 KB Output is correct
39 Correct 0 ms 292 KB Output is correct
40 Correct 7 ms 1176 KB Output is correct
41 Correct 158 ms 24080 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 796 KB Output is correct
45 Correct 73 ms 14112 KB Output is correct
46 Correct 0 ms 300 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 300 KB Output is correct
49 Correct 44 ms 6240 KB Output is correct
50 Correct 191 ms 24096 KB Output is correct
51 Correct 177 ms 24052 KB Output is correct
52 Correct 166 ms 23976 KB Output is correct
53 Correct 80 ms 14196 KB Output is correct
54 Correct 74 ms 14148 KB Output is correct
55 Correct 163 ms 24108 KB Output is correct
56 Correct 164 ms 24052 KB Output is correct
57 Correct 65 ms 14080 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 41 ms 5808 KB Output is correct
62 Correct 158 ms 22020 KB Output is correct
63 Correct 155 ms 22136 KB Output is correct
64 Correct 169 ms 22112 KB Output is correct
65 Correct 0 ms 212 KB Output is correct
66 Correct 42 ms 5752 KB Output is correct
67 Correct 157 ms 22032 KB Output is correct
68 Correct 162 ms 22164 KB Output is correct
69 Correct 161 ms 22036 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 212 KB Output is correct
73 Correct 44 ms 5708 KB Output is correct
74 Correct 161 ms 22060 KB Output is correct
75 Correct 181 ms 22148 KB Output is correct
76 Correct 244 ms 22044 KB Output is correct
77 Correct 154 ms 22092 KB Output is correct
78 Correct 0 ms 212 KB Output is correct
79 Correct 1 ms 212 KB Output is correct
80 Correct 3 ms 828 KB Output is correct
81 Correct 68 ms 14112 KB Output is correct
82 Correct 1 ms 212 KB Output is correct
83 Correct 0 ms 212 KB Output is correct
84 Correct 0 ms 212 KB Output is correct
85 Correct 44 ms 6264 KB Output is correct
86 Correct 63 ms 14156 KB Output is correct
87 Correct 66 ms 14064 KB Output is correct
88 Correct 65 ms 14192 KB Output is correct
89 Correct 64 ms 14200 KB Output is correct
90 Correct 0 ms 212 KB Output is correct
91 Correct 1 ms 212 KB Output is correct
92 Correct 3 ms 852 KB Output is correct
93 Correct 64 ms 14124 KB Output is correct
94 Correct 0 ms 212 KB Output is correct
95 Correct 1 ms 212 KB Output is correct
96 Correct 1 ms 212 KB Output is correct
97 Correct 15 ms 3704 KB Output is correct
98 Correct 63 ms 13992 KB Output is correct
99 Correct 69 ms 14072 KB Output is correct
100 Correct 61 ms 14068 KB Output is correct
101 Correct 63 ms 14072 KB Output is correct
102 Correct 60 ms 14068 KB Output is correct
103 Correct 62 ms 14028 KB Output is correct
104 Correct 65 ms 14080 KB Output is correct
105 Correct 71 ms 14084 KB Output is correct