Submission #841604

# Submission time Handle Problem Language Result Execution time Memory
841604 2023-09-01T18:22:34 Z qthang2k11 Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
170 ms 26412 KB
#include "supertrees.h"
#include <bits/stdc++.h>

using namespace std;

const int N = 1003;

vector<int> adj[2][N];

struct DisjointSetUnion {
  vector<int> con[N];
  int par[N];
  
  DisjointSetUnion() {
  	for (int i = 0; i < N; i++)
  	  par[i] = i, con[i] = {i};
  }
  
  inline int find(int x) {
    if (x != par[x]) 
      par[x] = find(par[x]);
    return par[x];
  }
  
  void join(int x, int y) {
    x = find(x); y = find(y);
    if (x == y) return;
    if (con[x].size() < con[y].size()) swap(x, y);
    par[y] = x;
    for (int i: con[y])
      con[x].emplace_back(i);
    con[y].clear();
    con[y].shrink_to_fit();
  }
} one, two, positive;

int construct(vector<vector<int>> p) {
	int n = p.size();
	for (int i = 0; i < n; i++) {
	  for (int j = 0; j < n; j++) {
	    if (p[i][j] != p[j][i] || p[i][j] == 3) return 0;
	    if (!p[i][j]) continue;
	    adj[p[i][j] - 1][i].emplace_back(j);
	    positive.join(i, j);
	  }
	}
	for (int i = 0; i < n; i++)
	  for (int j: adj[0][i])
	    one.join(i, j);
	for (int i = 0; i < n; i++)
	  for (int j: adj[1][i])
	    two.join(one.find(i), one.find(j));
	vector<vector<int>> ans(n, vector<int>(n, 0));
	auto add_edge = [&] (int x, int y) -> void {
	  ans[x][y] = ans[y][x] = 1;
	};
	for (int i = 0; i < n; i++) {
	  if (positive.find(i) == i) {
  	  for (int x: positive.con[i])
  	    for (int y: positive.con[i])
  	      if (p[x][y] == 0)
  	        return 0;
	  }
	  if (one.find(i) == i) {
  	  for (int x: one.con[i])
  	    for (int y: one.con[i])
  	      if (p[x][y] != 1)
  	        return 0;
  	  for (int j = 0, o = one.con[i].size(); j < o - 1; j++)
  	    add_edge(one.con[i][j], one.con[i][j + 1]);
	  }
	  if (two.find(i) == i && (int)two.con[i].size() > 1) {
	    if ((int)two.con[i].size() == 2) return 0;
	    for (int j = 0, o = two.con[i].size(); j < o - 1; j++)
	      add_edge(two.con[i][j], two.con[i][j + 1]);
	    add_edge(two.con[i][0], two.con[i].back());
	  }
	}
	build(ans);
	return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 7 ms 1748 KB Output is correct
7 Correct 157 ms 26200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 7 ms 1748 KB Output is correct
7 Correct 157 ms 26200 KB Output is correct
8 Correct 0 ms 600 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 0 ms 604 KB Output is correct
12 Correct 6 ms 1368 KB Output is correct
13 Correct 141 ms 22268 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
15 Correct 0 ms 604 KB Output is correct
16 Correct 4 ms 1088 KB Output is correct
17 Correct 78 ms 14860 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 0 ms 604 KB Output is correct
20 Correct 42 ms 6072 KB Output is correct
21 Correct 150 ms 22868 KB Output is correct
22 Correct 144 ms 22464 KB Output is correct
23 Correct 158 ms 24132 KB Output is correct
24 Correct 142 ms 22256 KB Output is correct
25 Correct 63 ms 12800 KB Output is correct
26 Correct 59 ms 12272 KB Output is correct
27 Correct 157 ms 25332 KB Output is correct
28 Correct 151 ms 22604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 6 ms 1372 KB Output is correct
9 Correct 146 ms 22352 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 0 ms 604 KB Output is correct
12 Correct 7 ms 1596 KB Output is correct
13 Correct 156 ms 26412 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 4 ms 1092 KB Output is correct
17 Correct 79 ms 14836 KB Output is correct
18 Correct 0 ms 600 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 36 ms 6064 KB Output is correct
22 Correct 154 ms 22772 KB Output is correct
23 Correct 143 ms 22440 KB Output is correct
24 Correct 157 ms 25244 KB Output is correct
25 Correct 60 ms 12568 KB Output is correct
26 Correct 62 ms 12788 KB Output is correct
27 Correct 142 ms 22268 KB Output is correct
28 Correct 170 ms 24412 KB Output is correct
29 Correct 60 ms 12368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 38 ms 6016 KB Output is correct
5 Correct 152 ms 22920 KB Output is correct
6 Correct 145 ms 22252 KB Output is correct
7 Correct 161 ms 24492 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 37 ms 5948 KB Output is correct
10 Correct 159 ms 22868 KB Output is correct
11 Correct 155 ms 22448 KB Output is correct
12 Correct 163 ms 25368 KB Output is correct
13 Correct 1 ms 600 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 41 ms 6004 KB Output is correct
17 Correct 145 ms 22772 KB Output is correct
18 Correct 147 ms 22768 KB Output is correct
19 Correct 143 ms 22528 KB Output is correct
20 Correct 143 ms 22272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 7 ms 1748 KB Output is correct
7 Correct 157 ms 26200 KB Output is correct
8 Correct 0 ms 600 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 0 ms 604 KB Output is correct
12 Correct 6 ms 1368 KB Output is correct
13 Correct 141 ms 22268 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
15 Correct 0 ms 604 KB Output is correct
16 Correct 4 ms 1088 KB Output is correct
17 Correct 78 ms 14860 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 0 ms 604 KB Output is correct
20 Correct 42 ms 6072 KB Output is correct
21 Correct 150 ms 22868 KB Output is correct
22 Correct 144 ms 22464 KB Output is correct
23 Correct 158 ms 24132 KB Output is correct
24 Correct 142 ms 22256 KB Output is correct
25 Correct 63 ms 12800 KB Output is correct
26 Correct 59 ms 12272 KB Output is correct
27 Correct 157 ms 25332 KB Output is correct
28 Correct 151 ms 22604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 0 ms 604 KB Output is correct
31 Correct 0 ms 604 KB Output is correct
32 Correct 0 ms 604 KB Output is correct
33 Correct 0 ms 604 KB Output is correct
34 Correct 0 ms 604 KB Output is correct
35 Correct 0 ms 604 KB Output is correct
36 Correct 6 ms 1372 KB Output is correct
37 Correct 146 ms 22352 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
39 Correct 0 ms 604 KB Output is correct
40 Correct 7 ms 1596 KB Output is correct
41 Correct 156 ms 26412 KB Output is correct
42 Correct 1 ms 600 KB Output is correct
43 Correct 1 ms 604 KB Output is correct
44 Correct 4 ms 1092 KB Output is correct
45 Correct 79 ms 14836 KB Output is correct
46 Correct 0 ms 600 KB Output is correct
47 Correct 1 ms 600 KB Output is correct
48 Correct 1 ms 604 KB Output is correct
49 Correct 36 ms 6064 KB Output is correct
50 Correct 154 ms 22772 KB Output is correct
51 Correct 143 ms 22440 KB Output is correct
52 Correct 157 ms 25244 KB Output is correct
53 Correct 60 ms 12568 KB Output is correct
54 Correct 62 ms 12788 KB Output is correct
55 Correct 142 ms 22268 KB Output is correct
56 Correct 170 ms 24412 KB Output is correct
57 Correct 60 ms 12368 KB Output is correct
58 Correct 1 ms 604 KB Output is correct
59 Correct 1 ms 604 KB Output is correct
60 Correct 4 ms 1116 KB Output is correct
61 Correct 81 ms 16368 KB Output is correct
62 Correct 1 ms 600 KB Output is correct
63 Correct 0 ms 604 KB Output is correct
64 Correct 0 ms 604 KB Output is correct
65 Correct 36 ms 5980 KB Output is correct
66 Correct 65 ms 12928 KB Output is correct
67 Correct 65 ms 12884 KB Output is correct
68 Correct 60 ms 12544 KB Output is correct
69 Correct 60 ms 12372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 7 ms 1748 KB Output is correct
7 Correct 157 ms 26200 KB Output is correct
8 Correct 0 ms 600 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 0 ms 604 KB Output is correct
12 Correct 6 ms 1368 KB Output is correct
13 Correct 141 ms 22268 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
15 Correct 0 ms 604 KB Output is correct
16 Correct 4 ms 1088 KB Output is correct
17 Correct 78 ms 14860 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 0 ms 604 KB Output is correct
20 Correct 42 ms 6072 KB Output is correct
21 Correct 150 ms 22868 KB Output is correct
22 Correct 144 ms 22464 KB Output is correct
23 Correct 158 ms 24132 KB Output is correct
24 Correct 142 ms 22256 KB Output is correct
25 Correct 63 ms 12800 KB Output is correct
26 Correct 59 ms 12272 KB Output is correct
27 Correct 157 ms 25332 KB Output is correct
28 Correct 151 ms 22604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 0 ms 604 KB Output is correct
31 Correct 0 ms 604 KB Output is correct
32 Correct 0 ms 604 KB Output is correct
33 Correct 0 ms 604 KB Output is correct
34 Correct 0 ms 604 KB Output is correct
35 Correct 0 ms 604 KB Output is correct
36 Correct 6 ms 1372 KB Output is correct
37 Correct 146 ms 22352 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
39 Correct 0 ms 604 KB Output is correct
40 Correct 7 ms 1596 KB Output is correct
41 Correct 156 ms 26412 KB Output is correct
42 Correct 1 ms 600 KB Output is correct
43 Correct 1 ms 604 KB Output is correct
44 Correct 4 ms 1092 KB Output is correct
45 Correct 79 ms 14836 KB Output is correct
46 Correct 0 ms 600 KB Output is correct
47 Correct 1 ms 600 KB Output is correct
48 Correct 1 ms 604 KB Output is correct
49 Correct 36 ms 6064 KB Output is correct
50 Correct 154 ms 22772 KB Output is correct
51 Correct 143 ms 22440 KB Output is correct
52 Correct 157 ms 25244 KB Output is correct
53 Correct 60 ms 12568 KB Output is correct
54 Correct 62 ms 12788 KB Output is correct
55 Correct 142 ms 22268 KB Output is correct
56 Correct 170 ms 24412 KB Output is correct
57 Correct 60 ms 12368 KB Output is correct
58 Correct 0 ms 604 KB Output is correct
59 Correct 0 ms 604 KB Output is correct
60 Correct 1 ms 604 KB Output is correct
61 Correct 38 ms 6016 KB Output is correct
62 Correct 152 ms 22920 KB Output is correct
63 Correct 145 ms 22252 KB Output is correct
64 Correct 161 ms 24492 KB Output is correct
65 Correct 0 ms 604 KB Output is correct
66 Correct 37 ms 5948 KB Output is correct
67 Correct 159 ms 22868 KB Output is correct
68 Correct 155 ms 22448 KB Output is correct
69 Correct 163 ms 25368 KB Output is correct
70 Correct 1 ms 600 KB Output is correct
71 Correct 0 ms 604 KB Output is correct
72 Correct 1 ms 600 KB Output is correct
73 Correct 41 ms 6004 KB Output is correct
74 Correct 145 ms 22772 KB Output is correct
75 Correct 147 ms 22768 KB Output is correct
76 Correct 143 ms 22528 KB Output is correct
77 Correct 143 ms 22272 KB Output is correct
78 Correct 1 ms 604 KB Output is correct
79 Correct 1 ms 604 KB Output is correct
80 Correct 4 ms 1116 KB Output is correct
81 Correct 81 ms 16368 KB Output is correct
82 Correct 1 ms 600 KB Output is correct
83 Correct 0 ms 604 KB Output is correct
84 Correct 0 ms 604 KB Output is correct
85 Correct 36 ms 5980 KB Output is correct
86 Correct 65 ms 12928 KB Output is correct
87 Correct 65 ms 12884 KB Output is correct
88 Correct 60 ms 12544 KB Output is correct
89 Correct 60 ms 12372 KB Output is correct
90 Correct 1 ms 604 KB Output is correct
91 Correct 1 ms 604 KB Output is correct
92 Correct 3 ms 872 KB Output is correct
93 Correct 59 ms 8444 KB Output is correct
94 Correct 1 ms 604 KB Output is correct
95 Correct 1 ms 600 KB Output is correct
96 Correct 1 ms 600 KB Output is correct
97 Correct 17 ms 2648 KB Output is correct
98 Correct 65 ms 8448 KB Output is correct
99 Correct 63 ms 8436 KB Output is correct
100 Correct 57 ms 8528 KB Output is correct
101 Correct 57 ms 8444 KB Output is correct
102 Correct 58 ms 8432 KB Output is correct
103 Correct 57 ms 8528 KB Output is correct
104 Correct 56 ms 8448 KB Output is correct
105 Correct 61 ms 8448 KB Output is correct