Submission #1083827

# Submission time Handle Problem Language Result Execution time Memory
1083827 2024-09-04T08:35:31 Z rayan_bd Connecting Supertrees (IOI20_supertrees) C++17
40 / 100
173 ms 24400 KB
#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;

vector<int> par1(1001),sz1(1001,1),par2(1001),sz2(1001,1),par3(1001),sz3(1003,1);

int findPar1(int u){
	if(par1[u]==u) return u;
	return par1[u]=findPar1(par1[u]);
}

int findPar2(int u){
	if(par2[u]==u) return u;
	return par2[u]=findPar2(par2[u]);
}

int findPar3(int u){
	if(par3[u]==u) return u;
	return par3[u]=findPar3(par3[u]);
}

void unite1(int u,int v){
	int ulp=findPar1(u);
	int vlp=findPar1(v);
	if(ulp==vlp) return;
	if(sz1[ulp]>sz1[vlp]){
		par1[vlp]=ulp;
		sz1[ulp]+=sz1[vlp];
	}else{
		par1[ulp]=vlp;
		sz1[vlp]+=sz1[ulp];
	}
}

void unite2(int u,int v){
	int ulp=findPar2(u);
	int vlp=findPar2(v);
	if(ulp==vlp) return;
	if(sz2[ulp]>sz2[vlp]){
		par2[vlp]=ulp;
		sz2[ulp]+=sz2[vlp];
	}else{
		par2[ulp]=vlp;
		sz2[vlp]+=sz2[ulp];
	}
}

void unite3(int u,int v){
	int ulp=findPar3(u);
	int vlp=findPar3(v);
	if(ulp==vlp) return;
	if(sz3[ulp]>sz3[vlp]){
		par3[vlp]=ulp;
		sz3[ulp]+=sz3[vlp];
	}else{
		par3[ulp]=vlp;
		sz3[vlp]+=sz3[ulp];
	}
}

bool is_samee1(int u,int v){
	return findPar1(u)==findPar1(v);
}

bool is_samee2(int u,int v){
	return findPar2(u)==findPar2(v);
}


bool is_samee3(int u,int v){
	return findPar3(u)==findPar3(v);
}



int construct(vector<vector<int>> p) {
	int n = p.size();
	vector<vector<int>> adj(n,vector<int>(n,0));

	// subtask 4

	for(int i=0;i<n;++i) par1[i]=par2[i]=par3[i]=i;

	for(int i=0;i<n;++i){
		for(int j=i+1;j<n;++j){
			if(p[i][j]==2){
				unite2(i,j);
				unite3(i,j);
			}
		}
	}

	for(int i=0;i<n;++i){
		for(int j=i+1;j<n;++j){
			if(p[i][j]==1&&sz2[i]==1&&sz2[j]==1){
				unite1(i,j);
				unite3(i,j);
			}
		}
	}

	for(int i=0;i<n;++i){
		if(sz2[findPar2(i)]==2) return 0;
		for(int j=i+1;j<n;++j){
			if(p[i][j]==0&&is_samee3(i,j)) return 0;
		}
	}

	vector<int> last_2(n+1,-1),first(n+1,-1),last_1(n+1,-1);


	for(int i=0;i<n;++i){
		// connecting the 2 ways road
		int ulp1=findPar2(i);
		if(last_2[ulp1]!=-1&&sz2[ulp1]>2){
			adj[i][last_2[ulp1]]=1;
			adj[last_2[ulp1]][i]=1;
		}
		if(first[ulp1]==-1) first[ulp1]=i;
		last_2[ulp1]=i;

		ulp1=findPar1(i);
		if(last_1[ulp1]!=-1&&sz1[ulp1]>1){
			adj[i][last_1[ulp1]]=1;
			adj[last_1[ulp1]][i]=1;
		}
		last_1[ulp1]=i;
	}

	for(int i=0;i<n;++i){
		int ulp=findPar2(i);
		if(sz2[ulp]>1) adj[last_2[ulp]][first[ulp]]=adj[first[ulp]][last_2[ulp]]=1;
	}

	for(int i=0;i<n;++i){
		for(int j=i+1;j<n;++j){
			if(p[i][j]==1&&!is_samee3(i,j)){
				unite3(i,j);
				adj[i][j]=adj[j][i]=1;

			}
		}
	}
/*
	for(int i=0;i<n;++i){
		for(int j=i+1;j<n;++j){
			if(adj[i][j]>0){
				cout<<i<<" - "<<j<<'\n';
			}
		}
	}*/

	build(adj);
	
	return 1;
}


# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 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 1112 KB Output is correct
7 Correct 134 ms 22168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 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 1112 KB Output is correct
7 Correct 134 ms 22168 KB Output is correct
8 Correct 0 ms 600 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 5 ms 1116 KB Output is correct
13 Correct 117 ms 22100 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 860 KB Output is correct
17 Correct 61 ms 12076 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 37 ms 5892 KB Output is correct
21 Correct 119 ms 22100 KB Output is correct
22 Correct 116 ms 22100 KB Output is correct
23 Correct 126 ms 22100 KB Output is correct
24 Correct 123 ms 22096 KB Output is correct
25 Correct 52 ms 12080 KB Output is correct
26 Correct 50 ms 12112 KB Output is correct
27 Correct 129 ms 22096 KB Output is correct
28 Correct 121 ms 22100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 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 1 ms 600 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 6 ms 1368 KB Output is correct
9 Correct 119 ms 24400 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 5 ms 1236 KB Output is correct
13 Correct 122 ms 24092 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 860 KB Output is correct
17 Correct 69 ms 14112 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 29 ms 6304 KB Output is correct
22 Correct 139 ms 24176 KB Output is correct
23 Correct 127 ms 24148 KB Output is correct
24 Correct 164 ms 24096 KB Output is correct
25 Correct 55 ms 14164 KB Output is correct
26 Correct 60 ms 14108 KB Output is correct
27 Correct 119 ms 23976 KB Output is correct
28 Correct 173 ms 24148 KB Output is correct
29 Correct 59 ms 14164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 452 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 30 ms 5876 KB Output is correct
5 Correct 123 ms 22064 KB Output is correct
6 Correct 124 ms 22096 KB Output is correct
7 Correct 126 ms 22104 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 32 ms 5712 KB Output is correct
10 Correct 120 ms 22064 KB Output is correct
11 Correct 126 ms 22096 KB Output is correct
12 Correct 123 ms 22060 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Incorrect 0 ms 348 KB Too many ways to get from 0 to 4, should be 1 found no less than 2
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 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 1112 KB Output is correct
7 Correct 134 ms 22168 KB Output is correct
8 Correct 0 ms 600 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 5 ms 1116 KB Output is correct
13 Correct 117 ms 22100 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 860 KB Output is correct
17 Correct 61 ms 12076 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 37 ms 5892 KB Output is correct
21 Correct 119 ms 22100 KB Output is correct
22 Correct 116 ms 22100 KB Output is correct
23 Correct 126 ms 22100 KB Output is correct
24 Correct 123 ms 22096 KB Output is correct
25 Correct 52 ms 12080 KB Output is correct
26 Correct 50 ms 12112 KB Output is correct
27 Correct 129 ms 22096 KB Output is correct
28 Correct 121 ms 22100 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 0 ms 344 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 348 KB Output is correct
34 Correct 1 ms 600 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 6 ms 1368 KB Output is correct
37 Correct 119 ms 24400 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 5 ms 1236 KB Output is correct
41 Correct 122 ms 24092 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 3 ms 860 KB Output is correct
45 Correct 69 ms 14112 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 1 ms 468 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 29 ms 6304 KB Output is correct
50 Correct 139 ms 24176 KB Output is correct
51 Correct 127 ms 24148 KB Output is correct
52 Correct 164 ms 24096 KB Output is correct
53 Correct 55 ms 14164 KB Output is correct
54 Correct 60 ms 14108 KB Output is correct
55 Correct 119 ms 23976 KB Output is correct
56 Correct 173 ms 24148 KB Output is correct
57 Correct 59 ms 14164 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 3 ms 860 KB Output is correct
61 Correct 70 ms 14356 KB Output is correct
62 Correct 1 ms 344 KB Output is correct
63 Correct 1 ms 348 KB Output is correct
64 Correct 0 ms 348 KB Output is correct
65 Incorrect 30 ms 6228 KB Too many ways to get from 1 to 269, should be 1 found no less than 2
66 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 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 1112 KB Output is correct
7 Correct 134 ms 22168 KB Output is correct
8 Correct 0 ms 600 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 5 ms 1116 KB Output is correct
13 Correct 117 ms 22100 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 860 KB Output is correct
17 Correct 61 ms 12076 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 37 ms 5892 KB Output is correct
21 Correct 119 ms 22100 KB Output is correct
22 Correct 116 ms 22100 KB Output is correct
23 Correct 126 ms 22100 KB Output is correct
24 Correct 123 ms 22096 KB Output is correct
25 Correct 52 ms 12080 KB Output is correct
26 Correct 50 ms 12112 KB Output is correct
27 Correct 129 ms 22096 KB Output is correct
28 Correct 121 ms 22100 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 0 ms 344 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 348 KB Output is correct
34 Correct 1 ms 600 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 6 ms 1368 KB Output is correct
37 Correct 119 ms 24400 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 5 ms 1236 KB Output is correct
41 Correct 122 ms 24092 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 3 ms 860 KB Output is correct
45 Correct 69 ms 14112 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 1 ms 468 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 29 ms 6304 KB Output is correct
50 Correct 139 ms 24176 KB Output is correct
51 Correct 127 ms 24148 KB Output is correct
52 Correct 164 ms 24096 KB Output is correct
53 Correct 55 ms 14164 KB Output is correct
54 Correct 60 ms 14108 KB Output is correct
55 Correct 119 ms 23976 KB Output is correct
56 Correct 173 ms 24148 KB Output is correct
57 Correct 59 ms 14164 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 0 ms 452 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 30 ms 5876 KB Output is correct
62 Correct 123 ms 22064 KB Output is correct
63 Correct 124 ms 22096 KB Output is correct
64 Correct 126 ms 22104 KB Output is correct
65 Correct 0 ms 344 KB Output is correct
66 Correct 32 ms 5712 KB Output is correct
67 Correct 120 ms 22064 KB Output is correct
68 Correct 126 ms 22096 KB Output is correct
69 Correct 123 ms 22060 KB Output is correct
70 Correct 0 ms 344 KB Output is correct
71 Correct 0 ms 348 KB Output is correct
72 Incorrect 0 ms 348 KB Too many ways to get from 0 to 4, should be 1 found no less than 2
73 Halted 0 ms 0 KB -