Submission #873390

# Submission time Handle Problem Language Result Execution time Memory
873390 2023-11-15T02:08:27 Z bobbilyking Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
183 ms 22352 KB
#include "supertrees.h"
using namespace std;
#include <bits/stdc++.h>
using ll=long long;
 
#define F(i, l, r) for (ll i = (l); i < ll(r); ++i)
bool vis[1010];
vector<ll> comp;
 
struct info {
	vector<ll> twos;
	vector<vector<ll>> ones;
};
 
vector<info> glob;
 
void dfs(ll i, vector<vector<int>>& p) {
	if (vis[i]) return;
	vis[i] = 1;
	comp.push_back(i);
	F(j, 0, p.size()) if (p[i][j]) dfs(j, p);
}
 
ll dsu[1010];
vector<ll> st[1010];
ll find(ll i) {
	return dsu[i] == i ? i: dsu[i] = find(dsu[i]);
}
 
 
bool my_build(std::vector<std::vector<int>>& b) {
	for (auto &[twos, ones]: glob) {
		bool flag = false;
		if (!twos.size()) {
			// if it's just one one, then we can get away with not doing shit
			// else, if it's two ones, we can't do it
			// else, we gotta make a cycle
			if (ones.size() == 1) flag = false;
			else if (ones.size() == 2) return false;
			else flag = true;
		} else {
			// we have a cycle, everyones gotta contribute
			flag = 1;
		}
		// for (auto x: twos) cout << x << " "; cout << endl;
		// cout << "-----\n";
		// for (auto v: ones) {
		// 	for (auto x: v)  cout << x << " "; cout << endl;
		// }
		// cout << "============\n";

		for (const auto &y: ones) {
			if (flag) twos.push_back(y[0]); // then we actually neeed this shit
			for (ll i = 0; i + 1 < ll(y.size()); ++i) b[y[i]][y[i+1]] = b[y[i+1]][y[i]] = 1;
		}
		if (twos.size() != 0 and twos.size() < 3) return false; // cant have a cycle with less than 3 nodes bruh
		F(i, 0, twos.size()) {
			ll nidx = (i+1)%twos.size();
			b[twos[i]][twos[nidx]] = b[twos[nidx]][twos[i]] = 1;
		}
	}
	return 1;
}
 
int construct(std::vector<std::vector<int>> p) {
	int n = p.size();
 
	for (auto &x: p) for (auto y: x) if (y==3) return 0;
	F(i, 0, n) {
		dsu[i] = i;
		st[i] = {i};
	}
 
	F(i, 0, n) if (!vis[i]) {
		comp.clear();
		dfs(i, p);
		if (comp.size() == 1) continue; // empty live my life yo
		info cur;
 
		for (auto idx: comp) {
			ll ign = 0;
			for (auto x: comp) if (x-idx) {
				if (!p[idx][x]) return false; // everything within component is connected
				if (p[idx][x] == 1) {
					ll u = find(idx), v = find(x);
					if (u-v) {	
						if (st[u].size() < st[v].size()) swap(u, v);
						dsu[v] = u;
						st[u].insert(st[u].end(), st[v].begin(), st[v].end());
					}
				} else ign++;
			}
			if (ign + 1 == ll(comp.size())) cur.twos.push_back(idx); // all twos
		}
 
		set<ll> ivis;
		for (auto idx: comp) if (st[find(idx)].size() > 1) ivis.insert(find(idx));
		for (auto idx: ivis) {
			for (auto y: st[idx]) for (auto z: st[idx]) if (p[y][z] != 1) return false;
			cur.ones.push_back(st[idx]);
		}
 
		glob.push_back(cur);
	}	
 
	std::vector<std::vector<int>> answer;
	for (int i = 0; i < n; i++) {
		std::vector<int> row;
		row.resize(n);
		answer.push_back(row);
	}
	if (!my_build(answer)) return false;
 
	build(answer);
	return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 7 ms 1324 KB Output is correct
7 Correct 160 ms 22232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 7 ms 1324 KB Output is correct
7 Correct 160 ms 22232 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 6 ms 1360 KB Output is correct
13 Correct 152 ms 22312 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 604 KB Output is correct
17 Correct 70 ms 8248 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 38 ms 5920 KB Output is correct
21 Correct 150 ms 22100 KB Output is correct
22 Correct 152 ms 22096 KB Output is correct
23 Correct 160 ms 22260 KB Output is correct
24 Correct 147 ms 22080 KB Output is correct
25 Correct 59 ms 8232 KB Output is correct
26 Correct 57 ms 8272 KB Output is correct
27 Correct 163 ms 22072 KB Output is correct
28 Correct 171 ms 22040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 6 ms 1200 KB Output is correct
9 Correct 147 ms 22068 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 8 ms 1372 KB Output is correct
13 Correct 156 ms 22296 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 596 KB Output is correct
16 Correct 4 ms 600 KB Output is correct
17 Correct 77 ms 8228 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 37 ms 5716 KB Output is correct
22 Correct 149 ms 22056 KB Output is correct
23 Correct 150 ms 22324 KB Output is correct
24 Correct 154 ms 22068 KB Output is correct
25 Correct 59 ms 12084 KB Output is correct
26 Correct 58 ms 8240 KB Output is correct
27 Correct 147 ms 22140 KB Output is correct
28 Correct 154 ms 22096 KB Output is correct
29 Correct 63 ms 12328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 38 ms 5748 KB Output is correct
5 Correct 150 ms 22100 KB Output is correct
6 Correct 154 ms 22096 KB Output is correct
7 Correct 156 ms 22096 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 41 ms 5808 KB Output is correct
10 Correct 149 ms 22096 KB Output is correct
11 Correct 147 ms 22100 KB Output is correct
12 Correct 164 ms 22052 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 37 ms 5748 KB Output is correct
17 Correct 183 ms 22164 KB Output is correct
18 Correct 157 ms 22352 KB Output is correct
19 Correct 147 ms 22104 KB Output is correct
20 Correct 146 ms 22052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 7 ms 1324 KB Output is correct
7 Correct 160 ms 22232 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 6 ms 1360 KB Output is correct
13 Correct 152 ms 22312 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 604 KB Output is correct
17 Correct 70 ms 8248 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 38 ms 5920 KB Output is correct
21 Correct 150 ms 22100 KB Output is correct
22 Correct 152 ms 22096 KB Output is correct
23 Correct 160 ms 22260 KB Output is correct
24 Correct 147 ms 22080 KB Output is correct
25 Correct 59 ms 8232 KB Output is correct
26 Correct 57 ms 8272 KB Output is correct
27 Correct 163 ms 22072 KB Output is correct
28 Correct 171 ms 22040 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 0 ms 344 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 6 ms 1200 KB Output is correct
37 Correct 147 ms 22068 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 8 ms 1372 KB Output is correct
41 Correct 156 ms 22296 KB Output is correct
42 Correct 0 ms 344 KB Output is correct
43 Correct 0 ms 596 KB Output is correct
44 Correct 4 ms 600 KB Output is correct
45 Correct 77 ms 8228 KB Output is correct
46 Correct 0 ms 344 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 37 ms 5716 KB Output is correct
50 Correct 149 ms 22056 KB Output is correct
51 Correct 150 ms 22324 KB Output is correct
52 Correct 154 ms 22068 KB Output is correct
53 Correct 59 ms 12084 KB Output is correct
54 Correct 58 ms 8240 KB Output is correct
55 Correct 147 ms 22140 KB Output is correct
56 Correct 154 ms 22096 KB Output is correct
57 Correct 63 ms 12328 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 5 ms 604 KB Output is correct
61 Correct 66 ms 8396 KB Output is correct
62 Correct 0 ms 344 KB Output is correct
63 Correct 0 ms 348 KB Output is correct
64 Correct 1 ms 348 KB Output is correct
65 Correct 49 ms 5788 KB Output is correct
66 Correct 58 ms 10320 KB Output is correct
67 Correct 59 ms 10324 KB Output is correct
68 Correct 58 ms 10136 KB Output is correct
69 Correct 74 ms 14228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 7 ms 1324 KB Output is correct
7 Correct 160 ms 22232 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 6 ms 1360 KB Output is correct
13 Correct 152 ms 22312 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 604 KB Output is correct
17 Correct 70 ms 8248 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 38 ms 5920 KB Output is correct
21 Correct 150 ms 22100 KB Output is correct
22 Correct 152 ms 22096 KB Output is correct
23 Correct 160 ms 22260 KB Output is correct
24 Correct 147 ms 22080 KB Output is correct
25 Correct 59 ms 8232 KB Output is correct
26 Correct 57 ms 8272 KB Output is correct
27 Correct 163 ms 22072 KB Output is correct
28 Correct 171 ms 22040 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 0 ms 344 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 6 ms 1200 KB Output is correct
37 Correct 147 ms 22068 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 8 ms 1372 KB Output is correct
41 Correct 156 ms 22296 KB Output is correct
42 Correct 0 ms 344 KB Output is correct
43 Correct 0 ms 596 KB Output is correct
44 Correct 4 ms 600 KB Output is correct
45 Correct 77 ms 8228 KB Output is correct
46 Correct 0 ms 344 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 37 ms 5716 KB Output is correct
50 Correct 149 ms 22056 KB Output is correct
51 Correct 150 ms 22324 KB Output is correct
52 Correct 154 ms 22068 KB Output is correct
53 Correct 59 ms 12084 KB Output is correct
54 Correct 58 ms 8240 KB Output is correct
55 Correct 147 ms 22140 KB Output is correct
56 Correct 154 ms 22096 KB Output is correct
57 Correct 63 ms 12328 KB Output is correct
58 Correct 0 ms 344 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 38 ms 5748 KB Output is correct
62 Correct 150 ms 22100 KB Output is correct
63 Correct 154 ms 22096 KB Output is correct
64 Correct 156 ms 22096 KB Output is correct
65 Correct 0 ms 344 KB Output is correct
66 Correct 41 ms 5808 KB Output is correct
67 Correct 149 ms 22096 KB Output is correct
68 Correct 147 ms 22100 KB Output is correct
69 Correct 164 ms 22052 KB Output is correct
70 Correct 1 ms 344 KB Output is correct
71 Correct 0 ms 348 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 37 ms 5748 KB Output is correct
74 Correct 183 ms 22164 KB Output is correct
75 Correct 157 ms 22352 KB Output is correct
76 Correct 147 ms 22104 KB Output is correct
77 Correct 146 ms 22052 KB Output is correct
78 Correct 0 ms 348 KB Output is correct
79 Correct 0 ms 348 KB Output is correct
80 Correct 5 ms 604 KB Output is correct
81 Correct 66 ms 8396 KB Output is correct
82 Correct 0 ms 344 KB Output is correct
83 Correct 0 ms 348 KB Output is correct
84 Correct 1 ms 348 KB Output is correct
85 Correct 49 ms 5788 KB Output is correct
86 Correct 58 ms 10320 KB Output is correct
87 Correct 59 ms 10324 KB Output is correct
88 Correct 58 ms 10136 KB Output is correct
89 Correct 74 ms 14228 KB Output is correct
90 Correct 0 ms 344 KB Output is correct
91 Correct 1 ms 344 KB Output is correct
92 Correct 3 ms 836 KB Output is correct
93 Correct 61 ms 10272 KB Output is correct
94 Correct 0 ms 344 KB Output is correct
95 Correct 0 ms 348 KB Output is correct
96 Correct 0 ms 348 KB Output is correct
97 Correct 17 ms 2908 KB Output is correct
98 Correct 58 ms 10136 KB Output is correct
99 Correct 58 ms 10148 KB Output is correct
100 Correct 57 ms 10088 KB Output is correct
101 Correct 63 ms 10184 KB Output is correct
102 Correct 58 ms 10068 KB Output is correct
103 Correct 59 ms 10188 KB Output is correct
104 Correct 58 ms 10064 KB Output is correct
105 Correct 79 ms 10236 KB Output is correct