Submission #1039751

# Submission time Handle Problem Language Result Execution time Memory
1039751 2024-07-31T08:28:23 Z amirhoseinfar1385 Connecting Supertrees (IOI20_supertrees) C++17
96 / 100
156 ms 24400 KB
#include "supertrees.h"
#include<bits/stdc++.h>
using namespace std;
const int maxn=1000+10;
int n,vis[maxn];
vector<vector<int>>res;

struct dsu{
	int par[maxn];
	vector<int>adj[maxn];
	dsu(){
		for(int i=0;i<maxn;i++){
			par[i]=i;
		}
	}
	int root(int u){
		if(u==par[u]){
			return u;
		}
		return par[u]=root(par[u]);
	}
	void con(int u,int v){
		u=root(u),v=root(v);
		if(u!=v){
			par[u]=v;
			adj[v].push_back(u);
		}
	}
	vector<int>ret;
	void dfs(int u,int par=-1){
		ret.push_back(u);
		for(auto x:adj[u]){
			if(x==par){
				continue;
			}
			dfs(x,u);
		}
	}
	vector<int>allc(int u){
		ret.clear();
		u=root(u);
		dfs(u);
		return ret;
	}
}ds1,ds2;

void adde(int u,int v){
	res[u][v]=res[v][u]=1;
}

void addp(vector<int>a){
	for(int i=1;i<(int)a.size();i++){
		adde(a[i-1],a[i]);
	}
}

int construct(std::vector<std::vector<int>> p) {
	n = p.size();
	res.resize(n,vector<int>(n));
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(i==j){
				continue;
			}
			if(p[i][j]==1){
				ds1.con(i,j);
			}
		}
	}
	for(int i=0;i<n;i++){
		if(vis[i]==0){
			vector<int>hey=ds1.allc(i);
			for(auto x:hey){
				vis[x]=1;
			}
			addp(hey);
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(p[i][j]==2){
				ds2.con(ds1.root(i),ds1.root(j));
			}
		}
		vis[i]=0;
	}
	int retrr=1;
	for(int i=0;i<n;i++){
		if(vis[i]==0){
			vector<int>hey=ds2.allc(i);
			for(auto x:hey){
				vis[x]=1;
			}
			addp(hey);
			if((int)hey.size()>2){
				adde(hey[0],hey.back());
			}
			if((int)hey.size()==2){
				retrr=-1;
			}
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(i==j){
				continue;
			}
			if(p[i][j]==1&&ds1.root(i)!=ds1.root(j)){
				retrr=-1;
			}
			if(p[i][j]==0&&(ds1.root(i)==ds1.root(j)||ds2.root(ds1.root(i))==ds2.root(ds1.root(j)))){
				retrr=-1;
			}
			if(p[i][j]==2&&(ds2.root(ds1.root(i))!=ds2.root(ds1.root(j))||ds1.root(i)==ds1.root(j))){
				retrr=-1;
			}
		}
	}
	if(retrr==-1){
		return 0;
	}
	build(res);
	return retrr;
}
# 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 348 KB Output is correct
6 Correct 5 ms 1368 KB Output is correct
7 Correct 121 ms 24144 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 348 KB Output is correct
6 Correct 5 ms 1368 KB Output is correct
7 Correct 121 ms 24144 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 496 KB Output is correct
12 Correct 7 ms 1368 KB Output is correct
13 Correct 132 ms 24176 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 76 ms 14276 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 30 ms 6408 KB Output is correct
21 Correct 130 ms 24148 KB Output is correct
22 Correct 122 ms 24120 KB Output is correct
23 Correct 132 ms 24144 KB Output is correct
24 Correct 122 ms 24148 KB Output is correct
25 Correct 59 ms 14160 KB Output is correct
26 Correct 56 ms 14164 KB Output is correct
27 Correct 132 ms 24048 KB Output is correct
28 Correct 143 ms 24044 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 1 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 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 5 ms 1436 KB Output is correct
9 Correct 118 ms 24012 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 1372 KB Output is correct
13 Correct 130 ms 24116 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 860 KB Output is correct
17 Correct 74 ms 14128 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 32 ms 6392 KB Output is correct
22 Correct 121 ms 24016 KB Output is correct
23 Correct 117 ms 24112 KB Output is correct
24 Correct 132 ms 24068 KB Output is correct
25 Correct 53 ms 14276 KB Output is correct
26 Correct 55 ms 14160 KB Output is correct
27 Correct 118 ms 24108 KB Output is correct
28 Correct 121 ms 24144 KB Output is correct
29 Correct 52 ms 14272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 492 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 32 ms 6328 KB Output is correct
5 Correct 126 ms 24148 KB Output is correct
6 Correct 114 ms 24116 KB Output is correct
7 Correct 134 ms 24400 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 30 ms 6400 KB Output is correct
10 Correct 137 ms 23992 KB Output is correct
11 Correct 156 ms 24148 KB Output is correct
12 Correct 130 ms 24244 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 30 ms 6340 KB Output is correct
17 Correct 129 ms 24004 KB Output is correct
18 Correct 123 ms 24148 KB Output is correct
19 Correct 121 ms 24116 KB Output is correct
20 Correct 122 ms 24068 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 348 KB Output is correct
6 Correct 5 ms 1368 KB Output is correct
7 Correct 121 ms 24144 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 496 KB Output is correct
12 Correct 7 ms 1368 KB Output is correct
13 Correct 132 ms 24176 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 76 ms 14276 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 30 ms 6408 KB Output is correct
21 Correct 130 ms 24148 KB Output is correct
22 Correct 122 ms 24120 KB Output is correct
23 Correct 132 ms 24144 KB Output is correct
24 Correct 122 ms 24148 KB Output is correct
25 Correct 59 ms 14160 KB Output is correct
26 Correct 56 ms 14164 KB Output is correct
27 Correct 132 ms 24048 KB Output is correct
28 Correct 143 ms 24044 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 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 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 5 ms 1436 KB Output is correct
37 Correct 118 ms 24012 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 1372 KB Output is correct
41 Correct 130 ms 24116 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 3 ms 860 KB Output is correct
45 Correct 74 ms 14128 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 344 KB Output is correct
49 Correct 32 ms 6392 KB Output is correct
50 Correct 121 ms 24016 KB Output is correct
51 Correct 117 ms 24112 KB Output is correct
52 Correct 132 ms 24068 KB Output is correct
53 Correct 53 ms 14276 KB Output is correct
54 Correct 55 ms 14160 KB Output is correct
55 Correct 118 ms 24108 KB Output is correct
56 Correct 121 ms 24144 KB Output is correct
57 Correct 52 ms 14272 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 73 ms 14164 KB Output is correct
62 Correct 0 ms 344 KB Output is correct
63 Correct 0 ms 348 KB Output is correct
64 Correct 0 ms 604 KB Output is correct
65 Correct 30 ms 6236 KB Output is correct
66 Correct 55 ms 14160 KB Output is correct
67 Correct 58 ms 14276 KB Output is correct
68 Correct 56 ms 14272 KB Output is correct
69 Correct 56 ms 14160 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 348 KB Output is correct
6 Correct 5 ms 1368 KB Output is correct
7 Correct 121 ms 24144 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 496 KB Output is correct
12 Correct 7 ms 1368 KB Output is correct
13 Correct 132 ms 24176 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 76 ms 14276 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 30 ms 6408 KB Output is correct
21 Correct 130 ms 24148 KB Output is correct
22 Correct 122 ms 24120 KB Output is correct
23 Correct 132 ms 24144 KB Output is correct
24 Correct 122 ms 24148 KB Output is correct
25 Correct 59 ms 14160 KB Output is correct
26 Correct 56 ms 14164 KB Output is correct
27 Correct 132 ms 24048 KB Output is correct
28 Correct 143 ms 24044 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 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 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 5 ms 1436 KB Output is correct
37 Correct 118 ms 24012 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 1372 KB Output is correct
41 Correct 130 ms 24116 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 3 ms 860 KB Output is correct
45 Correct 74 ms 14128 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 344 KB Output is correct
49 Correct 32 ms 6392 KB Output is correct
50 Correct 121 ms 24016 KB Output is correct
51 Correct 117 ms 24112 KB Output is correct
52 Correct 132 ms 24068 KB Output is correct
53 Correct 53 ms 14276 KB Output is correct
54 Correct 55 ms 14160 KB Output is correct
55 Correct 118 ms 24108 KB Output is correct
56 Correct 121 ms 24144 KB Output is correct
57 Correct 52 ms 14272 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 492 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 32 ms 6328 KB Output is correct
62 Correct 126 ms 24148 KB Output is correct
63 Correct 114 ms 24116 KB Output is correct
64 Correct 134 ms 24400 KB Output is correct
65 Correct 0 ms 348 KB Output is correct
66 Correct 30 ms 6400 KB Output is correct
67 Correct 137 ms 23992 KB Output is correct
68 Correct 156 ms 24148 KB Output is correct
69 Correct 130 ms 24244 KB Output is correct
70 Correct 1 ms 348 KB Output is correct
71 Correct 0 ms 348 KB Output is correct
72 Correct 1 ms 348 KB Output is correct
73 Correct 30 ms 6340 KB Output is correct
74 Correct 129 ms 24004 KB Output is correct
75 Correct 123 ms 24148 KB Output is correct
76 Correct 121 ms 24116 KB Output is correct
77 Correct 122 ms 24068 KB Output is correct
78 Correct 0 ms 348 KB Output is correct
79 Correct 0 ms 348 KB Output is correct
80 Correct 3 ms 860 KB Output is correct
81 Correct 73 ms 14164 KB Output is correct
82 Correct 0 ms 344 KB Output is correct
83 Correct 0 ms 348 KB Output is correct
84 Correct 0 ms 604 KB Output is correct
85 Correct 30 ms 6236 KB Output is correct
86 Correct 55 ms 14160 KB Output is correct
87 Correct 58 ms 14276 KB Output is correct
88 Correct 56 ms 14272 KB Output is correct
89 Correct 56 ms 14160 KB Output is correct
90 Correct 0 ms 344 KB Output is correct
91 Correct 0 ms 348 KB Output is correct
92 Correct 4 ms 860 KB Output is correct
93 Correct 88 ms 14276 KB Output is correct
94 Incorrect 1 ms 348 KB Answer gives possible 1 while actual possible 0
95 Halted 0 ms 0 KB -