Submission #896591

# Submission time Handle Problem Language Result Execution time Memory
896591 2024-01-01T17:27:08 Z LCJLY Connecting Supertrees (IOI20_supertrees) C++14
96 / 100
184 ms 22864 KB
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << "  " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << "  " << j << " " << #i << "  " << q << " " << #p << endl; 
#define show4(x,y) for(auto it:x) cout << it << " "; cout << #y << endl;

struct DSU{
	vector<int>e;
	vector<int>storage[1005];
	void init(int n){
		e=vector<int>(n,-1);
		for(int x=0;x<n;x++){
			storage[x].push_back(x);
		}
	}
	
	int get(int x){return e[x]<0?x:e[x]=get(e[x]);}
	
	bool sameSet(int x, int y){return get(x)==get(y);}
	
	int size(int x){return -e[get(x)];}
	
	bool unite(int x, int y){
		x=get(x);
		y=get(y);
		if(x==y) return 0;
		if(e[x]>e[y]) swap(x,y);
		e[x]+=e[y];
		for(auto it:storage[y]) storage[x].push_back(it);
		e[y]=x;
		return 1;
	}
};

int construct(vector<vector<int>> p) {
	int n=p.size();
	DSU one;
	one.init(n+5);
	DSU two;
	two.init(n+5);
	DSU three;
	three.init(n+5);
	
	for(int x=0;x<n;x++){
		for(int y=0;y<n;y++){
			if(p[x][y]==1){
				one.unite(x,y);
			}
			else if(p[x][y]==2){
				two.unite(x,y);
			}
			else if(p[x][y]==3){
				three.unite(x,y);
			}
		}
	}
	
	vector<vector<int>>ans;
	ans.resize(n);
	for(int x=0;x<n;x++){
		for(int y=0;y<n;y++){
			ans[x].push_back(0);
		}
	}
	
	bool done[n+5];
	memset(done,0,sizeof(done));
	for(int x=0;x<n;x++){
		if(one.get(x)==x){
			int last=-1;
			for(auto it:one.storage[x]){
				if(last!=-1){
					ans[last][it]=1;
					ans[it][last]=1;
					done[it]=true;
				}
				last=it;
			}
			
			for(auto it:one.storage[x]){
				for(auto it2:one.storage[x]){
					if(p[it][it2]!=1) return 0;
				}
			}
		}
	}
	
	for(int x=0;x<n;x++){
		if(two.get(x)==x&&two.size(x)>1){
			if(two.size(x)==2) return 0;
			int last=-1;
			for(auto it:two.storage[x]){
				if(done[it]) continue; 
				if(last!=-1){
					ans[last][it]=1;
					ans[it][last]=1;
				}
				last=it;
			}
			if(last!=-1){
				ans[last][two.storage[x][0]]=1;
				ans[two.storage[x][0]][last]=1;
			}
			for(auto it:two.storage[x]){
				for(auto it2:two.storage[x]){
					if(it==it2) continue;
					if(one.sameSet(it,it2)) continue;
					if(p[it][it2]!=2) return 0;
				}
			}
		}
	}
	
	for(int x=0;x<n;x++){
		if(three.get(x)==x&&three.size(x)>1){
			if(three.size(x)<=3) return 0;
			int last=-1;
			for(auto it:three.storage[x]){
				if(done[it]) continue; 
				if(last!=-1){
					ans[last][it]=1;
					ans[it][last]=1;
				}
				last=it;
			}
			if(last!=-1){
				ans[last][three.storage[x][0]]=1;
				ans[three.storage[x][0]][last]=1;
				
				ans[last][three.storage[x][1]]=1;
				ans[three.storage[x][1]][last]=1;
			}
			for(auto it:three.storage[x]){
				for(auto it2:three.storage[x]){
					if(it==it2) continue;
					if(one.sameSet(it,it2)) continue;
					if(two.sameSet(it,it2)) return 0;
					if(p[it][it2]!=3) return 0;
				}
			}
		}
	}

	build(ans);
	return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 500 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 8 ms 1372 KB Output is correct
7 Correct 150 ms 22296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 500 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 8 ms 1372 KB Output is correct
7 Correct 150 ms 22296 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 6 ms 1372 KB Output is correct
13 Correct 144 ms 22356 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 856 KB Output is correct
17 Correct 74 ms 12368 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 37 ms 6036 KB Output is correct
21 Correct 148 ms 22296 KB Output is correct
22 Correct 152 ms 22276 KB Output is correct
23 Correct 156 ms 22276 KB Output is correct
24 Correct 184 ms 22320 KB Output is correct
25 Correct 64 ms 12324 KB Output is correct
26 Correct 63 ms 12296 KB Output is correct
27 Correct 160 ms 22356 KB Output is correct
28 Correct 149 ms 22352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 356 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 9 ms 1368 KB Output is correct
9 Correct 148 ms 22276 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 7 ms 1372 KB Output is correct
13 Correct 148 ms 22296 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 4 ms 856 KB Output is correct
17 Correct 81 ms 12316 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 37 ms 5968 KB Output is correct
22 Correct 148 ms 22460 KB Output is correct
23 Correct 149 ms 22384 KB Output is correct
24 Correct 156 ms 22864 KB Output is correct
25 Correct 68 ms 12372 KB Output is correct
26 Correct 65 ms 12292 KB Output is correct
27 Correct 144 ms 22276 KB Output is correct
28 Correct 154 ms 22320 KB Output is correct
29 Correct 63 ms 12368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 46 ms 5928 KB Output is correct
5 Correct 148 ms 22284 KB Output is correct
6 Correct 146 ms 22412 KB Output is correct
7 Correct 154 ms 22348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 37 ms 6028 KB Output is correct
10 Correct 175 ms 22352 KB Output is correct
11 Correct 147 ms 22356 KB Output is correct
12 Correct 154 ms 22352 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 36 ms 5884 KB Output is correct
17 Correct 159 ms 22340 KB Output is correct
18 Correct 151 ms 22360 KB Output is correct
19 Correct 145 ms 22256 KB Output is correct
20 Correct 148 ms 22304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 500 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 8 ms 1372 KB Output is correct
7 Correct 150 ms 22296 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 6 ms 1372 KB Output is correct
13 Correct 144 ms 22356 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 856 KB Output is correct
17 Correct 74 ms 12368 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 37 ms 6036 KB Output is correct
21 Correct 148 ms 22296 KB Output is correct
22 Correct 152 ms 22276 KB Output is correct
23 Correct 156 ms 22276 KB Output is correct
24 Correct 184 ms 22320 KB Output is correct
25 Correct 64 ms 12324 KB Output is correct
26 Correct 63 ms 12296 KB Output is correct
27 Correct 160 ms 22356 KB Output is correct
28 Correct 149 ms 22352 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 356 KB Output is correct
31 Correct 0 ms 344 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 9 ms 1368 KB Output is correct
37 Correct 148 ms 22276 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 7 ms 1372 KB Output is correct
41 Correct 148 ms 22296 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 4 ms 856 KB Output is correct
45 Correct 81 ms 12316 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 37 ms 5968 KB Output is correct
50 Correct 148 ms 22460 KB Output is correct
51 Correct 149 ms 22384 KB Output is correct
52 Correct 156 ms 22864 KB Output is correct
53 Correct 68 ms 12372 KB Output is correct
54 Correct 65 ms 12292 KB Output is correct
55 Correct 144 ms 22276 KB Output is correct
56 Correct 154 ms 22320 KB Output is correct
57 Correct 63 ms 12368 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 4 ms 860 KB Output is correct
61 Correct 82 ms 12300 KB Output is correct
62 Correct 0 ms 348 KB Output is correct
63 Correct 0 ms 344 KB Output is correct
64 Correct 0 ms 348 KB Output is correct
65 Correct 37 ms 6000 KB Output is correct
66 Correct 65 ms 12368 KB Output is correct
67 Correct 65 ms 12312 KB Output is correct
68 Correct 76 ms 12308 KB Output is correct
69 Correct 62 ms 12308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 500 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 8 ms 1372 KB Output is correct
7 Correct 150 ms 22296 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 6 ms 1372 KB Output is correct
13 Correct 144 ms 22356 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 856 KB Output is correct
17 Correct 74 ms 12368 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 37 ms 6036 KB Output is correct
21 Correct 148 ms 22296 KB Output is correct
22 Correct 152 ms 22276 KB Output is correct
23 Correct 156 ms 22276 KB Output is correct
24 Correct 184 ms 22320 KB Output is correct
25 Correct 64 ms 12324 KB Output is correct
26 Correct 63 ms 12296 KB Output is correct
27 Correct 160 ms 22356 KB Output is correct
28 Correct 149 ms 22352 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 356 KB Output is correct
31 Correct 0 ms 344 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 9 ms 1368 KB Output is correct
37 Correct 148 ms 22276 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 7 ms 1372 KB Output is correct
41 Correct 148 ms 22296 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 4 ms 856 KB Output is correct
45 Correct 81 ms 12316 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 37 ms 5968 KB Output is correct
50 Correct 148 ms 22460 KB Output is correct
51 Correct 149 ms 22384 KB Output is correct
52 Correct 156 ms 22864 KB Output is correct
53 Correct 68 ms 12372 KB Output is correct
54 Correct 65 ms 12292 KB Output is correct
55 Correct 144 ms 22276 KB Output is correct
56 Correct 154 ms 22320 KB Output is correct
57 Correct 63 ms 12368 KB Output is correct
58 Correct 1 ms 344 KB Output is correct
59 Correct 1 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 46 ms 5928 KB Output is correct
62 Correct 148 ms 22284 KB Output is correct
63 Correct 146 ms 22412 KB Output is correct
64 Correct 154 ms 22348 KB Output is correct
65 Correct 0 ms 344 KB Output is correct
66 Correct 37 ms 6028 KB Output is correct
67 Correct 175 ms 22352 KB Output is correct
68 Correct 147 ms 22356 KB Output is correct
69 Correct 154 ms 22352 KB Output is correct
70 Correct 0 ms 348 KB Output is correct
71 Correct 0 ms 348 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 36 ms 5884 KB Output is correct
74 Correct 159 ms 22340 KB Output is correct
75 Correct 151 ms 22360 KB Output is correct
76 Correct 145 ms 22256 KB Output is correct
77 Correct 148 ms 22304 KB Output is correct
78 Correct 0 ms 348 KB Output is correct
79 Correct 0 ms 348 KB Output is correct
80 Correct 4 ms 860 KB Output is correct
81 Correct 82 ms 12300 KB Output is correct
82 Correct 0 ms 348 KB Output is correct
83 Correct 0 ms 344 KB Output is correct
84 Correct 0 ms 348 KB Output is correct
85 Correct 37 ms 6000 KB Output is correct
86 Correct 65 ms 12368 KB Output is correct
87 Correct 65 ms 12312 KB Output is correct
88 Correct 76 ms 12308 KB Output is correct
89 Correct 62 ms 12308 KB Output is correct
90 Correct 0 ms 348 KB Output is correct
91 Correct 0 ms 348 KB Output is correct
92 Correct 3 ms 860 KB Output is correct
93 Correct 77 ms 12512 KB Output is correct
94 Correct 1 ms 344 KB Output is correct
95 Correct 1 ms 348 KB Output is correct
96 Correct 1 ms 348 KB Output is correct
97 Correct 16 ms 3480 KB Output is correct
98 Correct 78 ms 12328 KB Output is correct
99 Correct 66 ms 12372 KB Output is correct
100 Correct 63 ms 12372 KB Output is correct
101 Correct 62 ms 12372 KB Output is correct
102 Incorrect 156 ms 22280 KB Answer gives possible 1 while actual possible 0
103 Halted 0 ms 0 KB -