#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
/*<DEBUG>*/
#define tem template <typename
#define can_shift(_X_, ...) enable_if_t<sizeof test<_X_>(0) __VA_ARGS__ 8, debug&> operator<<(T i)
#define _op debug& operator<<
tem C > auto test(C *x) -> decltype(cerr << *x, 0LL);
tem C > char test(...);
tem C > struct itr{C begin, end; };
tem C > itr<C> get_range(C b, C e) { return itr<C>{b, e}; };
struct debug{
#ifdef _LOCAL
~debug(){ cerr << endl; }
tem T > can_shift(T, ==){ cerr << boolalpha << i; return *this; }
tem T> can_shift(T, !=){ return *this << get_range(begin(i), end(i)); }
tem T, typename U > _op (pair<T, U> i){
return *this << "< " << i.first << " , " << i.second << " >"; }
tem T> _op (itr<T> i){
*this << "{ ";
for(auto it = i.begin; it != i.end; it++){
*this << " , " + (it==i.begin?2:0) << *it;
}
return *this << " }";
}
#else
tem T> _op (const T&) { return *this; }
#endif
};
string _ARR_(int* arr, int sz){
string ret = "{ " + to_string(arr[0]);
for(int i = 1; i < sz; i++) ret += " , " + to_string(arr[i]);
ret += " }"; return ret;
}
#define exp(...) " [ " << #__VA_ARGS__ << " : " << (__VA_ARGS__) << " ]"
/*</DEBUG>*/
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int, int> pii;
#define pb push_back
#define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define TC int __TC__; cin >> __TC__; while(__TC__--)
const int INF = 1e9 + 7;
struct DSU{
vector<int> p;
//vector<int> sz;
//int cnt;
void init(int n){
//cnt = 0;
p.resize(n);
//sz.assign(n, 1);
iota(p.begin(), p.end(), 0);
return;
}
int findSet(int a){ return p[a] = (p[a] == a ? a : findSet(p[a])); }
bool sameSet(int a, int b){ return findSet(a) == findSet(b);}
void unionSets(int a, int b){
a = findSet(a);
b = findSet(b);
if(a == b) return;
p[a] = b;
return;
}
};
int construct(vector<vector<int>> p) {
int n = (int)p.size();
vector<vector<int>> sets(n, vector<int>());
{
DSU dsu;
dsu.init(n);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(p[i][j] == 3) return 0;
if(p[i][j]){
dsu.unionSets(i, j);
}
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(!p[i][j] && dsu.sameSet(i, j)){
return 0;
}
}
}
for(int i = 0; i < n; i++){
sets[dsu.findSet(i)].pb(i);
}
}
debug() << exp(sets);
vector<vector<int>> answer(n, vector<int>(n, 0));
for(auto v : sets){
int sz = (int)v.size();
DSU dsu;
dsu.init(n);
for(int i : v){
for(int j : v){
if(p[i][j] == 1) dsu.unionSets(i, j);
}
}
for(int i : v){
for(int j : v){
if(p[i][j] == 2 && dsu.sameSet(i, j)) return 0;
}
}
map<int, vector<int>> trees;
for(int i : v){
trees[dsu.findSet(i)].pb(i);
}
vector<int> roots;
for(auto m : trees){
roots.pb(m.first);
for(int i = 0; i < (int)m.second.size()-1; i++){
answer[m.second[i]][m.second[i+1]] = 1;
answer[m.second[i+1]][m.second[i]] = 1;
}
}
sz = (int)roots.size();
if(sz == 0 || sz == 1) continue;
if(sz < 3) return 0;
for(int i = 0; i < sz-1; i++){
answer[roots[i]][roots[i+1]] = 1;
answer[roots[i+1]][roots[i]] = 1;
}
answer[roots[0]][roots[sz-1]] = 1;
answer[roots[sz-1]][roots[0]] = 1;
}
build(answer);
return 1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
13 ms |
1152 KB |
Output is correct |
7 |
Correct |
254 ms |
22096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
13 ms |
1152 KB |
Output is correct |
7 |
Correct |
254 ms |
22096 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
10 ms |
1152 KB |
Output is correct |
13 |
Correct |
239 ms |
22136 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
640 KB |
Output is correct |
17 |
Correct |
110 ms |
8184 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
65 ms |
5880 KB |
Output is correct |
21 |
Correct |
247 ms |
22136 KB |
Output is correct |
22 |
Correct |
248 ms |
22264 KB |
Output is correct |
23 |
Correct |
255 ms |
22136 KB |
Output is correct |
24 |
Correct |
243 ms |
22136 KB |
Output is correct |
25 |
Correct |
95 ms |
8184 KB |
Output is correct |
26 |
Correct |
91 ms |
8312 KB |
Output is correct |
27 |
Correct |
256 ms |
22136 KB |
Output is correct |
28 |
Correct |
237 ms |
22136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
10 ms |
1152 KB |
Output is correct |
9 |
Correct |
236 ms |
22136 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
11 ms |
1152 KB |
Output is correct |
13 |
Correct |
247 ms |
22136 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
640 KB |
Output is correct |
17 |
Correct |
111 ms |
8184 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
256 KB |
Output is correct |
20 |
Correct |
1 ms |
256 KB |
Output is correct |
21 |
Correct |
65 ms |
5880 KB |
Output is correct |
22 |
Correct |
247 ms |
22264 KB |
Output is correct |
23 |
Correct |
244 ms |
22136 KB |
Output is correct |
24 |
Correct |
254 ms |
22264 KB |
Output is correct |
25 |
Correct |
98 ms |
12152 KB |
Output is correct |
26 |
Correct |
95 ms |
8184 KB |
Output is correct |
27 |
Correct |
251 ms |
22112 KB |
Output is correct |
28 |
Correct |
258 ms |
22136 KB |
Output is correct |
29 |
Correct |
100 ms |
12152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
62 ms |
5880 KB |
Output is correct |
5 |
Correct |
247 ms |
22264 KB |
Output is correct |
6 |
Correct |
241 ms |
22136 KB |
Output is correct |
7 |
Correct |
269 ms |
22304 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
62 ms |
5836 KB |
Output is correct |
10 |
Correct |
246 ms |
22136 KB |
Output is correct |
11 |
Correct |
246 ms |
22136 KB |
Output is correct |
12 |
Correct |
256 ms |
22136 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
64 ms |
5880 KB |
Output is correct |
17 |
Correct |
252 ms |
22172 KB |
Output is correct |
18 |
Correct |
248 ms |
22136 KB |
Output is correct |
19 |
Correct |
244 ms |
22136 KB |
Output is correct |
20 |
Correct |
238 ms |
22136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
13 ms |
1152 KB |
Output is correct |
7 |
Correct |
254 ms |
22096 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
10 ms |
1152 KB |
Output is correct |
13 |
Correct |
239 ms |
22136 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
640 KB |
Output is correct |
17 |
Correct |
110 ms |
8184 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
65 ms |
5880 KB |
Output is correct |
21 |
Correct |
247 ms |
22136 KB |
Output is correct |
22 |
Correct |
248 ms |
22264 KB |
Output is correct |
23 |
Correct |
255 ms |
22136 KB |
Output is correct |
24 |
Correct |
243 ms |
22136 KB |
Output is correct |
25 |
Correct |
95 ms |
8184 KB |
Output is correct |
26 |
Correct |
91 ms |
8312 KB |
Output is correct |
27 |
Correct |
256 ms |
22136 KB |
Output is correct |
28 |
Correct |
237 ms |
22136 KB |
Output is correct |
29 |
Correct |
0 ms |
256 KB |
Output is correct |
30 |
Correct |
0 ms |
256 KB |
Output is correct |
31 |
Correct |
0 ms |
256 KB |
Output is correct |
32 |
Correct |
0 ms |
256 KB |
Output is correct |
33 |
Correct |
0 ms |
256 KB |
Output is correct |
34 |
Correct |
1 ms |
256 KB |
Output is correct |
35 |
Correct |
0 ms |
256 KB |
Output is correct |
36 |
Correct |
10 ms |
1152 KB |
Output is correct |
37 |
Correct |
236 ms |
22136 KB |
Output is correct |
38 |
Correct |
0 ms |
256 KB |
Output is correct |
39 |
Correct |
0 ms |
256 KB |
Output is correct |
40 |
Correct |
11 ms |
1152 KB |
Output is correct |
41 |
Correct |
247 ms |
22136 KB |
Output is correct |
42 |
Correct |
0 ms |
256 KB |
Output is correct |
43 |
Correct |
0 ms |
256 KB |
Output is correct |
44 |
Correct |
5 ms |
640 KB |
Output is correct |
45 |
Correct |
111 ms |
8184 KB |
Output is correct |
46 |
Correct |
0 ms |
256 KB |
Output is correct |
47 |
Correct |
1 ms |
256 KB |
Output is correct |
48 |
Correct |
1 ms |
256 KB |
Output is correct |
49 |
Correct |
65 ms |
5880 KB |
Output is correct |
50 |
Correct |
247 ms |
22264 KB |
Output is correct |
51 |
Correct |
244 ms |
22136 KB |
Output is correct |
52 |
Correct |
254 ms |
22264 KB |
Output is correct |
53 |
Correct |
98 ms |
12152 KB |
Output is correct |
54 |
Correct |
95 ms |
8184 KB |
Output is correct |
55 |
Correct |
251 ms |
22112 KB |
Output is correct |
56 |
Correct |
258 ms |
22136 KB |
Output is correct |
57 |
Correct |
100 ms |
12152 KB |
Output is correct |
58 |
Correct |
0 ms |
256 KB |
Output is correct |
59 |
Correct |
0 ms |
256 KB |
Output is correct |
60 |
Correct |
8 ms |
640 KB |
Output is correct |
61 |
Correct |
108 ms |
8184 KB |
Output is correct |
62 |
Correct |
0 ms |
256 KB |
Output is correct |
63 |
Correct |
0 ms |
256 KB |
Output is correct |
64 |
Correct |
0 ms |
256 KB |
Output is correct |
65 |
Correct |
63 ms |
5880 KB |
Output is correct |
66 |
Correct |
120 ms |
8184 KB |
Output is correct |
67 |
Correct |
116 ms |
8312 KB |
Output is correct |
68 |
Correct |
96 ms |
8276 KB |
Output is correct |
69 |
Correct |
98 ms |
12280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
13 ms |
1152 KB |
Output is correct |
7 |
Correct |
254 ms |
22096 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
10 ms |
1152 KB |
Output is correct |
13 |
Correct |
239 ms |
22136 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
640 KB |
Output is correct |
17 |
Correct |
110 ms |
8184 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
65 ms |
5880 KB |
Output is correct |
21 |
Correct |
247 ms |
22136 KB |
Output is correct |
22 |
Correct |
248 ms |
22264 KB |
Output is correct |
23 |
Correct |
255 ms |
22136 KB |
Output is correct |
24 |
Correct |
243 ms |
22136 KB |
Output is correct |
25 |
Correct |
95 ms |
8184 KB |
Output is correct |
26 |
Correct |
91 ms |
8312 KB |
Output is correct |
27 |
Correct |
256 ms |
22136 KB |
Output is correct |
28 |
Correct |
237 ms |
22136 KB |
Output is correct |
29 |
Correct |
0 ms |
256 KB |
Output is correct |
30 |
Correct |
0 ms |
256 KB |
Output is correct |
31 |
Correct |
0 ms |
256 KB |
Output is correct |
32 |
Correct |
0 ms |
256 KB |
Output is correct |
33 |
Correct |
0 ms |
256 KB |
Output is correct |
34 |
Correct |
1 ms |
256 KB |
Output is correct |
35 |
Correct |
0 ms |
256 KB |
Output is correct |
36 |
Correct |
10 ms |
1152 KB |
Output is correct |
37 |
Correct |
236 ms |
22136 KB |
Output is correct |
38 |
Correct |
0 ms |
256 KB |
Output is correct |
39 |
Correct |
0 ms |
256 KB |
Output is correct |
40 |
Correct |
11 ms |
1152 KB |
Output is correct |
41 |
Correct |
247 ms |
22136 KB |
Output is correct |
42 |
Correct |
0 ms |
256 KB |
Output is correct |
43 |
Correct |
0 ms |
256 KB |
Output is correct |
44 |
Correct |
5 ms |
640 KB |
Output is correct |
45 |
Correct |
111 ms |
8184 KB |
Output is correct |
46 |
Correct |
0 ms |
256 KB |
Output is correct |
47 |
Correct |
1 ms |
256 KB |
Output is correct |
48 |
Correct |
1 ms |
256 KB |
Output is correct |
49 |
Correct |
65 ms |
5880 KB |
Output is correct |
50 |
Correct |
247 ms |
22264 KB |
Output is correct |
51 |
Correct |
244 ms |
22136 KB |
Output is correct |
52 |
Correct |
254 ms |
22264 KB |
Output is correct |
53 |
Correct |
98 ms |
12152 KB |
Output is correct |
54 |
Correct |
95 ms |
8184 KB |
Output is correct |
55 |
Correct |
251 ms |
22112 KB |
Output is correct |
56 |
Correct |
258 ms |
22136 KB |
Output is correct |
57 |
Correct |
100 ms |
12152 KB |
Output is correct |
58 |
Correct |
1 ms |
256 KB |
Output is correct |
59 |
Correct |
0 ms |
256 KB |
Output is correct |
60 |
Correct |
0 ms |
256 KB |
Output is correct |
61 |
Correct |
62 ms |
5880 KB |
Output is correct |
62 |
Correct |
247 ms |
22264 KB |
Output is correct |
63 |
Correct |
241 ms |
22136 KB |
Output is correct |
64 |
Correct |
269 ms |
22304 KB |
Output is correct |
65 |
Correct |
1 ms |
384 KB |
Output is correct |
66 |
Correct |
62 ms |
5836 KB |
Output is correct |
67 |
Correct |
246 ms |
22136 KB |
Output is correct |
68 |
Correct |
246 ms |
22136 KB |
Output is correct |
69 |
Correct |
256 ms |
22136 KB |
Output is correct |
70 |
Correct |
1 ms |
256 KB |
Output is correct |
71 |
Correct |
0 ms |
256 KB |
Output is correct |
72 |
Correct |
0 ms |
256 KB |
Output is correct |
73 |
Correct |
64 ms |
5880 KB |
Output is correct |
74 |
Correct |
252 ms |
22172 KB |
Output is correct |
75 |
Correct |
248 ms |
22136 KB |
Output is correct |
76 |
Correct |
244 ms |
22136 KB |
Output is correct |
77 |
Correct |
238 ms |
22136 KB |
Output is correct |
78 |
Correct |
0 ms |
256 KB |
Output is correct |
79 |
Correct |
0 ms |
256 KB |
Output is correct |
80 |
Correct |
8 ms |
640 KB |
Output is correct |
81 |
Correct |
108 ms |
8184 KB |
Output is correct |
82 |
Correct |
0 ms |
256 KB |
Output is correct |
83 |
Correct |
0 ms |
256 KB |
Output is correct |
84 |
Correct |
0 ms |
256 KB |
Output is correct |
85 |
Correct |
63 ms |
5880 KB |
Output is correct |
86 |
Correct |
120 ms |
8184 KB |
Output is correct |
87 |
Correct |
116 ms |
8312 KB |
Output is correct |
88 |
Correct |
96 ms |
8276 KB |
Output is correct |
89 |
Correct |
98 ms |
12280 KB |
Output is correct |
90 |
Correct |
1 ms |
256 KB |
Output is correct |
91 |
Correct |
1 ms |
256 KB |
Output is correct |
92 |
Correct |
4 ms |
640 KB |
Output is correct |
93 |
Correct |
99 ms |
8312 KB |
Output is correct |
94 |
Correct |
1 ms |
256 KB |
Output is correct |
95 |
Correct |
0 ms |
256 KB |
Output is correct |
96 |
Correct |
0 ms |
256 KB |
Output is correct |
97 |
Correct |
23 ms |
2296 KB |
Output is correct |
98 |
Correct |
112 ms |
8312 KB |
Output is correct |
99 |
Correct |
107 ms |
8348 KB |
Output is correct |
100 |
Correct |
93 ms |
8184 KB |
Output is correct |
101 |
Correct |
91 ms |
8184 KB |
Output is correct |
102 |
Correct |
92 ms |
8184 KB |
Output is correct |
103 |
Correct |
94 ms |
8184 KB |
Output is correct |
104 |
Correct |
94 ms |
8312 KB |
Output is correct |
105 |
Correct |
107 ms |
8188 KB |
Output is correct |