#include "supertrees.h"
// author : sentheta aka vanwij
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<cassert>
#include<random>
#include<chrono>
#include<cmath>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
#define Int long long
#define V vector
#define pb push_back
#define pii pair<int,int>
#define ff first
#define ss second
#define rand() (uniform_int_distribution<int>(0,1<<30)(rng))
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pow2(x) (1LL<<(x))
#define msb(x) (63-__builtin_clzll(x))
#define bitcnt(x) (__builtin_popcountll(x))
#define nl '\n'
#define _ << ' ' <<
#define dbg(x) cout<<"?"<< #x <<" : "<<(x)<<endl; cout.flush();
#define rep(i,a,b) for(int i = (int)(a); i < (int)(b); i++)
#define all(x) (x).begin(), (x).end()
const int N = 1005;
struct Dsu{
int p[N];
V<int> v[N];
void reset(){
rep(i,0,N){
p[i] = -1;
v[i] = {i};
}
}
int find(int x){
if(p[x]==-1) return x;
return p[x] = find(p[x]);
}
bool same(int x,int y){
return find(x)==find(y);
}
void unite(int x,int y){
if((x=find(x))==(y=find(y))) return;
// wlog sz(x) > sz(y)
if(v[x].size() < v[y].size()) swap(x,y);
p[y] = x;
for(int i : v[y]) v[x].pb(i);
v[y].clear();
}
} dsu, t;
int n;
V<V<int>> ans, p;
void make_edge(int x,int y){
ans[x][y] = ans[y][x] = 1;
}
bool f(V<int>& v){
int sz = v.size();
bool two = false, tri = false;
for(int x : v) for(int y : v){
two |= p[x][y]==2;
tri |= p[x][y]==3;
}
if(two && tri) return false;
// straight path
if(!two && !tri){
rep(i,1,sz){
make_edge(v[i], v[i-1]);
}
return true;
}
// cycle grouping
for(int x : v) for(int y : v){
if(p[x][y] == 1){
t.unite(x,y);
}
}
// find main cycle
V<int> lead;
for(int x : v) if(t.find(x)==x){
lead.pb(x);
}
if(lead.size() <= 2) return false;
if(tri) return false;
//if(tri && lead.size()<=3) return false;
// make main cycle
rep(i,0,lead.size()){
make_edge(lead[i], lead[(i+1)%lead.size()]);
}
if(tri){
make_edge(lead[0], lead[2]);
}
// group of each nodes
for(int x : lead){
for(int y : t.v[x]) if(y != x){
make_edge(x, y);
}
}
return true;
}
int construct(V<V<int>> _p) {
p = _p; n = p.size();
dsu.reset(); t.reset();
ans = V<V<int>>(n,V<int>(n));
// connected components
rep(i,0,n) rep(j,0,i) if(p[i][j]){
dsu.unite(i,j);
}
rep(i,0,n) rep(j,0,i) if(dsu.same(i,j)){
if(p[i][j] == 0) return 0;
}
// solve per cc
rep(i,0,n) if(dsu.find(i) == i){
if(!f(dsu.v[i])) return 0;
}
build(ans);
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
360 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
8 ms |
1492 KB |
Output is correct |
7 |
Correct |
178 ms |
28004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
360 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
8 ms |
1492 KB |
Output is correct |
7 |
Correct |
178 ms |
28004 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
360 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
8 ms |
1524 KB |
Output is correct |
13 |
Correct |
174 ms |
28036 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
1108 KB |
Output is correct |
17 |
Correct |
88 ms |
18152 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
44 ms |
7352 KB |
Output is correct |
21 |
Correct |
189 ms |
27980 KB |
Output is correct |
22 |
Correct |
234 ms |
27960 KB |
Output is correct |
23 |
Correct |
188 ms |
28088 KB |
Output is correct |
24 |
Correct |
209 ms |
28004 KB |
Output is correct |
25 |
Correct |
78 ms |
18128 KB |
Output is correct |
26 |
Correct |
74 ms |
18252 KB |
Output is correct |
27 |
Correct |
206 ms |
28080 KB |
Output is correct |
28 |
Correct |
179 ms |
28028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
360 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
7 ms |
1684 KB |
Output is correct |
9 |
Correct |
184 ms |
28080 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
9 ms |
1524 KB |
Output is correct |
13 |
Correct |
182 ms |
28080 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
1108 KB |
Output is correct |
17 |
Correct |
80 ms |
18228 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
45 ms |
7372 KB |
Output is correct |
22 |
Correct |
178 ms |
27976 KB |
Output is correct |
23 |
Correct |
182 ms |
28008 KB |
Output is correct |
24 |
Correct |
192 ms |
28028 KB |
Output is correct |
25 |
Correct |
73 ms |
18104 KB |
Output is correct |
26 |
Correct |
71 ms |
18096 KB |
Output is correct |
27 |
Correct |
176 ms |
28016 KB |
Output is correct |
28 |
Correct |
181 ms |
27996 KB |
Output is correct |
29 |
Correct |
71 ms |
18112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
48 ms |
7348 KB |
Output is correct |
5 |
Correct |
175 ms |
28028 KB |
Output is correct |
6 |
Correct |
193 ms |
28016 KB |
Output is correct |
7 |
Correct |
182 ms |
28012 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
49 ms |
7328 KB |
Output is correct |
10 |
Correct |
171 ms |
28088 KB |
Output is correct |
11 |
Correct |
256 ms |
27980 KB |
Output is correct |
12 |
Correct |
195 ms |
28076 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
360 KB |
Output is correct |
16 |
Correct |
46 ms |
7276 KB |
Output is correct |
17 |
Correct |
175 ms |
28088 KB |
Output is correct |
18 |
Correct |
205 ms |
28052 KB |
Output is correct |
19 |
Correct |
177 ms |
28000 KB |
Output is correct |
20 |
Correct |
191 ms |
28232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
360 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
8 ms |
1492 KB |
Output is correct |
7 |
Correct |
178 ms |
28004 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
360 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
8 ms |
1524 KB |
Output is correct |
13 |
Correct |
174 ms |
28036 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
1108 KB |
Output is correct |
17 |
Correct |
88 ms |
18152 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
44 ms |
7352 KB |
Output is correct |
21 |
Correct |
189 ms |
27980 KB |
Output is correct |
22 |
Correct |
234 ms |
27960 KB |
Output is correct |
23 |
Correct |
188 ms |
28088 KB |
Output is correct |
24 |
Correct |
209 ms |
28004 KB |
Output is correct |
25 |
Correct |
78 ms |
18128 KB |
Output is correct |
26 |
Correct |
74 ms |
18252 KB |
Output is correct |
27 |
Correct |
206 ms |
28080 KB |
Output is correct |
28 |
Correct |
179 ms |
28028 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
360 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
7 ms |
1684 KB |
Output is correct |
37 |
Correct |
184 ms |
28080 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
9 ms |
1524 KB |
Output is correct |
41 |
Correct |
182 ms |
28080 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
4 ms |
1108 KB |
Output is correct |
45 |
Correct |
80 ms |
18228 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
45 ms |
7372 KB |
Output is correct |
50 |
Correct |
178 ms |
27976 KB |
Output is correct |
51 |
Correct |
182 ms |
28008 KB |
Output is correct |
52 |
Correct |
192 ms |
28028 KB |
Output is correct |
53 |
Correct |
73 ms |
18104 KB |
Output is correct |
54 |
Correct |
71 ms |
18096 KB |
Output is correct |
55 |
Correct |
176 ms |
28016 KB |
Output is correct |
56 |
Correct |
181 ms |
27996 KB |
Output is correct |
57 |
Correct |
71 ms |
18112 KB |
Output is correct |
58 |
Correct |
1 ms |
400 KB |
Output is correct |
59 |
Correct |
1 ms |
360 KB |
Output is correct |
60 |
Correct |
4 ms |
1144 KB |
Output is correct |
61 |
Correct |
84 ms |
18092 KB |
Output is correct |
62 |
Correct |
1 ms |
340 KB |
Output is correct |
63 |
Correct |
1 ms |
356 KB |
Output is correct |
64 |
Correct |
1 ms |
340 KB |
Output is correct |
65 |
Correct |
68 ms |
7364 KB |
Output is correct |
66 |
Correct |
72 ms |
18124 KB |
Output is correct |
67 |
Correct |
77 ms |
18112 KB |
Output is correct |
68 |
Correct |
76 ms |
18104 KB |
Output is correct |
69 |
Correct |
82 ms |
18104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
360 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
8 ms |
1492 KB |
Output is correct |
7 |
Correct |
178 ms |
28004 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
360 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
8 ms |
1524 KB |
Output is correct |
13 |
Correct |
174 ms |
28036 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
1108 KB |
Output is correct |
17 |
Correct |
88 ms |
18152 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
44 ms |
7352 KB |
Output is correct |
21 |
Correct |
189 ms |
27980 KB |
Output is correct |
22 |
Correct |
234 ms |
27960 KB |
Output is correct |
23 |
Correct |
188 ms |
28088 KB |
Output is correct |
24 |
Correct |
209 ms |
28004 KB |
Output is correct |
25 |
Correct |
78 ms |
18128 KB |
Output is correct |
26 |
Correct |
74 ms |
18252 KB |
Output is correct |
27 |
Correct |
206 ms |
28080 KB |
Output is correct |
28 |
Correct |
179 ms |
28028 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
360 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
7 ms |
1684 KB |
Output is correct |
37 |
Correct |
184 ms |
28080 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
9 ms |
1524 KB |
Output is correct |
41 |
Correct |
182 ms |
28080 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
4 ms |
1108 KB |
Output is correct |
45 |
Correct |
80 ms |
18228 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
45 ms |
7372 KB |
Output is correct |
50 |
Correct |
178 ms |
27976 KB |
Output is correct |
51 |
Correct |
182 ms |
28008 KB |
Output is correct |
52 |
Correct |
192 ms |
28028 KB |
Output is correct |
53 |
Correct |
73 ms |
18104 KB |
Output is correct |
54 |
Correct |
71 ms |
18096 KB |
Output is correct |
55 |
Correct |
176 ms |
28016 KB |
Output is correct |
56 |
Correct |
181 ms |
27996 KB |
Output is correct |
57 |
Correct |
71 ms |
18112 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
48 ms |
7348 KB |
Output is correct |
62 |
Correct |
175 ms |
28028 KB |
Output is correct |
63 |
Correct |
193 ms |
28016 KB |
Output is correct |
64 |
Correct |
182 ms |
28012 KB |
Output is correct |
65 |
Correct |
1 ms |
340 KB |
Output is correct |
66 |
Correct |
49 ms |
7328 KB |
Output is correct |
67 |
Correct |
171 ms |
28088 KB |
Output is correct |
68 |
Correct |
256 ms |
27980 KB |
Output is correct |
69 |
Correct |
195 ms |
28076 KB |
Output is correct |
70 |
Correct |
1 ms |
340 KB |
Output is correct |
71 |
Correct |
1 ms |
340 KB |
Output is correct |
72 |
Correct |
1 ms |
360 KB |
Output is correct |
73 |
Correct |
46 ms |
7276 KB |
Output is correct |
74 |
Correct |
175 ms |
28088 KB |
Output is correct |
75 |
Correct |
205 ms |
28052 KB |
Output is correct |
76 |
Correct |
177 ms |
28000 KB |
Output is correct |
77 |
Correct |
191 ms |
28232 KB |
Output is correct |
78 |
Correct |
1 ms |
400 KB |
Output is correct |
79 |
Correct |
1 ms |
360 KB |
Output is correct |
80 |
Correct |
4 ms |
1144 KB |
Output is correct |
81 |
Correct |
84 ms |
18092 KB |
Output is correct |
82 |
Correct |
1 ms |
340 KB |
Output is correct |
83 |
Correct |
1 ms |
356 KB |
Output is correct |
84 |
Correct |
1 ms |
340 KB |
Output is correct |
85 |
Correct |
68 ms |
7364 KB |
Output is correct |
86 |
Correct |
72 ms |
18124 KB |
Output is correct |
87 |
Correct |
77 ms |
18112 KB |
Output is correct |
88 |
Correct |
76 ms |
18104 KB |
Output is correct |
89 |
Correct |
82 ms |
18104 KB |
Output is correct |
90 |
Correct |
1 ms |
340 KB |
Output is correct |
91 |
Correct |
1 ms |
340 KB |
Output is correct |
92 |
Correct |
5 ms |
1108 KB |
Output is correct |
93 |
Correct |
79 ms |
18104 KB |
Output is correct |
94 |
Correct |
1 ms |
340 KB |
Output is correct |
95 |
Correct |
1 ms |
356 KB |
Output is correct |
96 |
Correct |
1 ms |
408 KB |
Output is correct |
97 |
Correct |
19 ms |
4812 KB |
Output is correct |
98 |
Correct |
77 ms |
18096 KB |
Output is correct |
99 |
Correct |
75 ms |
18124 KB |
Output is correct |
100 |
Correct |
74 ms |
18100 KB |
Output is correct |
101 |
Correct |
94 ms |
18096 KB |
Output is correct |
102 |
Correct |
80 ms |
18104 KB |
Output is correct |
103 |
Correct |
82 ms |
18124 KB |
Output is correct |
104 |
Correct |
87 ms |
18096 KB |
Output is correct |
105 |
Correct |
83 ms |
18096 KB |
Output is correct |