Submission #744735

# Submission time Handle Problem Language Result Execution time Memory
744735 2023-05-19T04:27:05 Z josanneo22 Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
185 ms 26180 KB
#include <bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
#include<algorithm>
using namespace std;

#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second 

#include "supertrees.h"
#include <vector>
void build(vector<vector<int>> b);
const int N = 1005;
int n, mx, vis[N];
vector<int> block, edge, circle;
vector<vector<int>> path, ans;
void st(int u, int v) {
	ans[u][v] = 1; ans[v][u] = 1;
}
void dfs(int u) {
	vis[u] = 1;
	block.push_back(u);
	for (int v = 0; v < n; v++) {
		mx = max(mx, path[u][v]);
		if (!vis[v] && path[u][v]) dfs(v);
	}
}
void find_branches(int u) {
	vis[u] = 2;
	edge.push_back(u);
	for (int v = 0; v < n; v++) {
		if (vis[v] == 1 && path[u][v] == 1) find_branches(v);
	}
}
/*
	solution explaination:
		observation 1: we can nveer construct if some path[i][j] is 3
		observation 2: if path[i][j] is 0: nothing happens
					   if path[i][j] is 1: meaning that is connected once
					   if path[i][j] is 2: meaning that there must be a cycle
		observation 3: the graph is only a cycle with branches going out of it
					   if consists of two cycles, we might get graph[i][j]>=4
					   and inside the cycle, all graph[i][j] must be 1
*/
int construct(std::vector<std::vector<int>> p) {
	path = p; n = p.size(); ans.resize(n);
	for (int i = 0; i < n; i++) ans[i].resize(n);
	//run each connected blocks check if there is a need to add cycle
	for (int i = 0; i < n; i++) {
		if (vis[i]) continue;//if visisted
		mx = 1; block.clear(); dfs(i);
		//record maximum such that there is no path[i][j]>=3
		//clear block and dfs
		if (mx == 3) return 0;
		//now my blocks should have all connected
		int sz = block.size();
		for (int f = 1; f < sz; f++) {
			for (int k = 0; k < f; k++) {
				if (!path[block[f]][block[k]]) return 0;//all path[i][j] be at least>=1
			}
		}
		//if max is just 1, just make a star
		if (mx == 1) {
			for(int j=1;j<sz;j++)st(block[0], block[j]);
		}
		else {
			circle.clear();
			for (int j = 0; j < sz; j++) {
				int k = block[j];//current index
				if (vis[k] != 1) continue;
				edge.clear();
				find_branches(k);//find all branches from current index
				for (int a = 1; a < edge.size(); a++) {
					for (int b = 0; b < a; b++) {
						if (path[edge[a]][edge[b]] != 1) return 0;
					}
					st(edge[0], edge[a]);//make star from edge[0] to all branches
				}
				circle.push_back(edge[0]);//edge[0] is inside circle now
			}
			//note that cycle size must be >=3
			if (circle.size() <= 2) return 0;
			for (int j = 0; j < circle.size(); j++) st(circle[j], circle[(j + 1) % circle.size()]);
		}
	}
	build(ans);
	return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:76:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |     for (int a = 1; a < edge.size(); a++) {
      |                     ~~^~~~~~~~~~~~~
supertrees.cpp:86:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |    for (int j = 0; j < circle.size(); j++) st(circle[j], circle[(j + 1) % circle.size()]);
      |                    ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 8 ms 1364 KB Output is correct
7 Correct 168 ms 26004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 8 ms 1364 KB Output is correct
7 Correct 168 ms 26004 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 7 ms 1364 KB Output is correct
13 Correct 166 ms 25900 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 852 KB Output is correct
17 Correct 80 ms 16140 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 43 ms 6748 KB Output is correct
21 Correct 168 ms 25944 KB Output is correct
22 Correct 167 ms 26004 KB Output is correct
23 Correct 179 ms 26012 KB Output is correct
24 Correct 172 ms 25932 KB Output is correct
25 Correct 75 ms 16016 KB Output is correct
26 Correct 70 ms 16008 KB Output is correct
27 Correct 177 ms 25992 KB Output is correct
28 Correct 168 ms 26180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 7 ms 1364 KB Output is correct
9 Correct 169 ms 25988 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 8 ms 1364 KB Output is correct
13 Correct 179 ms 25988 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 852 KB Output is correct
17 Correct 81 ms 16140 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 43 ms 6728 KB Output is correct
22 Correct 167 ms 25988 KB Output is correct
23 Correct 172 ms 25936 KB Output is correct
24 Correct 176 ms 25992 KB Output is correct
25 Correct 70 ms 16020 KB Output is correct
26 Correct 72 ms 16004 KB Output is correct
27 Correct 172 ms 25988 KB Output is correct
28 Correct 185 ms 26060 KB Output is correct
29 Correct 72 ms 16088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 43 ms 6744 KB Output is correct
5 Correct 183 ms 25988 KB Output is correct
6 Correct 169 ms 25940 KB Output is correct
7 Correct 177 ms 25988 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 49 ms 6668 KB Output is correct
10 Correct 169 ms 25988 KB Output is correct
11 Correct 164 ms 25996 KB Output is correct
12 Correct 173 ms 25988 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 44 ms 6704 KB Output is correct
17 Correct 169 ms 25992 KB Output is correct
18 Correct 172 ms 25996 KB Output is correct
19 Correct 171 ms 25892 KB Output is correct
20 Correct 169 ms 25996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 8 ms 1364 KB Output is correct
7 Correct 168 ms 26004 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 7 ms 1364 KB Output is correct
13 Correct 166 ms 25900 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 852 KB Output is correct
17 Correct 80 ms 16140 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 43 ms 6748 KB Output is correct
21 Correct 168 ms 25944 KB Output is correct
22 Correct 167 ms 26004 KB Output is correct
23 Correct 179 ms 26012 KB Output is correct
24 Correct 172 ms 25932 KB Output is correct
25 Correct 75 ms 16016 KB Output is correct
26 Correct 70 ms 16008 KB Output is correct
27 Correct 177 ms 25992 KB Output is correct
28 Correct 168 ms 26180 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 7 ms 1364 KB Output is correct
37 Correct 169 ms 25988 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 8 ms 1364 KB Output is correct
41 Correct 179 ms 25988 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 4 ms 852 KB Output is correct
45 Correct 81 ms 16140 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 43 ms 6728 KB Output is correct
50 Correct 167 ms 25988 KB Output is correct
51 Correct 172 ms 25936 KB Output is correct
52 Correct 176 ms 25992 KB Output is correct
53 Correct 70 ms 16020 KB Output is correct
54 Correct 72 ms 16004 KB Output is correct
55 Correct 172 ms 25988 KB Output is correct
56 Correct 185 ms 26060 KB Output is correct
57 Correct 72 ms 16088 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 3 ms 844 KB Output is correct
61 Correct 86 ms 16132 KB Output is correct
62 Correct 0 ms 212 KB Output is correct
63 Correct 0 ms 212 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Correct 43 ms 6664 KB Output is correct
66 Correct 70 ms 16088 KB Output is correct
67 Correct 76 ms 16004 KB Output is correct
68 Correct 72 ms 16016 KB Output is correct
69 Correct 66 ms 16076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 8 ms 1364 KB Output is correct
7 Correct 168 ms 26004 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 7 ms 1364 KB Output is correct
13 Correct 166 ms 25900 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 852 KB Output is correct
17 Correct 80 ms 16140 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 43 ms 6748 KB Output is correct
21 Correct 168 ms 25944 KB Output is correct
22 Correct 167 ms 26004 KB Output is correct
23 Correct 179 ms 26012 KB Output is correct
24 Correct 172 ms 25932 KB Output is correct
25 Correct 75 ms 16016 KB Output is correct
26 Correct 70 ms 16008 KB Output is correct
27 Correct 177 ms 25992 KB Output is correct
28 Correct 168 ms 26180 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 7 ms 1364 KB Output is correct
37 Correct 169 ms 25988 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 8 ms 1364 KB Output is correct
41 Correct 179 ms 25988 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 4 ms 852 KB Output is correct
45 Correct 81 ms 16140 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 43 ms 6728 KB Output is correct
50 Correct 167 ms 25988 KB Output is correct
51 Correct 172 ms 25936 KB Output is correct
52 Correct 176 ms 25992 KB Output is correct
53 Correct 70 ms 16020 KB Output is correct
54 Correct 72 ms 16004 KB Output is correct
55 Correct 172 ms 25988 KB Output is correct
56 Correct 185 ms 26060 KB Output is correct
57 Correct 72 ms 16088 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 43 ms 6744 KB Output is correct
62 Correct 183 ms 25988 KB Output is correct
63 Correct 169 ms 25940 KB Output is correct
64 Correct 177 ms 25988 KB Output is correct
65 Correct 0 ms 212 KB Output is correct
66 Correct 49 ms 6668 KB Output is correct
67 Correct 169 ms 25988 KB Output is correct
68 Correct 164 ms 25996 KB Output is correct
69 Correct 173 ms 25988 KB Output is correct
70 Correct 0 ms 212 KB Output is correct
71 Correct 0 ms 212 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 44 ms 6704 KB Output is correct
74 Correct 169 ms 25992 KB Output is correct
75 Correct 172 ms 25996 KB Output is correct
76 Correct 171 ms 25892 KB Output is correct
77 Correct 169 ms 25996 KB Output is correct
78 Correct 0 ms 212 KB Output is correct
79 Correct 0 ms 212 KB Output is correct
80 Correct 3 ms 844 KB Output is correct
81 Correct 86 ms 16132 KB Output is correct
82 Correct 0 ms 212 KB Output is correct
83 Correct 0 ms 212 KB Output is correct
84 Correct 1 ms 212 KB Output is correct
85 Correct 43 ms 6664 KB Output is correct
86 Correct 70 ms 16088 KB Output is correct
87 Correct 76 ms 16004 KB Output is correct
88 Correct 72 ms 16016 KB Output is correct
89 Correct 66 ms 16076 KB Output is correct
90 Correct 1 ms 212 KB Output is correct
91 Correct 0 ms 212 KB Output is correct
92 Correct 4 ms 852 KB Output is correct
93 Correct 84 ms 16136 KB Output is correct
94 Correct 0 ms 212 KB Output is correct
95 Correct 1 ms 212 KB Output is correct
96 Correct 0 ms 212 KB Output is correct
97 Correct 19 ms 4236 KB Output is correct
98 Correct 73 ms 16004 KB Output is correct
99 Correct 76 ms 16008 KB Output is correct
100 Correct 74 ms 16092 KB Output is correct
101 Correct 80 ms 16016 KB Output is correct
102 Correct 76 ms 16076 KB Output is correct
103 Correct 75 ms 16152 KB Output is correct
104 Correct 76 ms 16076 KB Output is correct
105 Correct 77 ms 16140 KB Output is correct