Submission #400918

# Submission time Handle Problem Language Result Execution time Memory
400918 2021-05-08T20:17:03 Z shivensinha4 Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
274 ms 32124 KB
#include "bits/stdc++.h"
#include "supertrees.h"
using namespace std;
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef array<int, 2> ii;
// #define endl '\n'

bool valid = true;
vi singleComp;
vector<vi> answer, paths, doubleAdj, compEl; // da: -1: dunno, 0: nope, 1: yep
int n;

void dfs(int p, vi &comp, vector<vi> &adj) {
	compEl[comp[p]].push_back(p);
	for_(i, 0, n) if (p != i and adj[p][i] == 1) {
		if (comp[i] == -1) {
			comp[i] = comp[p];
			dfs(i, comp, adj);
		}
		
		if (comp[i] != comp[p]) valid = false;
	}
}

bool checkDouble(int a, int b) {
	for (auto x: compEl[a]) for (auto y: compEl[b]) if (paths[x][y] != 2) return false;
	
	return true;
}

int construct(vector<vi> p) {
	paths = p;
	n = paths.size();
	answer.resize(n, vi(n)); singleComp.resize(n, -1); doubleAdj.resize(n, vi(n, -1)); compEl.resize(n);
	
	for_(i, 0, n) if (singleComp[i] == -1) {
		singleComp[i] = i;
		dfs(i, singleComp, paths);
	}
	
	for_(i, 0, n) for_(j, i+1, n) if (paths[i][j] == 2) {
		if (singleComp[i] == singleComp[j]) {
			valid = false;
			continue;
		}
		
		int a = singleComp[i], b = singleComp[j];
		if (doubleAdj[a][b] == -1) {
			doubleAdj[a][b] = doubleAdj[b][a] = checkDouble(a, b);
			if (!doubleAdj[a][b]) valid = false;
		}
	}
		
	compEl.assign(n, {});
	vi doubleComp(n, -1);
	for_(i, 0, n) if (singleComp[i] == i and doubleComp[i] == -1) {
		doubleComp[i] = i;
		dfs(i, doubleComp, doubleAdj);
	}
	
	for_(i, 0, n) if (compEl[i].size() == 2) valid = false;
	
	for_(i, 0, n) for_(j, i+1, n) {
		if (singleComp[i] != singleComp[j] and (paths[i][j] == 2 or doubleComp[singleComp[i]] == doubleComp[singleComp[j]])) {
			valid = valid and paths[i][j] == 2 and doubleComp[singleComp[i]] == doubleComp[singleComp[j]];
		}
		
		if (paths[i][j] == 0) valid = valid and singleComp[i] != singleComp[j] and doubleComp[singleComp[i]] != doubleComp[singleComp[j]];
		else if (paths[i][j] == 1) valid = valid and singleComp[i] == singleComp[j];
		else if (paths[i][j] == 2) valid = valid and singleComp[i] != singleComp[j] and doubleComp[singleComp[i]] == doubleComp[singleComp[j]];
		else valid = false;
	}
	
	for_(i, 0, n) for_(j, i+1, n) if (singleComp[i] == i and singleComp[j] == j and doubleComp[i] == doubleComp[j]) valid = valid and checkDouble(i, j);
	
	
	if (!valid) return 0;
	
	for_(i, 0, n) if (singleComp[i] != i) answer[i][singleComp[i]] = answer[singleComp[i]][i] = 1;
	
	for_(i, 0, n) if (compEl[i].size() > 1) {
		int s = compEl[i].size();
		for_(j, 0, s) {
			int nxt = j == s-1 ? 0 : j+1;
			answer[compEl[i][j]][compEl[i][nxt]] = answer[compEl[i][nxt]][compEl[i][j]] = 1;
		}
	}
	
	build(answer);
	return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 296 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 11 ms 1524 KB Output is correct
7 Correct 255 ms 31992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 296 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 11 ms 1524 KB Output is correct
7 Correct 255 ms 31992 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 11 ms 1596 KB Output is correct
13 Correct 256 ms 31940 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 296 KB Output is correct
16 Correct 6 ms 1100 KB Output is correct
17 Correct 127 ms 21996 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 65 ms 8180 KB Output is correct
21 Correct 258 ms 31988 KB Output is correct
22 Correct 257 ms 31940 KB Output is correct
23 Correct 261 ms 31940 KB Output is correct
24 Correct 254 ms 32000 KB Output is correct
25 Correct 111 ms 22000 KB Output is correct
26 Correct 109 ms 21956 KB Output is correct
27 Correct 267 ms 31940 KB Output is correct
28 Correct 263 ms 31868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 11 ms 1568 KB Output is correct
9 Correct 252 ms 31864 KB Output is correct
10 Correct 1 ms 296 KB Output is correct
11 Correct 1 ms 292 KB Output is correct
12 Correct 12 ms 1612 KB Output is correct
13 Correct 267 ms 32016 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 6 ms 1196 KB Output is correct
17 Correct 130 ms 22136 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 292 KB Output is correct
21 Correct 67 ms 8196 KB Output is correct
22 Correct 259 ms 31940 KB Output is correct
23 Correct 253 ms 31940 KB Output is correct
24 Correct 274 ms 31976 KB Output is correct
25 Correct 116 ms 22012 KB Output is correct
26 Correct 117 ms 22064 KB Output is correct
27 Correct 253 ms 31972 KB Output is correct
28 Correct 268 ms 32072 KB Output is correct
29 Correct 111 ms 22000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 67 ms 8260 KB Output is correct
5 Correct 264 ms 31940 KB Output is correct
6 Correct 259 ms 31940 KB Output is correct
7 Correct 260 ms 31988 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 67 ms 8248 KB Output is correct
10 Correct 258 ms 31892 KB Output is correct
11 Correct 264 ms 32124 KB Output is correct
12 Correct 271 ms 31996 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 65 ms 8216 KB Output is correct
17 Correct 259 ms 31940 KB Output is correct
18 Correct 264 ms 31940 KB Output is correct
19 Correct 264 ms 31920 KB Output is correct
20 Correct 253 ms 31960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 296 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 11 ms 1524 KB Output is correct
7 Correct 255 ms 31992 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 11 ms 1596 KB Output is correct
13 Correct 256 ms 31940 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 296 KB Output is correct
16 Correct 6 ms 1100 KB Output is correct
17 Correct 127 ms 21996 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 65 ms 8180 KB Output is correct
21 Correct 258 ms 31988 KB Output is correct
22 Correct 257 ms 31940 KB Output is correct
23 Correct 261 ms 31940 KB Output is correct
24 Correct 254 ms 32000 KB Output is correct
25 Correct 111 ms 22000 KB Output is correct
26 Correct 109 ms 21956 KB Output is correct
27 Correct 267 ms 31940 KB Output is correct
28 Correct 263 ms 31868 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 11 ms 1568 KB Output is correct
37 Correct 252 ms 31864 KB Output is correct
38 Correct 1 ms 296 KB Output is correct
39 Correct 1 ms 292 KB Output is correct
40 Correct 12 ms 1612 KB Output is correct
41 Correct 267 ms 32016 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 6 ms 1196 KB Output is correct
45 Correct 130 ms 22136 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 292 KB Output is correct
49 Correct 67 ms 8196 KB Output is correct
50 Correct 259 ms 31940 KB Output is correct
51 Correct 253 ms 31940 KB Output is correct
52 Correct 274 ms 31976 KB Output is correct
53 Correct 116 ms 22012 KB Output is correct
54 Correct 117 ms 22064 KB Output is correct
55 Correct 253 ms 31972 KB Output is correct
56 Correct 268 ms 32072 KB Output is correct
57 Correct 111 ms 22000 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 6 ms 1100 KB Output is correct
61 Correct 125 ms 22016 KB Output is correct
62 Correct 1 ms 204 KB Output is correct
63 Correct 1 ms 296 KB Output is correct
64 Correct 1 ms 204 KB Output is correct
65 Correct 66 ms 8176 KB Output is correct
66 Correct 117 ms 22084 KB Output is correct
67 Correct 114 ms 22048 KB Output is correct
68 Correct 111 ms 22004 KB Output is correct
69 Correct 110 ms 22008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 296 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 11 ms 1524 KB Output is correct
7 Correct 255 ms 31992 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 11 ms 1596 KB Output is correct
13 Correct 256 ms 31940 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 296 KB Output is correct
16 Correct 6 ms 1100 KB Output is correct
17 Correct 127 ms 21996 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 65 ms 8180 KB Output is correct
21 Correct 258 ms 31988 KB Output is correct
22 Correct 257 ms 31940 KB Output is correct
23 Correct 261 ms 31940 KB Output is correct
24 Correct 254 ms 32000 KB Output is correct
25 Correct 111 ms 22000 KB Output is correct
26 Correct 109 ms 21956 KB Output is correct
27 Correct 267 ms 31940 KB Output is correct
28 Correct 263 ms 31868 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 11 ms 1568 KB Output is correct
37 Correct 252 ms 31864 KB Output is correct
38 Correct 1 ms 296 KB Output is correct
39 Correct 1 ms 292 KB Output is correct
40 Correct 12 ms 1612 KB Output is correct
41 Correct 267 ms 32016 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 6 ms 1196 KB Output is correct
45 Correct 130 ms 22136 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 292 KB Output is correct
49 Correct 67 ms 8196 KB Output is correct
50 Correct 259 ms 31940 KB Output is correct
51 Correct 253 ms 31940 KB Output is correct
52 Correct 274 ms 31976 KB Output is correct
53 Correct 116 ms 22012 KB Output is correct
54 Correct 117 ms 22064 KB Output is correct
55 Correct 253 ms 31972 KB Output is correct
56 Correct 268 ms 32072 KB Output is correct
57 Correct 111 ms 22000 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 67 ms 8260 KB Output is correct
62 Correct 264 ms 31940 KB Output is correct
63 Correct 259 ms 31940 KB Output is correct
64 Correct 260 ms 31988 KB Output is correct
65 Correct 1 ms 204 KB Output is correct
66 Correct 67 ms 8248 KB Output is correct
67 Correct 258 ms 31892 KB Output is correct
68 Correct 264 ms 32124 KB Output is correct
69 Correct 271 ms 31996 KB Output is correct
70 Correct 1 ms 204 KB Output is correct
71 Correct 1 ms 204 KB Output is correct
72 Correct 1 ms 204 KB Output is correct
73 Correct 65 ms 8216 KB Output is correct
74 Correct 259 ms 31940 KB Output is correct
75 Correct 264 ms 31940 KB Output is correct
76 Correct 264 ms 31920 KB Output is correct
77 Correct 253 ms 31960 KB Output is correct
78 Correct 1 ms 204 KB Output is correct
79 Correct 1 ms 204 KB Output is correct
80 Correct 6 ms 1100 KB Output is correct
81 Correct 125 ms 22016 KB Output is correct
82 Correct 1 ms 204 KB Output is correct
83 Correct 1 ms 296 KB Output is correct
84 Correct 1 ms 204 KB Output is correct
85 Correct 66 ms 8176 KB Output is correct
86 Correct 117 ms 22084 KB Output is correct
87 Correct 114 ms 22048 KB Output is correct
88 Correct 111 ms 22004 KB Output is correct
89 Correct 110 ms 22008 KB Output is correct
90 Correct 1 ms 208 KB Output is correct
91 Correct 1 ms 204 KB Output is correct
92 Correct 6 ms 1100 KB Output is correct
93 Correct 125 ms 22016 KB Output is correct
94 Correct 1 ms 204 KB Output is correct
95 Correct 1 ms 204 KB Output is correct
96 Correct 1 ms 208 KB Output is correct
97 Correct 30 ms 5744 KB Output is correct
98 Correct 114 ms 22012 KB Output is correct
99 Correct 114 ms 21996 KB Output is correct
100 Correct 115 ms 21996 KB Output is correct
101 Correct 112 ms 22012 KB Output is correct
102 Correct 111 ms 22012 KB Output is correct
103 Correct 125 ms 22084 KB Output is correct
104 Correct 118 ms 22084 KB Output is correct
105 Correct 121 ms 22012 KB Output is correct