#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define sz(a) a.size()
typedef long long ll;
typedef pair<int, int> pii;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int cmp[1005];
int cmpSize[1005];
vector<vector<int>> adj;
int r = 0;
int cs = 0;
int N;
void dfs(int u){
cmp[u] = r;
cs++;
for(int i = 0; i < N;i ++){
if(adj[u][i] != 0 && cmp[i] == -1){
dfs(i);
}
}
}
int dsu[1005];
int sz[1005];
int find(int u){
if(dsu[u] == u)return u;
return dsu[u] = find(dsu[u]);
}
void merge(int u, int v){
u = find(u);
v = find(v);
if(u == v)return;
if(sz[u] < sz[v])swap(u,v);
dsu[v] = u;
sz[u] += sz[v];
}
int construct(vector<vector<int>> p){
N = p.size();
adj.resize(N);
vector<vector<int>> edges;
edges.resize(N);
for(int i = 0; i < N; i++){
adj[i].resize(N);
edges[i].resize(N);
dsu[i] = i;
sz[i] = 1;
for(int j = 0;j < N;j ++){
adj[i][j] = p[i][j];
if(p[i][j] == 3)return 0;
}
}
memset(cmp, -1, sizeof(cmp));
for(int i = 0; i < N; i++){
if(cmp[i] == -1){
cs = 0;
r++;
dfs(i);
cmpSize[r] = cs;
}
}
for(int i = 0; i < N; i++){
for(int j = 0;j < N;j ++){
if(cmp[i] == cmp[j] && p[i][j] == 0){
return 0;
}
}
}
for(int i = 0; i < N;i ++){
int g = 0;
for(int j =0;j < N; j++){
g = max(g, p[i][j]);
}
if(cmpSize[cmp[i]] <= g && cmpSize[cmp[i]] != 1)return 0;
}
//0 = different components
//1 = same branch
//2 = in da cycle. can't have multiple cycles in one connected component
//dsu to store the branches
for(int i = 0; i < N; i++){
for(int j =0;j < N; j++){
if(p[i][j] == 1){
merge(i,j);
}
}
}
//store the possible parents of branches
set<int> pa;
vector<int> parents;
for(int i = 0; i < N;i ++){
pa.insert(find(i));
}
for(int u : pa)parents.pb(u);
//add edges between parents that are in the same cycle
int prevCmp[N+5];
int fCmp[N+5];
memset(fCmp, -1, sizeof(fCmp));
memset(prevCmp, -1, sizeof(prevCmp));
for(int i = 0; i < parents.size(); i++){
int curr = parents[i];
int comp = cmp[curr];
if(fCmp[comp] == -1){
fCmp[comp] = curr;
}
//find the previous one in curr's connected comp
if(prevCmp[comp] != -1){
//add edge
edges[prevCmp[comp]][curr] = 1;
edges[curr][prevCmp[comp]] = 1;
}
prevCmp[comp] = curr;
}
//create the loop in each component
for(int i = 1; i <= r; i++){
if(prevCmp[i] != fCmp[i]){
if(prevCmp[i] == -1 || fCmp[i] == -1)continue;
edges[prevCmp[i]][fCmp[i]] = 1;
edges[fCmp[i]][prevCmp[i]] = 1;
}
}
//add edges in the branches
int prevD[N+5];
memset(prevD, -1, sizeof(prevD));
for(int i = 0; i < N; i++){
if(prevD[find(i)] != -1){
edges[prevD[find(i)]][i] = 1;
edges[i][prevD[find(i)]] = 1;
}
prevD[find(i)] = i;
}
build(edges);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:105:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | for(int i = 0; i < parents.size(); i++){
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
11 ms |
1388 KB |
Output is correct |
7 |
Correct |
253 ms |
26092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
11 ms |
1388 KB |
Output is correct |
7 |
Correct |
253 ms |
26092 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
0 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
11 ms |
1388 KB |
Output is correct |
13 |
Correct |
256 ms |
26088 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
5 ms |
1004 KB |
Output is correct |
17 |
Correct |
116 ms |
16236 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
63 ms |
6892 KB |
Output is correct |
21 |
Correct |
264 ms |
26092 KB |
Output is correct |
22 |
Correct |
248 ms |
26092 KB |
Output is correct |
23 |
Correct |
264 ms |
26092 KB |
Output is correct |
24 |
Correct |
250 ms |
26220 KB |
Output is correct |
25 |
Correct |
107 ms |
16108 KB |
Output is correct |
26 |
Correct |
104 ms |
16108 KB |
Output is correct |
27 |
Correct |
263 ms |
26092 KB |
Output is correct |
28 |
Correct |
251 ms |
26092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
14 ms |
1388 KB |
Output is correct |
9 |
Correct |
253 ms |
26220 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
11 ms |
1388 KB |
Output is correct |
13 |
Correct |
250 ms |
26220 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
5 ms |
1004 KB |
Output is correct |
17 |
Correct |
117 ms |
16236 KB |
Output is correct |
18 |
Correct |
0 ms |
364 KB |
Output is correct |
19 |
Correct |
0 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
62 ms |
6892 KB |
Output is correct |
22 |
Correct |
249 ms |
26220 KB |
Output is correct |
23 |
Correct |
262 ms |
26092 KB |
Output is correct |
24 |
Correct |
255 ms |
26220 KB |
Output is correct |
25 |
Correct |
109 ms |
16108 KB |
Output is correct |
26 |
Correct |
106 ms |
16108 KB |
Output is correct |
27 |
Correct |
245 ms |
26092 KB |
Output is correct |
28 |
Correct |
252 ms |
26092 KB |
Output is correct |
29 |
Correct |
104 ms |
16108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
63 ms |
6892 KB |
Output is correct |
5 |
Correct |
249 ms |
26092 KB |
Output is correct |
6 |
Correct |
253 ms |
26092 KB |
Output is correct |
7 |
Correct |
261 ms |
26092 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
62 ms |
6892 KB |
Output is correct |
10 |
Correct |
248 ms |
26092 KB |
Output is correct |
11 |
Correct |
251 ms |
26220 KB |
Output is correct |
12 |
Correct |
258 ms |
26220 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
62 ms |
6892 KB |
Output is correct |
17 |
Correct |
250 ms |
26092 KB |
Output is correct |
18 |
Correct |
254 ms |
26220 KB |
Output is correct |
19 |
Correct |
256 ms |
26092 KB |
Output is correct |
20 |
Correct |
252 ms |
26092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
11 ms |
1388 KB |
Output is correct |
7 |
Correct |
253 ms |
26092 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
0 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
11 ms |
1388 KB |
Output is correct |
13 |
Correct |
256 ms |
26088 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
5 ms |
1004 KB |
Output is correct |
17 |
Correct |
116 ms |
16236 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
63 ms |
6892 KB |
Output is correct |
21 |
Correct |
264 ms |
26092 KB |
Output is correct |
22 |
Correct |
248 ms |
26092 KB |
Output is correct |
23 |
Correct |
264 ms |
26092 KB |
Output is correct |
24 |
Correct |
250 ms |
26220 KB |
Output is correct |
25 |
Correct |
107 ms |
16108 KB |
Output is correct |
26 |
Correct |
104 ms |
16108 KB |
Output is correct |
27 |
Correct |
263 ms |
26092 KB |
Output is correct |
28 |
Correct |
251 ms |
26092 KB |
Output is correct |
29 |
Correct |
0 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
0 ms |
364 KB |
Output is correct |
32 |
Correct |
0 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
14 ms |
1388 KB |
Output is correct |
37 |
Correct |
253 ms |
26220 KB |
Output is correct |
38 |
Correct |
1 ms |
492 KB |
Output is correct |
39 |
Correct |
0 ms |
364 KB |
Output is correct |
40 |
Correct |
11 ms |
1388 KB |
Output is correct |
41 |
Correct |
250 ms |
26220 KB |
Output is correct |
42 |
Correct |
1 ms |
364 KB |
Output is correct |
43 |
Correct |
1 ms |
364 KB |
Output is correct |
44 |
Correct |
5 ms |
1004 KB |
Output is correct |
45 |
Correct |
117 ms |
16236 KB |
Output is correct |
46 |
Correct |
0 ms |
364 KB |
Output is correct |
47 |
Correct |
0 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
62 ms |
6892 KB |
Output is correct |
50 |
Correct |
249 ms |
26220 KB |
Output is correct |
51 |
Correct |
262 ms |
26092 KB |
Output is correct |
52 |
Correct |
255 ms |
26220 KB |
Output is correct |
53 |
Correct |
109 ms |
16108 KB |
Output is correct |
54 |
Correct |
106 ms |
16108 KB |
Output is correct |
55 |
Correct |
245 ms |
26092 KB |
Output is correct |
56 |
Correct |
252 ms |
26092 KB |
Output is correct |
57 |
Correct |
104 ms |
16108 KB |
Output is correct |
58 |
Correct |
0 ms |
364 KB |
Output is correct |
59 |
Correct |
1 ms |
364 KB |
Output is correct |
60 |
Correct |
5 ms |
1004 KB |
Output is correct |
61 |
Correct |
115 ms |
16236 KB |
Output is correct |
62 |
Correct |
1 ms |
364 KB |
Output is correct |
63 |
Correct |
1 ms |
364 KB |
Output is correct |
64 |
Correct |
1 ms |
364 KB |
Output is correct |
65 |
Correct |
62 ms |
6892 KB |
Output is correct |
66 |
Correct |
110 ms |
16108 KB |
Output is correct |
67 |
Correct |
112 ms |
16108 KB |
Output is correct |
68 |
Correct |
104 ms |
16108 KB |
Output is correct |
69 |
Correct |
108 ms |
16108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
11 ms |
1388 KB |
Output is correct |
7 |
Correct |
253 ms |
26092 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
0 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
11 ms |
1388 KB |
Output is correct |
13 |
Correct |
256 ms |
26088 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
5 ms |
1004 KB |
Output is correct |
17 |
Correct |
116 ms |
16236 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
63 ms |
6892 KB |
Output is correct |
21 |
Correct |
264 ms |
26092 KB |
Output is correct |
22 |
Correct |
248 ms |
26092 KB |
Output is correct |
23 |
Correct |
264 ms |
26092 KB |
Output is correct |
24 |
Correct |
250 ms |
26220 KB |
Output is correct |
25 |
Correct |
107 ms |
16108 KB |
Output is correct |
26 |
Correct |
104 ms |
16108 KB |
Output is correct |
27 |
Correct |
263 ms |
26092 KB |
Output is correct |
28 |
Correct |
251 ms |
26092 KB |
Output is correct |
29 |
Correct |
0 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
0 ms |
364 KB |
Output is correct |
32 |
Correct |
0 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
14 ms |
1388 KB |
Output is correct |
37 |
Correct |
253 ms |
26220 KB |
Output is correct |
38 |
Correct |
1 ms |
492 KB |
Output is correct |
39 |
Correct |
0 ms |
364 KB |
Output is correct |
40 |
Correct |
11 ms |
1388 KB |
Output is correct |
41 |
Correct |
250 ms |
26220 KB |
Output is correct |
42 |
Correct |
1 ms |
364 KB |
Output is correct |
43 |
Correct |
1 ms |
364 KB |
Output is correct |
44 |
Correct |
5 ms |
1004 KB |
Output is correct |
45 |
Correct |
117 ms |
16236 KB |
Output is correct |
46 |
Correct |
0 ms |
364 KB |
Output is correct |
47 |
Correct |
0 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
62 ms |
6892 KB |
Output is correct |
50 |
Correct |
249 ms |
26220 KB |
Output is correct |
51 |
Correct |
262 ms |
26092 KB |
Output is correct |
52 |
Correct |
255 ms |
26220 KB |
Output is correct |
53 |
Correct |
109 ms |
16108 KB |
Output is correct |
54 |
Correct |
106 ms |
16108 KB |
Output is correct |
55 |
Correct |
245 ms |
26092 KB |
Output is correct |
56 |
Correct |
252 ms |
26092 KB |
Output is correct |
57 |
Correct |
104 ms |
16108 KB |
Output is correct |
58 |
Correct |
0 ms |
364 KB |
Output is correct |
59 |
Correct |
1 ms |
364 KB |
Output is correct |
60 |
Correct |
1 ms |
364 KB |
Output is correct |
61 |
Correct |
63 ms |
6892 KB |
Output is correct |
62 |
Correct |
249 ms |
26092 KB |
Output is correct |
63 |
Correct |
253 ms |
26092 KB |
Output is correct |
64 |
Correct |
261 ms |
26092 KB |
Output is correct |
65 |
Correct |
1 ms |
364 KB |
Output is correct |
66 |
Correct |
62 ms |
6892 KB |
Output is correct |
67 |
Correct |
248 ms |
26092 KB |
Output is correct |
68 |
Correct |
251 ms |
26220 KB |
Output is correct |
69 |
Correct |
258 ms |
26220 KB |
Output is correct |
70 |
Correct |
1 ms |
364 KB |
Output is correct |
71 |
Correct |
1 ms |
364 KB |
Output is correct |
72 |
Correct |
1 ms |
364 KB |
Output is correct |
73 |
Correct |
62 ms |
6892 KB |
Output is correct |
74 |
Correct |
250 ms |
26092 KB |
Output is correct |
75 |
Correct |
254 ms |
26220 KB |
Output is correct |
76 |
Correct |
256 ms |
26092 KB |
Output is correct |
77 |
Correct |
252 ms |
26092 KB |
Output is correct |
78 |
Correct |
0 ms |
364 KB |
Output is correct |
79 |
Correct |
1 ms |
364 KB |
Output is correct |
80 |
Correct |
5 ms |
1004 KB |
Output is correct |
81 |
Correct |
115 ms |
16236 KB |
Output is correct |
82 |
Correct |
1 ms |
364 KB |
Output is correct |
83 |
Correct |
1 ms |
364 KB |
Output is correct |
84 |
Correct |
1 ms |
364 KB |
Output is correct |
85 |
Correct |
62 ms |
6892 KB |
Output is correct |
86 |
Correct |
110 ms |
16108 KB |
Output is correct |
87 |
Correct |
112 ms |
16108 KB |
Output is correct |
88 |
Correct |
104 ms |
16108 KB |
Output is correct |
89 |
Correct |
108 ms |
16108 KB |
Output is correct |
90 |
Correct |
1 ms |
364 KB |
Output is correct |
91 |
Correct |
1 ms |
364 KB |
Output is correct |
92 |
Correct |
5 ms |
620 KB |
Output is correct |
93 |
Correct |
103 ms |
8300 KB |
Output is correct |
94 |
Correct |
0 ms |
364 KB |
Output is correct |
95 |
Correct |
1 ms |
364 KB |
Output is correct |
96 |
Correct |
1 ms |
364 KB |
Output is correct |
97 |
Correct |
25 ms |
2924 KB |
Output is correct |
98 |
Correct |
100 ms |
10732 KB |
Output is correct |
99 |
Correct |
101 ms |
11372 KB |
Output is correct |
100 |
Correct |
98 ms |
10880 KB |
Output is correct |
101 |
Correct |
102 ms |
12908 KB |
Output is correct |
102 |
Correct |
98 ms |
10220 KB |
Output is correct |
103 |
Correct |
101 ms |
10220 KB |
Output is correct |
104 |
Correct |
99 ms |
10220 KB |
Output is correct |
105 |
Correct |
108 ms |
10220 KB |
Output is correct |