Submission #795872

# Submission time Handle Problem Language Result Execution time Memory
795872 2023-07-27T18:56:52 Z hgmhc Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
185 ms 24140 KB
#include "supertrees.h"
#include <bits/stdc++.h>
#define rep(i,a,b) for (auto i = (a); i <= (b); ++i)
#define all(x) begin(x), end(x)
#define siz(x) int((x).size())
#define dbg(...) fprintf(stderr,__VA_ARGS__)
using namespace std;
using ii = pair<int,int>;

const int N = 1003;
int n;
vector<vector<int>> adj;

struct disjoint_set {
	int p[N];
	disjoint_set(){ iota(p,p+N,0); }
	int find(int x){
		if (x==p[x]) return x;
		return p[x]=find(p[x]);
	}
	bool same(int a, int b){ return find(a)==find(b); }
	void merge(int a, int b) {
		a = find(a), b = find(b);
		assert(a!=b);
		p[a]=b;
	}
} ds, DS;

bool found3(vector<vector<int>> &p) {
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (p[i][j] == 3) return true;
		}
	}
	return false;
}

vector<int> roots;

void connect_ones(vector<vector<int>> &p) {
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) if (p[ds.find(i)][ds.find(j)] == 1) {
			if (not ds.same(i,j)) {
				adj[ds.find(i)][ds.find(j)] =
				adj[ds.find(j)][ds.find(i)] = 1;
				ds.merge(i,j);
			}
		}
	}
}

bool doit(vector<vector<int>> &p) {
	for (int i = 0; i < n; ++i) roots.push_back(ds.find(i));
	sort(all(roots)), roots.erase(unique(all(roots)),end(roots));
	for (auto r1 : roots) for (auto r2 : roots) if (p[r1][r2] == 2) {
		if (not DS.same(r1,r2)) DS.merge(r1,r2);
	}
	vector<vector<int>> comp(n);
	for (auto r : roots) comp[DS.find(r)].push_back(r);
	for (auto &c : comp) {
		if (siz(c) < 2) continue;
		if (siz(c) == 2) return false;
		for (int j = 0; j < siz(c); ++j) {
			adj[c[j]][c[(j+1)%siz(c)]] = 1;
			adj[c[(j+1)%siz(c)]][c[j]] = 1;
		}
	}
	return true;
}

int construct(vector<vector<int>> p) {
	n = p.size(), adj.resize(n,vector<int>(n));
	if (found3(p)) return 0;
	connect_ones(p);
	if (!doit(p)) return 0;
	
	for (int i = 0; i < n; ++i) {
		adj[i][i] = 0;
		for (int j = 0; j < n; ++j) if (i != j) {
			if (p[i][j] == 0) {
				if (ds.same(i,j)) return 0;
				if (DS.same(ds.find(i),ds.find(j))) return 0;
			} else if (p[i][j] == 1) {
				if (not ds.same(i,j)) return 0;
			} else if (p[i][j] == 2) {
				if (ds.same(i,j)) return 0;
				if (not DS.same(ds.find(i),ds.find(j))) return 0;
			} else assert(0);
		}
	}
	
	build(adj);
	return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 1 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 1 ms 212 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
6 Correct 7 ms 1236 KB Output is correct
7 Correct 161 ms 23980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 1 ms 212 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
6 Correct 7 ms 1236 KB Output is correct
7 Correct 161 ms 23980 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 308 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 7 ms 1220 KB Output is correct
13 Correct 165 ms 24140 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 3 ms 852 KB Output is correct
17 Correct 77 ms 14072 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 40 ms 6244 KB Output is correct
21 Correct 160 ms 24012 KB Output is correct
22 Correct 170 ms 23944 KB Output is correct
23 Correct 170 ms 23940 KB Output is correct
24 Correct 159 ms 23940 KB Output is correct
25 Correct 79 ms 14072 KB Output is correct
26 Correct 69 ms 14080 KB Output is correct
27 Correct 170 ms 24000 KB Output is correct
28 Correct 158 ms 23948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 308 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 304 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 7 ms 1216 KB Output is correct
9 Correct 154 ms 24016 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 8 ms 1220 KB Output is correct
13 Correct 165 ms 23932 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 308 KB Output is correct
16 Correct 4 ms 812 KB Output is correct
17 Correct 75 ms 14124 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 40 ms 6204 KB Output is correct
22 Correct 159 ms 23992 KB Output is correct
23 Correct 159 ms 23948 KB Output is correct
24 Correct 166 ms 24016 KB Output is correct
25 Correct 65 ms 14124 KB Output is correct
26 Correct 65 ms 14028 KB Output is correct
27 Correct 159 ms 23984 KB Output is correct
28 Correct 166 ms 23940 KB Output is correct
29 Correct 71 ms 14072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 304 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 42 ms 6156 KB Output is correct
5 Correct 184 ms 23948 KB Output is correct
6 Correct 160 ms 23944 KB Output is correct
7 Correct 167 ms 23928 KB Output is correct
8 Correct 0 ms 304 KB Output is correct
9 Correct 40 ms 6224 KB Output is correct
10 Correct 158 ms 23972 KB Output is correct
11 Correct 160 ms 23944 KB Output is correct
12 Correct 166 ms 24004 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 304 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 39 ms 6220 KB Output is correct
17 Correct 162 ms 24060 KB Output is correct
18 Correct 170 ms 24008 KB Output is correct
19 Correct 160 ms 23948 KB Output is correct
20 Correct 185 ms 23944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 1 ms 212 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
6 Correct 7 ms 1236 KB Output is correct
7 Correct 161 ms 23980 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 308 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 7 ms 1220 KB Output is correct
13 Correct 165 ms 24140 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 3 ms 852 KB Output is correct
17 Correct 77 ms 14072 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 40 ms 6244 KB Output is correct
21 Correct 160 ms 24012 KB Output is correct
22 Correct 170 ms 23944 KB Output is correct
23 Correct 170 ms 23940 KB Output is correct
24 Correct 159 ms 23940 KB Output is correct
25 Correct 79 ms 14072 KB Output is correct
26 Correct 69 ms 14080 KB Output is correct
27 Correct 170 ms 24000 KB Output is correct
28 Correct 158 ms 23948 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 308 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 304 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 7 ms 1216 KB Output is correct
37 Correct 154 ms 24016 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 8 ms 1220 KB Output is correct
41 Correct 165 ms 23932 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 308 KB Output is correct
44 Correct 4 ms 812 KB Output is correct
45 Correct 75 ms 14124 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 40 ms 6204 KB Output is correct
50 Correct 159 ms 23992 KB Output is correct
51 Correct 159 ms 23948 KB Output is correct
52 Correct 166 ms 24016 KB Output is correct
53 Correct 65 ms 14124 KB Output is correct
54 Correct 65 ms 14028 KB Output is correct
55 Correct 159 ms 23984 KB Output is correct
56 Correct 166 ms 23940 KB Output is correct
57 Correct 71 ms 14072 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 1 ms 304 KB Output is correct
60 Correct 3 ms 852 KB Output is correct
61 Correct 92 ms 14088 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 1 ms 212 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Correct 39 ms 6220 KB Output is correct
66 Correct 66 ms 14072 KB Output is correct
67 Correct 68 ms 14076 KB Output is correct
68 Correct 83 ms 14088 KB Output is correct
69 Correct 68 ms 14088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 1 ms 212 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
6 Correct 7 ms 1236 KB Output is correct
7 Correct 161 ms 23980 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 308 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 7 ms 1220 KB Output is correct
13 Correct 165 ms 24140 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 3 ms 852 KB Output is correct
17 Correct 77 ms 14072 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 40 ms 6244 KB Output is correct
21 Correct 160 ms 24012 KB Output is correct
22 Correct 170 ms 23944 KB Output is correct
23 Correct 170 ms 23940 KB Output is correct
24 Correct 159 ms 23940 KB Output is correct
25 Correct 79 ms 14072 KB Output is correct
26 Correct 69 ms 14080 KB Output is correct
27 Correct 170 ms 24000 KB Output is correct
28 Correct 158 ms 23948 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 308 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 304 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 7 ms 1216 KB Output is correct
37 Correct 154 ms 24016 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 8 ms 1220 KB Output is correct
41 Correct 165 ms 23932 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 308 KB Output is correct
44 Correct 4 ms 812 KB Output is correct
45 Correct 75 ms 14124 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 40 ms 6204 KB Output is correct
50 Correct 159 ms 23992 KB Output is correct
51 Correct 159 ms 23948 KB Output is correct
52 Correct 166 ms 24016 KB Output is correct
53 Correct 65 ms 14124 KB Output is correct
54 Correct 65 ms 14028 KB Output is correct
55 Correct 159 ms 23984 KB Output is correct
56 Correct 166 ms 23940 KB Output is correct
57 Correct 71 ms 14072 KB Output is correct
58 Correct 1 ms 304 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 42 ms 6156 KB Output is correct
62 Correct 184 ms 23948 KB Output is correct
63 Correct 160 ms 23944 KB Output is correct
64 Correct 167 ms 23928 KB Output is correct
65 Correct 0 ms 304 KB Output is correct
66 Correct 40 ms 6224 KB Output is correct
67 Correct 158 ms 23972 KB Output is correct
68 Correct 160 ms 23944 KB Output is correct
69 Correct 166 ms 24004 KB Output is correct
70 Correct 1 ms 212 KB Output is correct
71 Correct 1 ms 304 KB Output is correct
72 Correct 1 ms 212 KB Output is correct
73 Correct 39 ms 6220 KB Output is correct
74 Correct 162 ms 24060 KB Output is correct
75 Correct 170 ms 24008 KB Output is correct
76 Correct 160 ms 23948 KB Output is correct
77 Correct 185 ms 23944 KB Output is correct
78 Correct 1 ms 212 KB Output is correct
79 Correct 1 ms 304 KB Output is correct
80 Correct 3 ms 852 KB Output is correct
81 Correct 92 ms 14088 KB Output is correct
82 Correct 1 ms 212 KB Output is correct
83 Correct 1 ms 212 KB Output is correct
84 Correct 1 ms 212 KB Output is correct
85 Correct 39 ms 6220 KB Output is correct
86 Correct 66 ms 14072 KB Output is correct
87 Correct 68 ms 14076 KB Output is correct
88 Correct 83 ms 14088 KB Output is correct
89 Correct 68 ms 14088 KB Output is correct
90 Correct 1 ms 312 KB Output is correct
91 Correct 0 ms 212 KB Output is correct
92 Correct 3 ms 804 KB Output is correct
93 Correct 66 ms 14088 KB Output is correct
94 Correct 1 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 16 ms 3712 KB Output is correct
98 Correct 69 ms 14076 KB Output is correct
99 Correct 63 ms 14084 KB Output is correct
100 Correct 65 ms 14000 KB Output is correct
101 Correct 65 ms 14072 KB Output is correct
102 Correct 63 ms 14088 KB Output is correct
103 Correct 63 ms 14084 KB Output is correct
104 Correct 64 ms 14028 KB Output is correct
105 Correct 67 ms 14080 KB Output is correct