Submission #948011

# Submission time Handle Problem Language Result Execution time Memory
948011 2024-03-17T12:32:12 Z 4QT0R Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
193 ms 24404 KB
#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
#define vi vector<int>
#define vvi vector<vi>
#define loop(a,b,c) for(int a = b; a<(int)c; a++)

struct DSU{
	vi leader;
	vi siz;

	void init(int n){
		leader.resize(n+1);
		siz.resize(n+1);
		for (int i = 0; i<n; i++){
			leader[i]=i;
			siz[i]=1;
		}
	}
	
	int Find(int v){
		if (leader[v]==v)return v;
		leader[v]=Find(leader[v]);
		return leader[v];
	}

	bool same_set(int a, int b){
		return Find(a)==Find(b);
	}

	void Union(int a, int b){
		a=Find(a);
		b=Find(b);
		if (a==b)return;
		if (siz[a]<siz[b])swap(a,b);
		leader[b]=a;
		siz[a]+=siz[b];
	}
};

bool vis_branch[1502];
bool vis_comp[1502];
int nr_branch[1502];
int nr_comp[1502];

int construct(vvi p){
	int n=p.size();

	DSU compound,branch;
	compound.init(n);
	branch.init(n);
	vvi wyn;
	wyn.resize(n);
	loop(i,0,n)wyn[i].resize(n);
	loop(i,0,n){
		loop(j,0,n){
			if (p[i][j]){
				if (p[i][j]==3)return 0;
				compound.Union(i,j);
				if (p[i][j]==1)branch.Union(i,j);
			}
		}
	}
	loop(i,0,n){
		loop(j,i+1,n){
			if (p[i][j]==0 && compound.same_set(i,j))return 0;
			if (p[i][j]==2 && branch.same_set(i,j))return 0;
		}
	}
	vvi single;
	vvi cycle;
	int iter=0,iter2=0;
	loop(i,0,n){
		if (!vis_branch[branch.Find(i)]){
			if (!vis_comp[compound.Find(i)]){
				vis_comp[compound.Find(i)]=true;
				cycle.push_back({i});
				nr_comp[compound.Find(i)]=iter2++;
			}
			else cycle[nr_comp[compound.Find(i)]].push_back(i);
			vis_branch[branch.Find(i)]=true;
			single.push_back({i});
			nr_branch[branch.Find(i)]=iter++;
		}
		else single[nr_branch[branch.Find(i)]].push_back(i);
	}
	loop(i,0,iter){
		loop(j,1,single[i].size()){
			wyn[single[i][0]][single[i][j]]=wyn[single[i][j]][single[i][0]]=1;
		}
	}
	loop(i,0,iter2){
		if (cycle[i].size()==1)continue;
		else if (cycle[i].size()==2)return 0;
		wyn[cycle[i][0]][cycle[i].back()]=wyn[cycle[i].back()][cycle[i][0]]=1;
		loop(j,1,cycle[i].size()){
			wyn[cycle[i][j-1]][cycle[i][j]]=wyn[cycle[i][j]][cycle[i][j-1]]=1;
		}
	}
	build(wyn);
	return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 600 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 1 ms 348 KB Output is correct
6 Correct 6 ms 1404 KB Output is correct
7 Correct 148 ms 24032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 600 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 1 ms 348 KB Output is correct
6 Correct 6 ms 1404 KB Output is correct
7 Correct 148 ms 24032 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 6 ms 1400 KB Output is correct
13 Correct 144 ms 24240 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 436 KB Output is correct
16 Correct 3 ms 856 KB Output is correct
17 Correct 69 ms 13996 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 36 ms 6236 KB Output is correct
21 Correct 147 ms 24148 KB Output is correct
22 Correct 145 ms 24148 KB Output is correct
23 Correct 152 ms 23932 KB Output is correct
24 Correct 162 ms 24084 KB Output is correct
25 Correct 61 ms 14164 KB Output is correct
26 Correct 57 ms 13996 KB Output is correct
27 Correct 152 ms 24076 KB Output is correct
28 Correct 145 ms 24196 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 444 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 6 ms 1372 KB Output is correct
9 Correct 146 ms 24148 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 159 ms 24224 KB Output is correct
14 Correct 0 ms 600 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 3 ms 856 KB Output is correct
17 Correct 69 ms 14076 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 38 ms 6404 KB Output is correct
22 Correct 148 ms 24204 KB Output is correct
23 Correct 144 ms 24236 KB Output is correct
24 Correct 150 ms 24240 KB Output is correct
25 Correct 65 ms 14160 KB Output is correct
26 Correct 61 ms 14244 KB Output is correct
27 Correct 148 ms 24144 KB Output is correct
28 Correct 149 ms 24148 KB Output is correct
29 Correct 70 ms 14204 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 444 KB Output is correct
4 Correct 36 ms 6408 KB Output is correct
5 Correct 193 ms 24056 KB Output is correct
6 Correct 178 ms 24144 KB Output is correct
7 Correct 155 ms 23932 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 37 ms 6312 KB Output is correct
10 Correct 147 ms 24148 KB Output is correct
11 Correct 147 ms 24144 KB Output is correct
12 Correct 151 ms 24188 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 37 ms 6452 KB Output is correct
17 Correct 152 ms 24100 KB Output is correct
18 Correct 150 ms 24060 KB Output is correct
19 Correct 148 ms 24404 KB Output is correct
20 Correct 145 ms 24148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 600 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 1 ms 348 KB Output is correct
6 Correct 6 ms 1404 KB Output is correct
7 Correct 148 ms 24032 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 6 ms 1400 KB Output is correct
13 Correct 144 ms 24240 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 436 KB Output is correct
16 Correct 3 ms 856 KB Output is correct
17 Correct 69 ms 13996 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 36 ms 6236 KB Output is correct
21 Correct 147 ms 24148 KB Output is correct
22 Correct 145 ms 24148 KB Output is correct
23 Correct 152 ms 23932 KB Output is correct
24 Correct 162 ms 24084 KB Output is correct
25 Correct 61 ms 14164 KB Output is correct
26 Correct 57 ms 13996 KB Output is correct
27 Correct 152 ms 24076 KB Output is correct
28 Correct 145 ms 24196 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 444 KB Output is correct
34 Correct 0 ms 344 KB Output is correct
35 Correct 0 ms 344 KB Output is correct
36 Correct 6 ms 1372 KB Output is correct
37 Correct 146 ms 24148 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 6 ms 1372 KB Output is correct
41 Correct 159 ms 24224 KB Output is correct
42 Correct 0 ms 600 KB Output is correct
43 Correct 0 ms 344 KB Output is correct
44 Correct 3 ms 856 KB Output is correct
45 Correct 69 ms 14076 KB Output is correct
46 Correct 0 ms 344 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 38 ms 6404 KB Output is correct
50 Correct 148 ms 24204 KB Output is correct
51 Correct 144 ms 24236 KB Output is correct
52 Correct 150 ms 24240 KB Output is correct
53 Correct 65 ms 14160 KB Output is correct
54 Correct 61 ms 14244 KB Output is correct
55 Correct 148 ms 24144 KB Output is correct
56 Correct 149 ms 24148 KB Output is correct
57 Correct 70 ms 14204 KB Output is correct
58 Correct 0 ms 344 KB Output is correct
59 Correct 1 ms 348 KB Output is correct
60 Correct 3 ms 860 KB Output is correct
61 Correct 77 ms 14072 KB Output is correct
62 Correct 0 ms 348 KB Output is correct
63 Correct 0 ms 348 KB Output is correct
64 Correct 0 ms 348 KB Output is correct
65 Correct 36 ms 6316 KB Output is correct
66 Correct 61 ms 14200 KB Output is correct
67 Correct 61 ms 14004 KB Output is correct
68 Correct 62 ms 14204 KB Output is correct
69 Correct 60 ms 14160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 600 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 1 ms 348 KB Output is correct
6 Correct 6 ms 1404 KB Output is correct
7 Correct 148 ms 24032 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 6 ms 1400 KB Output is correct
13 Correct 144 ms 24240 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 436 KB Output is correct
16 Correct 3 ms 856 KB Output is correct
17 Correct 69 ms 13996 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 36 ms 6236 KB Output is correct
21 Correct 147 ms 24148 KB Output is correct
22 Correct 145 ms 24148 KB Output is correct
23 Correct 152 ms 23932 KB Output is correct
24 Correct 162 ms 24084 KB Output is correct
25 Correct 61 ms 14164 KB Output is correct
26 Correct 57 ms 13996 KB Output is correct
27 Correct 152 ms 24076 KB Output is correct
28 Correct 145 ms 24196 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 444 KB Output is correct
34 Correct 0 ms 344 KB Output is correct
35 Correct 0 ms 344 KB Output is correct
36 Correct 6 ms 1372 KB Output is correct
37 Correct 146 ms 24148 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 6 ms 1372 KB Output is correct
41 Correct 159 ms 24224 KB Output is correct
42 Correct 0 ms 600 KB Output is correct
43 Correct 0 ms 344 KB Output is correct
44 Correct 3 ms 856 KB Output is correct
45 Correct 69 ms 14076 KB Output is correct
46 Correct 0 ms 344 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 38 ms 6404 KB Output is correct
50 Correct 148 ms 24204 KB Output is correct
51 Correct 144 ms 24236 KB Output is correct
52 Correct 150 ms 24240 KB Output is correct
53 Correct 65 ms 14160 KB Output is correct
54 Correct 61 ms 14244 KB Output is correct
55 Correct 148 ms 24144 KB Output is correct
56 Correct 149 ms 24148 KB Output is correct
57 Correct 70 ms 14204 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 444 KB Output is correct
61 Correct 36 ms 6408 KB Output is correct
62 Correct 193 ms 24056 KB Output is correct
63 Correct 178 ms 24144 KB Output is correct
64 Correct 155 ms 23932 KB Output is correct
65 Correct 1 ms 348 KB Output is correct
66 Correct 37 ms 6312 KB Output is correct
67 Correct 147 ms 24148 KB Output is correct
68 Correct 147 ms 24144 KB Output is correct
69 Correct 151 ms 24188 KB Output is correct
70 Correct 1 ms 344 KB Output is correct
71 Correct 1 ms 348 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 37 ms 6452 KB Output is correct
74 Correct 152 ms 24100 KB Output is correct
75 Correct 150 ms 24060 KB Output is correct
76 Correct 148 ms 24404 KB Output is correct
77 Correct 145 ms 24148 KB Output is correct
78 Correct 0 ms 344 KB Output is correct
79 Correct 1 ms 348 KB Output is correct
80 Correct 3 ms 860 KB Output is correct
81 Correct 77 ms 14072 KB Output is correct
82 Correct 0 ms 348 KB Output is correct
83 Correct 0 ms 348 KB Output is correct
84 Correct 0 ms 348 KB Output is correct
85 Correct 36 ms 6316 KB Output is correct
86 Correct 61 ms 14200 KB Output is correct
87 Correct 61 ms 14004 KB Output is correct
88 Correct 62 ms 14204 KB Output is correct
89 Correct 60 ms 14160 KB Output is correct
90 Correct 1 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 65 ms 14108 KB Output is correct
94 Correct 0 ms 348 KB Output is correct
95 Correct 0 ms 348 KB Output is correct
96 Correct 0 ms 348 KB Output is correct
97 Correct 14 ms 3676 KB Output is correct
98 Correct 59 ms 14216 KB Output is correct
99 Correct 74 ms 14164 KB Output is correct
100 Correct 72 ms 14204 KB Output is correct
101 Correct 65 ms 14416 KB Output is correct
102 Correct 58 ms 14164 KB Output is correct
103 Correct 57 ms 14084 KB Output is correct
104 Correct 58 ms 14212 KB Output is correct
105 Correct 65 ms 14164 KB Output is correct