Submission #304202

# Submission time Handle Problem Language Result Execution time Memory
304202 2020-09-21T04:18:49 Z eggag32 Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
257 ms 30200 KB
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) debug(x), debug(y)
#define repn(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for(int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end() 
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))
const int mxN = 1005;

template<class T> T gcd(T a, T b){ return ((b == 0) ? a : gcd(b, a % b)); }

vector<pi> g[mxN];
int vis[mxN];
vi cur1, cur2;

void dfs3(int cur){
	vis[cur] = 1;
	cur2.pb(cur);
	for(pi x : g[cur]) if(x.se == 3 && !vis[x.fi]) dfs3(x.fi);
}

void dfs2(int cur){
	vis[cur] = 1;
	cur2.pb(cur);
	for(pi x : g[cur]) if(x.se == 2 && !vis[x.fi]) dfs2(x.fi);
}

void dfs1(int cur){
	vis[cur] = 1;
	cur1.pb(cur);
	for(pi x : g[cur]) if(x.se == 1 && !vis[x.fi]) dfs1(x.fi);
}

int construct(vector<vi> p) {
	int n = p.size();
	vector<vi> ans(n);
	rep(i, n) ans[i].resize(n);
	rep(i, n) rep(j, n) ans[i][j] = 0;
	int mx = 0;
	rep(i, n) rep(j, n) if(p[i][j] && i != j){
		g[i].pb({j, p[i][j]});
		mx = max(mx, p[i][j]);
	}
	if(mx == 3){
		rep(i, n) if(!vis[i]){
			int f = 0;
			for(pi x : g[i]){
				if(x.se == 2) f |= 2;
				if(x.se == 3) f |= 4;
			}
			if(f == 6) return 0;
			if(f == 4){
				cur2.clear();
				dfs3(i);
				vi circ; // the arc
				vi vis1((int)cur2.size(), 0);
				rep(j, cur2.size()) if(!vis1[j]){
					int cr = cur2[j];
					vis1[j] = 1;
					circ.pb(cr);
					vi ch = {cr};
					rep(k, cur2.size()) if(j != k && !vis1[k]){
						if(!p[cur2[j]][cur2[k]]) return 0;
						if(p[cur2[j]][cur2[k]] == 2) return 0;
						if(p[cur2[j]][cur2[k]] == 1){
							vis1[k] = 1;
							ans[cr][cur2[k]] = 1;
							ans[cur2[k]][cr] = 1;
							ch.pb(cur2[k]);
						}
					}
					rep(l, ch.size()){
						rep(k, cur2.size()) if(vis1[k]){
							if(p[ch[l]][cur2[k]] != 3) return 0;
						}
					}
					rep(k, ch.size()) rep(l, ch.size()) if(p[ch[k]][ch[l]] != 1) return 0;
				}
				if(circ.size() < 4) return 0;
				rep(j, circ.size() - 1){
					ans[circ[j]][circ[j + 1]] = 1;
					ans[circ[j + 1]][circ[j]] = 1;
				}
				int m = (int)circ.size() - 1;
				ans[circ[0]][circ[m]] = 1;
				ans[circ[m]][circ[0]] = 1;
				m--;
				ans[circ[0]][circ[m]] = 1;
				ans[circ[m]][circ[0]] = 1;
			}
		}
		rep(i, n) if(!vis[i]){
			int f = 0;
			for(pi x : g[i]){
				if(x.se == 2) f |= 2;
				if(x.se == 3) f |= 4;
			}
			if(f == 2){
				cur2.clear();
				dfs2(i);
				vi circ; // the arc
				vi vis1((int)cur2.size(), 0);
				rep(j, cur2.size()) if(!vis1[j]){
					int cr = cur2[j];
					vis1[j] = 1;
					circ.pb(cr);
					vi ch = {cr};
					rep(k, cur2.size()) if(j != k && !vis1[k]){
						if(!p[cur2[j]][cur2[k]]){
							return 0;
						}
						if(p[cur2[j]][cur2[k]] == 3){
							return 0;
						}
						if(p[cur2[j]][cur2[k]] == 1){
							vis1[k] = 1;
							ans[cr][cur2[k]] = 1;
							ans[cur2[k]][cr] = 1;
							ch.pb(cur2[k]);
						}
					}
					rep(l, ch.size()){
						rep(k, cur2.size()) if(vis1[k]){
							if(p[ch[l]][cur2[k]] != 2) return 0;
						}
					}
					rep(k, ch.size()) rep(l, ch.size()) if(p[ch[k]][ch[l]] != 1) return 0;
				}
				if(circ.size() < 3) return 0;
				rep(j, circ.size() - 1){
					ans[circ[j]][circ[j + 1]] = 1;
					ans[circ[j + 1]][circ[j]] = 1;
				}
				int m = (int)circ.size() - 1;
				ans[circ[0]][circ[m]] = 1;
				ans[circ[m]][circ[0]] = 1;
			}
		}
		rep(i, n) if(!vis[i]){
			int f = 0;
			for(pi x : g[i]){
				if(x.se == 1) f |= 1;
				if(x.se == 2) f |= 2;
				if(x.se == 3) f |= 4;
			}
			if(f == 1){
				cur1.clear();
				dfs1(i);
				rep(j, cur1.size()) rep(k, cur1.size()) if(j != k){
					if(p[cur1[j]][cur1[k]] != 1) return 0;
				}
				sort(all(cur1));
				rep(j, cur1.size() - 1){
					ans[cur1[j]][cur1[j + 1]] = 1;
					ans[cur1[j + 1]][cur1[j]] = 1;
				}
			}
		}
		build(ans);
		return 1;
	}
	rep(i, n) if(!vis[i]){
		int f = 0;
		for(pi x : g[i]) if(x.se == 2) f |= 2;
		if(f == 2){
			cur2.clear();
			dfs2(i);
			vi circ; // the arc
			vi vis1((int)cur2.size(), 0);
			rep(j, cur2.size()) if(!vis1[j]){
				int cr = cur2[j];
				vis1[j] = 1;
				circ.pb(cr);
				rep(k, cur2.size()) if(j != k && !vis1[k]){
					if(!p[cur2[j]][cur2[k]]){
						return 0;
					}
					if(p[cur2[j]][cur2[k]] == 1){
						vis1[k] = 1;
						ans[cr][cur2[k]] = 1;
						ans[cur2[k]][cr] = 1;
					}
				}
			}
			if(circ.size() < 3) return 0;
			rep(j, circ.size() - 1){
				ans[circ[j]][circ[j + 1]] = 1;
				ans[circ[j + 1]][circ[j]] = 1;
			}
			int m = (int)circ.size() - 1;
			ans[circ[0]][circ[m]] = 1;
			ans[circ[m]][circ[0]] = 1;
		}
	}
	rep(i, n) if(!vis[i]){
		int f = 0;
		for(pi x : g[i]){
			if(x.se == 1) f |= 1;
			if(x.se == 2) f |= 2;
		}
		if(f == 1){
			cur1.clear();
			dfs1(i);
			rep(j, cur1.size()) rep(k, cur1.size()) if(j != k){
				if(p[cur1[j]][cur1[k]] != 1) return 0;
			}
			sort(all(cur1));
			rep(j, cur1.size() - 1){
				ans[cur1[j]][cur1[j + 1]] = 1;
				ans[cur1[j + 1]][cur1[j]] = 1;
			}
		}
	}
	build(ans);
	return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 11 ms 1664 KB Output is correct
7 Correct 256 ms 30200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 11 ms 1664 KB Output is correct
7 Correct 256 ms 30200 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 10 ms 1280 KB Output is correct
13 Correct 241 ms 22212 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 5 ms 1024 KB Output is correct
17 Correct 115 ms 17016 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 60 ms 5880 KB Output is correct
21 Correct 243 ms 23160 KB Output is correct
22 Correct 233 ms 22264 KB Output is correct
23 Correct 249 ms 26104 KB Output is correct
24 Correct 237 ms 22132 KB Output is correct
25 Correct 101 ms 13180 KB Output is correct
26 Correct 95 ms 12276 KB Output is correct
27 Correct 251 ms 28384 KB Output is correct
28 Correct 241 ms 22136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 10 ms 1212 KB Output is correct
9 Correct 232 ms 22136 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 11 ms 1664 KB Output is correct
13 Correct 253 ms 30200 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 5 ms 1024 KB Output is correct
17 Correct 114 ms 17016 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 60 ms 5880 KB Output is correct
22 Correct 241 ms 23288 KB Output is correct
23 Correct 233 ms 22136 KB Output is correct
24 Correct 251 ms 28280 KB Output is correct
25 Correct 96 ms 12152 KB Output is correct
26 Correct 101 ms 13176 KB Output is correct
27 Correct 239 ms 22136 KB Output is correct
28 Correct 257 ms 26108 KB Output is correct
29 Correct 94 ms 12152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 71 ms 5952 KB Output is correct
5 Correct 237 ms 23160 KB Output is correct
6 Correct 244 ms 22264 KB Output is correct
7 Correct 251 ms 26144 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 59 ms 5880 KB Output is correct
10 Correct 242 ms 23160 KB Output is correct
11 Correct 232 ms 22136 KB Output is correct
12 Correct 250 ms 28280 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 61 ms 5880 KB Output is correct
17 Correct 240 ms 23160 KB Output is correct
18 Correct 239 ms 23160 KB Output is correct
19 Correct 234 ms 22520 KB Output is correct
20 Correct 234 ms 22224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 11 ms 1664 KB Output is correct
7 Correct 256 ms 30200 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 10 ms 1280 KB Output is correct
13 Correct 241 ms 22212 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 5 ms 1024 KB Output is correct
17 Correct 115 ms 17016 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 60 ms 5880 KB Output is correct
21 Correct 243 ms 23160 KB Output is correct
22 Correct 233 ms 22264 KB Output is correct
23 Correct 249 ms 26104 KB Output is correct
24 Correct 237 ms 22132 KB Output is correct
25 Correct 101 ms 13180 KB Output is correct
26 Correct 95 ms 12276 KB Output is correct
27 Correct 251 ms 28384 KB Output is correct
28 Correct 241 ms 22136 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 0 ms 384 KB Output is correct
31 Correct 0 ms 384 KB Output is correct
32 Correct 1 ms 384 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 1 ms 384 KB Output is correct
35 Correct 1 ms 384 KB Output is correct
36 Correct 10 ms 1212 KB Output is correct
37 Correct 232 ms 22136 KB Output is correct
38 Correct 1 ms 384 KB Output is correct
39 Correct 1 ms 384 KB Output is correct
40 Correct 11 ms 1664 KB Output is correct
41 Correct 253 ms 30200 KB Output is correct
42 Correct 1 ms 384 KB Output is correct
43 Correct 1 ms 384 KB Output is correct
44 Correct 5 ms 1024 KB Output is correct
45 Correct 114 ms 17016 KB Output is correct
46 Correct 0 ms 384 KB Output is correct
47 Correct 0 ms 384 KB Output is correct
48 Correct 0 ms 384 KB Output is correct
49 Correct 60 ms 5880 KB Output is correct
50 Correct 241 ms 23288 KB Output is correct
51 Correct 233 ms 22136 KB Output is correct
52 Correct 251 ms 28280 KB Output is correct
53 Correct 96 ms 12152 KB Output is correct
54 Correct 101 ms 13176 KB Output is correct
55 Correct 239 ms 22136 KB Output is correct
56 Correct 257 ms 26108 KB Output is correct
57 Correct 94 ms 12152 KB Output is correct
58 Correct 1 ms 384 KB Output is correct
59 Correct 0 ms 384 KB Output is correct
60 Correct 5 ms 1152 KB Output is correct
61 Correct 121 ms 20216 KB Output is correct
62 Correct 1 ms 384 KB Output is correct
63 Correct 0 ms 384 KB Output is correct
64 Correct 0 ms 384 KB Output is correct
65 Correct 60 ms 5884 KB Output is correct
66 Correct 100 ms 13304 KB Output is correct
67 Correct 102 ms 13176 KB Output is correct
68 Correct 102 ms 12536 KB Output is correct
69 Correct 101 ms 12152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 11 ms 1664 KB Output is correct
7 Correct 256 ms 30200 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 0 ms 384 KB Output is correct
12 Correct 10 ms 1280 KB Output is correct
13 Correct 241 ms 22212 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 5 ms 1024 KB Output is correct
17 Correct 115 ms 17016 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 60 ms 5880 KB Output is correct
21 Correct 243 ms 23160 KB Output is correct
22 Correct 233 ms 22264 KB Output is correct
23 Correct 249 ms 26104 KB Output is correct
24 Correct 237 ms 22132 KB Output is correct
25 Correct 101 ms 13180 KB Output is correct
26 Correct 95 ms 12276 KB Output is correct
27 Correct 251 ms 28384 KB Output is correct
28 Correct 241 ms 22136 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 0 ms 384 KB Output is correct
31 Correct 0 ms 384 KB Output is correct
32 Correct 1 ms 384 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 1 ms 384 KB Output is correct
35 Correct 1 ms 384 KB Output is correct
36 Correct 10 ms 1212 KB Output is correct
37 Correct 232 ms 22136 KB Output is correct
38 Correct 1 ms 384 KB Output is correct
39 Correct 1 ms 384 KB Output is correct
40 Correct 11 ms 1664 KB Output is correct
41 Correct 253 ms 30200 KB Output is correct
42 Correct 1 ms 384 KB Output is correct
43 Correct 1 ms 384 KB Output is correct
44 Correct 5 ms 1024 KB Output is correct
45 Correct 114 ms 17016 KB Output is correct
46 Correct 0 ms 384 KB Output is correct
47 Correct 0 ms 384 KB Output is correct
48 Correct 0 ms 384 KB Output is correct
49 Correct 60 ms 5880 KB Output is correct
50 Correct 241 ms 23288 KB Output is correct
51 Correct 233 ms 22136 KB Output is correct
52 Correct 251 ms 28280 KB Output is correct
53 Correct 96 ms 12152 KB Output is correct
54 Correct 101 ms 13176 KB Output is correct
55 Correct 239 ms 22136 KB Output is correct
56 Correct 257 ms 26108 KB Output is correct
57 Correct 94 ms 12152 KB Output is correct
58 Correct 1 ms 384 KB Output is correct
59 Correct 0 ms 384 KB Output is correct
60 Correct 1 ms 384 KB Output is correct
61 Correct 71 ms 5952 KB Output is correct
62 Correct 237 ms 23160 KB Output is correct
63 Correct 244 ms 22264 KB Output is correct
64 Correct 251 ms 26144 KB Output is correct
65 Correct 0 ms 384 KB Output is correct
66 Correct 59 ms 5880 KB Output is correct
67 Correct 242 ms 23160 KB Output is correct
68 Correct 232 ms 22136 KB Output is correct
69 Correct 250 ms 28280 KB Output is correct
70 Correct 1 ms 384 KB Output is correct
71 Correct 1 ms 384 KB Output is correct
72 Correct 1 ms 384 KB Output is correct
73 Correct 61 ms 5880 KB Output is correct
74 Correct 240 ms 23160 KB Output is correct
75 Correct 239 ms 23160 KB Output is correct
76 Correct 234 ms 22520 KB Output is correct
77 Correct 234 ms 22224 KB Output is correct
78 Correct 1 ms 384 KB Output is correct
79 Correct 0 ms 384 KB Output is correct
80 Correct 5 ms 1152 KB Output is correct
81 Correct 121 ms 20216 KB Output is correct
82 Correct 1 ms 384 KB Output is correct
83 Correct 0 ms 384 KB Output is correct
84 Correct 0 ms 384 KB Output is correct
85 Correct 60 ms 5884 KB Output is correct
86 Correct 100 ms 13304 KB Output is correct
87 Correct 102 ms 13176 KB Output is correct
88 Correct 102 ms 12536 KB Output is correct
89 Correct 101 ms 12152 KB Output is correct
90 Correct 0 ms 384 KB Output is correct
91 Correct 1 ms 384 KB Output is correct
92 Correct 5 ms 1280 KB Output is correct
93 Correct 114 ms 20344 KB Output is correct
94 Correct 1 ms 384 KB Output is correct
95 Correct 1 ms 384 KB Output is correct
96 Correct 0 ms 384 KB Output is correct
97 Correct 25 ms 3320 KB Output is correct
98 Correct 100 ms 13180 KB Output is correct
99 Correct 105 ms 13176 KB Output is correct
100 Correct 96 ms 12536 KB Output is correct
101 Correct 94 ms 12152 KB Output is correct
102 Correct 113 ms 20216 KB Output is correct
103 Correct 112 ms 20216 KB Output is correct
104 Correct 115 ms 20312 KB Output is correct
105 Correct 118 ms 17144 KB Output is correct