#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
struct DisjointSetUnion{
int N;
vector<int>sz;
vector<int>link;
DisjointSetUnion(int n): N(n){
sz.resize(n);
link.resize(n);
for (int i = 0; i < n; i++){
link[i] = i;
sz[i] = 1;
}
}
int Find(int a){
if (a == link[a])return a;
return link[a] = Find(link[a]);
}
bool Unite(int a, int b){
a = Find(a), b = Find(b);
if (a == b)return false;
if (sz[a] < sz[b])swap(a,b);
sz[a] += sz[b];
link[b] = a;
return true;
}
};
class Uniderected_Graph{
public:
int N, mx = 0, far = 0;
vector<vector<int>>adj;
vector<int>Visited;
Uniderected_Graph(int _N) : N(_N){
adj.resize(N);
Visited.resize(N);
Visited.assign(N,0);
}
void Add_Edge(int A, int B){
adj[A].push_back(B);
adj[B].push_back(A);
}
void Far(int u, int p, int depth = 0){
if (depth > mx){
mx = depth;
far = u;
}
Visited[u] = 1;
for (int& nxt : adj[u]){
if (nxt == p)continue;
Far(nxt, u, depth + 1);
}
}
pair<int,int> Find_Diameter(int Source){
mx = -1, far = 0;
Far(Source,Source,0);
int A = far;
mx = -1, far = 0;
Far(A,A,0);
int B = far;
return make_pair(A,B);
}
void Dfs(int u,vector<int>& p){
if (Visited[u])return;
Visited[u] = 1;
p.push_back(u);
for (int& nxt : adj[u]){
Dfs(nxt, p);
}
}
vector<int>pracka(int Source){
vector<int>ans;
Dfs(Source,ans);
return ans;
}
};
int construct(vector<vector<int>> p) {
int n = (int)p.size();
vector<vector<int>>adj(n,vector<int>(n));
DisjointSetUnion DSU(n + 1);
for (int i = 0; i < n; i++){
for (int j = i + 1; j < n; j++){
if (p[i][j] == 3)return 0;
if (p[i][j] == 1){
DSU.Unite(i,j);
}
}
}
//check if all p[i][j] = 1 in trees, otherwise contradiction
for (int i = 0; i < n; i++){
for (int j = i + 1; j < n; j++){
int A = DSU.Find(i) , B = DSU.Find(j);
if (p[i][j] != 1 && A == B){
return 0;
}
if (A != i){
adj[A][i] = adj[i][A] = 1;
}
if (B!=j){
adj[B][j] = adj[j][B] = 1;
}
}
}
//All good
//merge trees in cycle
Uniderected_Graph G(n+1);
for (int i = 0; i < n; i++){
for (int j = i + 1; j < n; j++){
int A = DSU.Find(i), B = DSU.Find(j);
if (A == B)continue;
//Different component
if (p[A][B] == 2){
G.Add_Edge(A,B);
}
}
}
for (int i = 0; i < n; i++){
if (G.Visited[i])continue;
auto response = G.pracka(i);
if (response.size() == 1)continue;
if (response.size() == 2)return 0;
adj[response[0]][response.back()] = 1;
adj[response.back()][response[0]] = 1;
DSU.Unite(response.back(), response[0]);
for (int j = 1; j < (int)response.size(); j++){
adj[response[j]][response[j-1]] = 1;
adj[response[j-1]][response[j]] = 1;
DSU.Unite(response[j],response[j-1]);
}
}
//check if trees merged in cycle and have p[i][j] = 0, should be in another component
//check if all needed are in same component
for (int i = 0; i < n; i++){
for (int j = i + 1; j < n; j++){
int A = DSU.Find(i) , B = DSU.Find(j);
if (p[i][j] == 0 && A == B)return 0;
if (p[i][j] != 0 && A != B)return 0;
}
}
build(adj);
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1212 KB |
Output is correct |
7 |
Correct |
153 ms |
23916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1212 KB |
Output is correct |
7 |
Correct |
153 ms |
23916 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
300 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
8 ms |
1176 KB |
Output is correct |
13 |
Correct |
166 ms |
23844 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
216 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
82 ms |
13988 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
50 ms |
6176 KB |
Output is correct |
21 |
Correct |
209 ms |
24040 KB |
Output is correct |
22 |
Correct |
155 ms |
24012 KB |
Output is correct |
23 |
Correct |
200 ms |
24080 KB |
Output is correct |
24 |
Correct |
145 ms |
23924 KB |
Output is correct |
25 |
Correct |
61 ms |
14108 KB |
Output is correct |
26 |
Correct |
71 ms |
14064 KB |
Output is correct |
27 |
Correct |
155 ms |
23920 KB |
Output is correct |
28 |
Correct |
149 ms |
24060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
216 KB |
Output is correct |
8 |
Correct |
6 ms |
1176 KB |
Output is correct |
9 |
Correct |
188 ms |
23924 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
9 ms |
1460 KB |
Output is correct |
13 |
Correct |
202 ms |
28132 KB |
Output is correct |
14 |
Correct |
1 ms |
300 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
980 KB |
Output is correct |
17 |
Correct |
74 ms |
16896 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
37 ms |
6328 KB |
Output is correct |
22 |
Correct |
155 ms |
24568 KB |
Output is correct |
23 |
Correct |
149 ms |
24056 KB |
Output is correct |
24 |
Correct |
182 ms |
27264 KB |
Output is correct |
25 |
Correct |
66 ms |
14068 KB |
Output is correct |
26 |
Correct |
67 ms |
14580 KB |
Output is correct |
27 |
Correct |
152 ms |
24012 KB |
Output is correct |
28 |
Correct |
158 ms |
26080 KB |
Output is correct |
29 |
Correct |
62 ms |
14068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
38 ms |
6232 KB |
Output is correct |
5 |
Correct |
163 ms |
23968 KB |
Output is correct |
6 |
Correct |
155 ms |
23984 KB |
Output is correct |
7 |
Correct |
156 ms |
24032 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
47 ms |
6276 KB |
Output is correct |
10 |
Correct |
148 ms |
24580 KB |
Output is correct |
11 |
Correct |
186 ms |
24072 KB |
Output is correct |
12 |
Correct |
163 ms |
27124 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
300 KB |
Output is correct |
16 |
Correct |
45 ms |
6212 KB |
Output is correct |
17 |
Correct |
175 ms |
24512 KB |
Output is correct |
18 |
Correct |
163 ms |
24600 KB |
Output is correct |
19 |
Correct |
162 ms |
24188 KB |
Output is correct |
20 |
Correct |
171 ms |
23936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1212 KB |
Output is correct |
7 |
Correct |
153 ms |
23916 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
300 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
8 ms |
1176 KB |
Output is correct |
13 |
Correct |
166 ms |
23844 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
216 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
82 ms |
13988 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
50 ms |
6176 KB |
Output is correct |
21 |
Correct |
209 ms |
24040 KB |
Output is correct |
22 |
Correct |
155 ms |
24012 KB |
Output is correct |
23 |
Correct |
200 ms |
24080 KB |
Output is correct |
24 |
Correct |
145 ms |
23924 KB |
Output is correct |
25 |
Correct |
61 ms |
14108 KB |
Output is correct |
26 |
Correct |
71 ms |
14064 KB |
Output is correct |
27 |
Correct |
155 ms |
23920 KB |
Output is correct |
28 |
Correct |
149 ms |
24060 KB |
Output is correct |
29 |
Correct |
1 ms |
300 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
1 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 |
1 ms |
216 KB |
Output is correct |
36 |
Correct |
6 ms |
1176 KB |
Output is correct |
37 |
Correct |
188 ms |
23924 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
9 ms |
1460 KB |
Output is correct |
41 |
Correct |
202 ms |
28132 KB |
Output is correct |
42 |
Correct |
1 ms |
300 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
3 ms |
980 KB |
Output is correct |
45 |
Correct |
74 ms |
16896 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
37 ms |
6328 KB |
Output is correct |
50 |
Correct |
155 ms |
24568 KB |
Output is correct |
51 |
Correct |
149 ms |
24056 KB |
Output is correct |
52 |
Correct |
182 ms |
27264 KB |
Output is correct |
53 |
Correct |
66 ms |
14068 KB |
Output is correct |
54 |
Correct |
67 ms |
14580 KB |
Output is correct |
55 |
Correct |
152 ms |
24012 KB |
Output is correct |
56 |
Correct |
158 ms |
26080 KB |
Output is correct |
57 |
Correct |
62 ms |
14068 KB |
Output is correct |
58 |
Correct |
1 ms |
296 KB |
Output is correct |
59 |
Correct |
1 ms |
296 KB |
Output is correct |
60 |
Correct |
3 ms |
852 KB |
Output is correct |
61 |
Correct |
69 ms |
14080 KB |
Output is correct |
62 |
Correct |
1 ms |
304 KB |
Output is correct |
63 |
Correct |
1 ms |
212 KB |
Output is correct |
64 |
Correct |
1 ms |
212 KB |
Output is correct |
65 |
Correct |
39 ms |
6200 KB |
Output is correct |
66 |
Correct |
71 ms |
14720 KB |
Output is correct |
67 |
Correct |
79 ms |
14068 KB |
Output is correct |
68 |
Correct |
95 ms |
14192 KB |
Output is correct |
69 |
Correct |
71 ms |
14064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1212 KB |
Output is correct |
7 |
Correct |
153 ms |
23916 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
300 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
8 ms |
1176 KB |
Output is correct |
13 |
Correct |
166 ms |
23844 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
216 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
82 ms |
13988 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
50 ms |
6176 KB |
Output is correct |
21 |
Correct |
209 ms |
24040 KB |
Output is correct |
22 |
Correct |
155 ms |
24012 KB |
Output is correct |
23 |
Correct |
200 ms |
24080 KB |
Output is correct |
24 |
Correct |
145 ms |
23924 KB |
Output is correct |
25 |
Correct |
61 ms |
14108 KB |
Output is correct |
26 |
Correct |
71 ms |
14064 KB |
Output is correct |
27 |
Correct |
155 ms |
23920 KB |
Output is correct |
28 |
Correct |
149 ms |
24060 KB |
Output is correct |
29 |
Correct |
1 ms |
300 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
1 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 |
1 ms |
216 KB |
Output is correct |
36 |
Correct |
6 ms |
1176 KB |
Output is correct |
37 |
Correct |
188 ms |
23924 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
9 ms |
1460 KB |
Output is correct |
41 |
Correct |
202 ms |
28132 KB |
Output is correct |
42 |
Correct |
1 ms |
300 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
3 ms |
980 KB |
Output is correct |
45 |
Correct |
74 ms |
16896 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
37 ms |
6328 KB |
Output is correct |
50 |
Correct |
155 ms |
24568 KB |
Output is correct |
51 |
Correct |
149 ms |
24056 KB |
Output is correct |
52 |
Correct |
182 ms |
27264 KB |
Output is correct |
53 |
Correct |
66 ms |
14068 KB |
Output is correct |
54 |
Correct |
67 ms |
14580 KB |
Output is correct |
55 |
Correct |
152 ms |
24012 KB |
Output is correct |
56 |
Correct |
158 ms |
26080 KB |
Output is correct |
57 |
Correct |
62 ms |
14068 KB |
Output is correct |
58 |
Correct |
1 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
1 ms |
296 KB |
Output is correct |
61 |
Correct |
38 ms |
6232 KB |
Output is correct |
62 |
Correct |
163 ms |
23968 KB |
Output is correct |
63 |
Correct |
155 ms |
23984 KB |
Output is correct |
64 |
Correct |
156 ms |
24032 KB |
Output is correct |
65 |
Correct |
1 ms |
212 KB |
Output is correct |
66 |
Correct |
47 ms |
6276 KB |
Output is correct |
67 |
Correct |
148 ms |
24580 KB |
Output is correct |
68 |
Correct |
186 ms |
24072 KB |
Output is correct |
69 |
Correct |
163 ms |
27124 KB |
Output is correct |
70 |
Correct |
1 ms |
212 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
1 ms |
300 KB |
Output is correct |
73 |
Correct |
45 ms |
6212 KB |
Output is correct |
74 |
Correct |
175 ms |
24512 KB |
Output is correct |
75 |
Correct |
163 ms |
24600 KB |
Output is correct |
76 |
Correct |
162 ms |
24188 KB |
Output is correct |
77 |
Correct |
171 ms |
23936 KB |
Output is correct |
78 |
Correct |
1 ms |
296 KB |
Output is correct |
79 |
Correct |
1 ms |
296 KB |
Output is correct |
80 |
Correct |
3 ms |
852 KB |
Output is correct |
81 |
Correct |
69 ms |
14080 KB |
Output is correct |
82 |
Correct |
1 ms |
304 KB |
Output is correct |
83 |
Correct |
1 ms |
212 KB |
Output is correct |
84 |
Correct |
1 ms |
212 KB |
Output is correct |
85 |
Correct |
39 ms |
6200 KB |
Output is correct |
86 |
Correct |
71 ms |
14720 KB |
Output is correct |
87 |
Correct |
79 ms |
14068 KB |
Output is correct |
88 |
Correct |
95 ms |
14192 KB |
Output is correct |
89 |
Correct |
71 ms |
14064 KB |
Output is correct |
90 |
Correct |
1 ms |
212 KB |
Output is correct |
91 |
Correct |
1 ms |
212 KB |
Output is correct |
92 |
Correct |
3 ms |
800 KB |
Output is correct |
93 |
Correct |
65 ms |
14064 KB |
Output is correct |
94 |
Correct |
1 ms |
300 KB |
Output is correct |
95 |
Correct |
1 ms |
212 KB |
Output is correct |
96 |
Correct |
1 ms |
212 KB |
Output is correct |
97 |
Correct |
15 ms |
3700 KB |
Output is correct |
98 |
Correct |
66 ms |
14068 KB |
Output is correct |
99 |
Correct |
75 ms |
14088 KB |
Output is correct |
100 |
Correct |
64 ms |
14084 KB |
Output is correct |
101 |
Correct |
62 ms |
14028 KB |
Output is correct |
102 |
Correct |
60 ms |
14064 KB |
Output is correct |
103 |
Correct |
99 ms |
14064 KB |
Output is correct |
104 |
Correct |
68 ms |
14076 KB |
Output is correct |
105 |
Correct |
67 ms |
14072 KB |
Output is correct |