Submission #428454

# Submission time Handle Problem Language Result Execution time Memory
428454 2021-06-15T11:58:02 Z alireza_kaviani Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
297 ms 30116 KB
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1010;
int n , par[MAXN] , sz[MAXN] , mark[MAXN] , adj[MAXN][MAXN] , ans[MAXN][MAXN];
vector<int> vec , comp[MAXN];

int Find(int v){
	return (par[v] == -1 ? v : par[v] = Find(par[v]));
}

void Union(int v , int u){
	v = Find(v); u = Find(u);
	if(v == u)	return;
	if(sz[v] < sz[u])	swap(v , u);
	par[u] = v;
	sz[v] += sz[u];
}

void DFS(int v){
	mark[v] = 1; vec.push_back(v);
	for(int u = 0 ; u < n ; u++){
		if(adj[v][u] > 0 && mark[u] == 0)	DFS(u);
	}
}

int solve(vector<int> v){
	vector<int> vec;
	for(int i : v){
		if(Find(i) == i){
			vec.push_back(i);
		}
	}
	int m = vec.size();
	if(m == 2)	return 0;
	for(int i = 0 ; i < m ; i++){
		int v = vec[i] , u = vec[(i + 1) % m];
		if(v != u)	ans[v][u] = ans[u][v] = 1;
	}
	return 1;
}

int construct(vector<vector<int>> p) {
	fill(par , par + MAXN , -1);
	fill(sz , sz + MAXN , 1);
	n = p.size();
	vector<vector<int>> answer;
	for (int i = 0; i < n; i++) {
		vector<int> row(n , 0);
		answer.push_back(row);
		for(int j = 0 ; j < n ; j++){
			adj[i][j] = p[i][j];
			if(adj[i][j] == 1)	Union(i , j);
			if(adj[i][j] == 3)	return 0;
		}
	}
	for(int i = 0 ; i < n ; i++){
		for(int j = 0 ; j < n ; j++){
			if(Find(i) == Find(j) && adj[i][j] != 1)	return 0;
		}
		comp[Find(i)].push_back(i);
	}
	for(int i = 0 ; i < n ; i++){
		if(comp[i].size() == 0)	continue;
		for(int j = 0 ; j + 1 < comp[i].size() ; j++){
			if(comp[i][j] == i){
				swap(comp[i][j] , comp[i][comp[i].size() - 1]);
			}
		}
		for(int j = 0 ; j + 1 < comp[i].size() ; j++){
			int v = comp[i][j] , u = comp[i][j + 1];
			ans[v][u] = ans[u][v] = 1;
		}
	}
	for(int i = 0 ; i < n ; i++){
		if(!mark[i]){
			vec.clear();
			DFS(i);
			for(int j : vec){
				for(int k : vec){
					if(adj[j][k] == 0)	return 0;
				}
			}
			if(solve(vec) == 0)	return 0;
		}
	}
	for(int i = 0 ; i < n ; i++){
		for(int j = 0 ; j < n ; j++){
			answer[i][j] = ans[i][j];
		}
	}
	build(answer);
	return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:66:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |   for(int j = 0 ; j + 1 < comp[i].size() ; j++){
      |                   ~~~~~~^~~~~~~~~~~~~~~~
supertrees.cpp:71:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |   for(int j = 0 ; j + 1 < comp[i].size() ; j++){
      |                   ~~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 12 ms 2764 KB Output is correct
7 Correct 252 ms 29980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 12 ms 2764 KB Output is correct
7 Correct 252 ms 29980 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 11 ms 1996 KB Output is correct
13 Correct 262 ms 25956 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 6 ms 1612 KB Output is correct
17 Correct 113 ms 16108 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 63 ms 9632 KB Output is correct
21 Correct 251 ms 29924 KB Output is correct
22 Correct 267 ms 29420 KB Output is correct
23 Correct 264 ms 30116 KB Output is correct
24 Correct 237 ms 26308 KB Output is correct
25 Correct 115 ms 16028 KB Output is correct
26 Correct 97 ms 16028 KB Output is correct
27 Correct 262 ms 30020 KB Output is correct
28 Correct 234 ms 26256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 11 ms 1960 KB Output is correct
9 Correct 245 ms 26024 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 11 ms 2812 KB Output is correct
13 Correct 234 ms 29980 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 6 ms 1632 KB Output is correct
17 Correct 114 ms 16172 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 60 ms 9540 KB Output is correct
22 Correct 233 ms 29984 KB Output is correct
23 Correct 282 ms 29892 KB Output is correct
24 Correct 252 ms 29984 KB Output is correct
25 Correct 98 ms 16324 KB Output is correct
26 Correct 103 ms 16928 KB Output is correct
27 Correct 238 ms 29884 KB Output is correct
28 Correct 275 ms 29996 KB Output is correct
29 Correct 138 ms 16324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 60 ms 9592 KB Output is correct
5 Correct 297 ms 29920 KB Output is correct
6 Correct 261 ms 29472 KB Output is correct
7 Correct 276 ms 29940 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 63 ms 9656 KB Output is correct
10 Correct 252 ms 30052 KB Output is correct
11 Correct 244 ms 29856 KB Output is correct
12 Correct 257 ms 30020 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 72 ms 9600 KB Output is correct
17 Correct 242 ms 29984 KB Output is correct
18 Correct 241 ms 29744 KB Output is correct
19 Correct 244 ms 29884 KB Output is correct
20 Correct 233 ms 26296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 12 ms 2764 KB Output is correct
7 Correct 252 ms 29980 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 11 ms 1996 KB Output is correct
13 Correct 262 ms 25956 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 6 ms 1612 KB Output is correct
17 Correct 113 ms 16108 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 63 ms 9632 KB Output is correct
21 Correct 251 ms 29924 KB Output is correct
22 Correct 267 ms 29420 KB Output is correct
23 Correct 264 ms 30116 KB Output is correct
24 Correct 237 ms 26308 KB Output is correct
25 Correct 115 ms 16028 KB Output is correct
26 Correct 97 ms 16028 KB Output is correct
27 Correct 262 ms 30020 KB Output is correct
28 Correct 234 ms 26256 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 11 ms 1960 KB Output is correct
37 Correct 245 ms 26024 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 11 ms 2812 KB Output is correct
41 Correct 234 ms 29980 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 6 ms 1632 KB Output is correct
45 Correct 114 ms 16172 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 332 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 60 ms 9540 KB Output is correct
50 Correct 233 ms 29984 KB Output is correct
51 Correct 282 ms 29892 KB Output is correct
52 Correct 252 ms 29984 KB Output is correct
53 Correct 98 ms 16324 KB Output is correct
54 Correct 103 ms 16928 KB Output is correct
55 Correct 238 ms 29884 KB Output is correct
56 Correct 275 ms 29996 KB Output is correct
57 Correct 138 ms 16324 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 1 ms 332 KB Output is correct
60 Correct 5 ms 1612 KB Output is correct
61 Correct 112 ms 16032 KB Output is correct
62 Correct 1 ms 332 KB Output is correct
63 Correct 1 ms 332 KB Output is correct
64 Correct 1 ms 332 KB Output is correct
65 Correct 61 ms 9628 KB Output is correct
66 Correct 111 ms 16544 KB Output is correct
67 Correct 99 ms 16068 KB Output is correct
68 Correct 107 ms 18472 KB Output is correct
69 Correct 98 ms 16296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 12 ms 2764 KB Output is correct
7 Correct 252 ms 29980 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 11 ms 1996 KB Output is correct
13 Correct 262 ms 25956 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 6 ms 1612 KB Output is correct
17 Correct 113 ms 16108 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 63 ms 9632 KB Output is correct
21 Correct 251 ms 29924 KB Output is correct
22 Correct 267 ms 29420 KB Output is correct
23 Correct 264 ms 30116 KB Output is correct
24 Correct 237 ms 26308 KB Output is correct
25 Correct 115 ms 16028 KB Output is correct
26 Correct 97 ms 16028 KB Output is correct
27 Correct 262 ms 30020 KB Output is correct
28 Correct 234 ms 26256 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 11 ms 1960 KB Output is correct
37 Correct 245 ms 26024 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 11 ms 2812 KB Output is correct
41 Correct 234 ms 29980 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 6 ms 1632 KB Output is correct
45 Correct 114 ms 16172 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 332 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 60 ms 9540 KB Output is correct
50 Correct 233 ms 29984 KB Output is correct
51 Correct 282 ms 29892 KB Output is correct
52 Correct 252 ms 29984 KB Output is correct
53 Correct 98 ms 16324 KB Output is correct
54 Correct 103 ms 16928 KB Output is correct
55 Correct 238 ms 29884 KB Output is correct
56 Correct 275 ms 29996 KB Output is correct
57 Correct 138 ms 16324 KB Output is correct
58 Correct 1 ms 332 KB Output is correct
59 Correct 1 ms 332 KB Output is correct
60 Correct 1 ms 332 KB Output is correct
61 Correct 60 ms 9592 KB Output is correct
62 Correct 297 ms 29920 KB Output is correct
63 Correct 261 ms 29472 KB Output is correct
64 Correct 276 ms 29940 KB Output is correct
65 Correct 1 ms 332 KB Output is correct
66 Correct 63 ms 9656 KB Output is correct
67 Correct 252 ms 30052 KB Output is correct
68 Correct 244 ms 29856 KB Output is correct
69 Correct 257 ms 30020 KB Output is correct
70 Correct 1 ms 332 KB Output is correct
71 Correct 1 ms 332 KB Output is correct
72 Correct 1 ms 332 KB Output is correct
73 Correct 72 ms 9600 KB Output is correct
74 Correct 242 ms 29984 KB Output is correct
75 Correct 241 ms 29744 KB Output is correct
76 Correct 244 ms 29884 KB Output is correct
77 Correct 233 ms 26296 KB Output is correct
78 Correct 1 ms 332 KB Output is correct
79 Correct 1 ms 332 KB Output is correct
80 Correct 5 ms 1612 KB Output is correct
81 Correct 112 ms 16032 KB Output is correct
82 Correct 1 ms 332 KB Output is correct
83 Correct 1 ms 332 KB Output is correct
84 Correct 1 ms 332 KB Output is correct
85 Correct 61 ms 9628 KB Output is correct
86 Correct 111 ms 16544 KB Output is correct
87 Correct 99 ms 16068 KB Output is correct
88 Correct 107 ms 18472 KB Output is correct
89 Correct 98 ms 16296 KB Output is correct
90 Correct 1 ms 332 KB Output is correct
91 Correct 1 ms 332 KB Output is correct
92 Correct 4 ms 588 KB Output is correct
93 Correct 94 ms 8220 KB Output is correct
94 Correct 1 ms 332 KB Output is correct
95 Correct 1 ms 332 KB Output is correct
96 Correct 1 ms 332 KB Output is correct
97 Correct 25 ms 2500 KB Output is correct
98 Correct 92 ms 8608 KB Output is correct
99 Correct 97 ms 9376 KB Output is correct
100 Correct 91 ms 8748 KB Output is correct
101 Correct 93 ms 10800 KB Output is correct
102 Correct 90 ms 8244 KB Output is correct
103 Correct 92 ms 8132 KB Output is correct
104 Correct 90 ms 8112 KB Output is correct
105 Correct 97 ms 8220 KB Output is correct