#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
using ll = long long;
using ld = long double;
#define fi first
#define se second
#define vi vector<int>
#define vll vector<long long>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
inline int rand(int l,int r){return uniform_int_distribution<int>(l, r)(rng);}
inline ll rand(ll l,ll r){return uniform_int_distribution<ll>(l, r)(rng64);}
#ifdef DEBUG
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
const int MAXN = 1e3 + 7;
int vis[MAXN];
int p[MAXN][MAXN];
vi g[MAXN];
int n;
vector<vi> ans;
vi akt;
map<int, int> cnt;
void getOthers(int v){
queue<int> q;
q.push(v);
vis[v] = 1;
while(sz(q)){
v = q.front();
q.pop();
cnt[v] = 1;
akt.pb(v);
for(auto u : g[v]){
if(!vis[u]){
vis[u] = 1;
q.push(u);
}
}
}
}
/*void build(vector<vi> xd){
for(int i = 0; i < sz(xd); i++){
for(auto ele : xd[i]){
cout << ele << ' ';
}
cout << '\n';
}
}*/
int construct(vector<vi> given) {
n = sz(given);
ans.resize(n);
for(int i = 0; i < n; i++){
ans[i].assign(n, 0);
for(int j = 0; j < n; j++){
p[i][j] = given[i][j];
if(p[i][j] > 0 && i != j){
g[i].pb(j);
}
}
}
for(int i = 0; i < n; i++){
if(!vis[i]){
akt.clear();
cnt.clear();
getOthers(i);
int mx = 0;
int was[4] = {0, 0, 0, 0};
for(auto ele : akt){
for(int x = 0; x < n; x++){
if(x != ele && p[ele][x] == 0 && cnt[x]){
return 0;
}
if(cnt[x]){
was[p[ele][x]]++;
mx = max(mx, p[ele][x]);
}
}
}
if(mx == 1){
for(int x = 0; x < sz(akt) - 1; x++){
ans[akt[x]][akt[x + 1]] = 1;
ans[akt[x + 1]][akt[x]] = 1;
}
continue;
}
if(was[2] && was[3]){
return 0;
}
map<int, int> f;
vector<vi> groups;
int k = 0;
for(auto ele : akt){
if(f[ele]){
continue;
}
groups.pb({ele});
queue<int> q;
q.push(ele);
f[ele] = 1;
while(sz(q)){
int v = q.front();
q.pop();
for(auto u : g[v]){
if(!f[u] && p[ele][u] == 1){
f[u] = 1;
groups[k].pb(u);
q.push(u);
}
}
}
for(int j = 0; j < sz(groups[k]); j++){
//cout << groups[k][j] << ' ';
if(j + 1 < sz(groups[k])){
ans[groups[k][j]][groups[k][j + 1]] = 1;
ans[groups[k][j + 1]][groups[k][j]] = 1;
}
for(int x = j + 1; x < sz(groups[k]); x++){
if(p[groups[k][x]][groups[k][j]] != 1){
return 0;
}
}
}
//cout << '\n';
k++;
}
if(was[2]){
if(k == 2){
return 0;
}
for(int j = 0; j + 1 < k; j++){
ans[groups[j][0]][groups[j + 1][0]] = 1;
ans[groups[j + 1][0]][groups[j][0]] = 1;
}
ans[groups[k - 1][0]][groups[0][0]] = 1;
ans[groups[0][0]][groups[k - 1][0]] = 1;
}else{
if(k <= 3){
return 0;
}
for(int j = 0; j + 1 < k; j++){
ans[groups[j][0]][groups[j + 1][0]] = 1;
ans[groups[j + 1][0]][groups[j][0]] = 1;
}
ans[groups[k - 1][0]][groups[0][0]] = 1;
ans[groups[0][0]][groups[k - 1][0]] = 1;
ans[groups[k - 1][0]][groups[1][0]] = 1;
ans[groups[1][0]][groups[k - 1][0]] = 1;
}
}
}
build(ans);
return 1;
}
/*int main(){
cout << construct({{1, 1, 2, 2}, {1, 1, 2, 2}, {2, 2, 1, 2}, {2, 2, 2, 1}}) << '\n';
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
2172 KB |
Output is correct |
7 |
Correct |
127 ms |
30000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
2172 KB |
Output is correct |
7 |
Correct |
127 ms |
30000 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
7 ms |
2140 KB |
Output is correct |
13 |
Correct |
179 ms |
26052 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
428 KB |
Output is correct |
16 |
Correct |
3 ms |
1628 KB |
Output is correct |
17 |
Correct |
63 ms |
18496 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
38 ms |
7832 KB |
Output is correct |
21 |
Correct |
140 ms |
26668 KB |
Output is correct |
22 |
Correct |
160 ms |
26196 KB |
Output is correct |
23 |
Correct |
141 ms |
27984 KB |
Output is correct |
24 |
Correct |
166 ms |
26196 KB |
Output is correct |
25 |
Correct |
64 ms |
16724 KB |
Output is correct |
26 |
Correct |
53 ms |
16328 KB |
Output is correct |
27 |
Correct |
134 ms |
29008 KB |
Output is correct |
28 |
Correct |
208 ms |
26196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
7 ms |
2140 KB |
Output is correct |
9 |
Correct |
168 ms |
26176 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
6 ms |
2140 KB |
Output is correct |
13 |
Correct |
184 ms |
30036 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
1628 KB |
Output is correct |
17 |
Correct |
70 ms |
18480 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
36 ms |
7796 KB |
Output is correct |
22 |
Correct |
154 ms |
26684 KB |
Output is correct |
23 |
Correct |
149 ms |
26192 KB |
Output is correct |
24 |
Correct |
149 ms |
29268 KB |
Output is correct |
25 |
Correct |
81 ms |
16196 KB |
Output is correct |
26 |
Correct |
58 ms |
16724 KB |
Output is correct |
27 |
Correct |
145 ms |
26172 KB |
Output is correct |
28 |
Correct |
150 ms |
28208 KB |
Output is correct |
29 |
Correct |
82 ms |
16212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
36 ms |
7892 KB |
Output is correct |
5 |
Correct |
146 ms |
26536 KB |
Output is correct |
6 |
Correct |
154 ms |
26196 KB |
Output is correct |
7 |
Correct |
134 ms |
27984 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
39 ms |
7804 KB |
Output is correct |
10 |
Correct |
142 ms |
26708 KB |
Output is correct |
11 |
Correct |
160 ms |
26096 KB |
Output is correct |
12 |
Correct |
143 ms |
29268 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
37 ms |
7796 KB |
Output is correct |
17 |
Correct |
141 ms |
26708 KB |
Output is correct |
18 |
Correct |
142 ms |
26708 KB |
Output is correct |
19 |
Correct |
145 ms |
26192 KB |
Output is correct |
20 |
Correct |
172 ms |
26192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
2172 KB |
Output is correct |
7 |
Correct |
127 ms |
30000 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
7 ms |
2140 KB |
Output is correct |
13 |
Correct |
179 ms |
26052 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
428 KB |
Output is correct |
16 |
Correct |
3 ms |
1628 KB |
Output is correct |
17 |
Correct |
63 ms |
18496 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
38 ms |
7832 KB |
Output is correct |
21 |
Correct |
140 ms |
26668 KB |
Output is correct |
22 |
Correct |
160 ms |
26196 KB |
Output is correct |
23 |
Correct |
141 ms |
27984 KB |
Output is correct |
24 |
Correct |
166 ms |
26196 KB |
Output is correct |
25 |
Correct |
64 ms |
16724 KB |
Output is correct |
26 |
Correct |
53 ms |
16328 KB |
Output is correct |
27 |
Correct |
134 ms |
29008 KB |
Output is correct |
28 |
Correct |
208 ms |
26196 KB |
Output is correct |
29 |
Correct |
0 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
600 KB |
Output is correct |
36 |
Correct |
7 ms |
2140 KB |
Output is correct |
37 |
Correct |
168 ms |
26176 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
6 ms |
2140 KB |
Output is correct |
41 |
Correct |
184 ms |
30036 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
4 ms |
1628 KB |
Output is correct |
45 |
Correct |
70 ms |
18480 KB |
Output is correct |
46 |
Correct |
0 ms |
344 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
36 ms |
7796 KB |
Output is correct |
50 |
Correct |
154 ms |
26684 KB |
Output is correct |
51 |
Correct |
149 ms |
26192 KB |
Output is correct |
52 |
Correct |
149 ms |
29268 KB |
Output is correct |
53 |
Correct |
81 ms |
16196 KB |
Output is correct |
54 |
Correct |
58 ms |
16724 KB |
Output is correct |
55 |
Correct |
145 ms |
26172 KB |
Output is correct |
56 |
Correct |
150 ms |
28208 KB |
Output is correct |
57 |
Correct |
82 ms |
16212 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
5 ms |
1916 KB |
Output is correct |
61 |
Correct |
61 ms |
20268 KB |
Output is correct |
62 |
Correct |
1 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
1 ms |
344 KB |
Output is correct |
65 |
Correct |
37 ms |
7788 KB |
Output is correct |
66 |
Correct |
54 ms |
16688 KB |
Output is correct |
67 |
Correct |
65 ms |
16724 KB |
Output is correct |
68 |
Correct |
69 ms |
16472 KB |
Output is correct |
69 |
Correct |
55 ms |
16212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
2172 KB |
Output is correct |
7 |
Correct |
127 ms |
30000 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
7 ms |
2140 KB |
Output is correct |
13 |
Correct |
179 ms |
26052 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
428 KB |
Output is correct |
16 |
Correct |
3 ms |
1628 KB |
Output is correct |
17 |
Correct |
63 ms |
18496 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
38 ms |
7832 KB |
Output is correct |
21 |
Correct |
140 ms |
26668 KB |
Output is correct |
22 |
Correct |
160 ms |
26196 KB |
Output is correct |
23 |
Correct |
141 ms |
27984 KB |
Output is correct |
24 |
Correct |
166 ms |
26196 KB |
Output is correct |
25 |
Correct |
64 ms |
16724 KB |
Output is correct |
26 |
Correct |
53 ms |
16328 KB |
Output is correct |
27 |
Correct |
134 ms |
29008 KB |
Output is correct |
28 |
Correct |
208 ms |
26196 KB |
Output is correct |
29 |
Correct |
0 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
600 KB |
Output is correct |
36 |
Correct |
7 ms |
2140 KB |
Output is correct |
37 |
Correct |
168 ms |
26176 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
6 ms |
2140 KB |
Output is correct |
41 |
Correct |
184 ms |
30036 KB |
Output is correct |
42 |
Correct |
0 ms |
344 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
4 ms |
1628 KB |
Output is correct |
45 |
Correct |
70 ms |
18480 KB |
Output is correct |
46 |
Correct |
0 ms |
344 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
36 ms |
7796 KB |
Output is correct |
50 |
Correct |
154 ms |
26684 KB |
Output is correct |
51 |
Correct |
149 ms |
26192 KB |
Output is correct |
52 |
Correct |
149 ms |
29268 KB |
Output is correct |
53 |
Correct |
81 ms |
16196 KB |
Output is correct |
54 |
Correct |
58 ms |
16724 KB |
Output is correct |
55 |
Correct |
145 ms |
26172 KB |
Output is correct |
56 |
Correct |
150 ms |
28208 KB |
Output is correct |
57 |
Correct |
82 ms |
16212 KB |
Output is correct |
58 |
Correct |
0 ms |
344 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
36 ms |
7892 KB |
Output is correct |
62 |
Correct |
146 ms |
26536 KB |
Output is correct |
63 |
Correct |
154 ms |
26196 KB |
Output is correct |
64 |
Correct |
134 ms |
27984 KB |
Output is correct |
65 |
Correct |
1 ms |
344 KB |
Output is correct |
66 |
Correct |
39 ms |
7804 KB |
Output is correct |
67 |
Correct |
142 ms |
26708 KB |
Output is correct |
68 |
Correct |
160 ms |
26096 KB |
Output is correct |
69 |
Correct |
143 ms |
29268 KB |
Output is correct |
70 |
Correct |
1 ms |
344 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
37 ms |
7796 KB |
Output is correct |
74 |
Correct |
141 ms |
26708 KB |
Output is correct |
75 |
Correct |
142 ms |
26708 KB |
Output is correct |
76 |
Correct |
145 ms |
26192 KB |
Output is correct |
77 |
Correct |
172 ms |
26192 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
5 ms |
1916 KB |
Output is correct |
81 |
Correct |
61 ms |
20268 KB |
Output is correct |
82 |
Correct |
1 ms |
348 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |
84 |
Correct |
1 ms |
344 KB |
Output is correct |
85 |
Correct |
37 ms |
7788 KB |
Output is correct |
86 |
Correct |
54 ms |
16688 KB |
Output is correct |
87 |
Correct |
65 ms |
16724 KB |
Output is correct |
88 |
Correct |
69 ms |
16472 KB |
Output is correct |
89 |
Correct |
55 ms |
16212 KB |
Output is correct |
90 |
Correct |
0 ms |
344 KB |
Output is correct |
91 |
Correct |
0 ms |
344 KB |
Output is correct |
92 |
Correct |
3 ms |
1884 KB |
Output is correct |
93 |
Correct |
58 ms |
20288 KB |
Output is correct |
94 |
Correct |
0 ms |
344 KB |
Output is correct |
95 |
Correct |
0 ms |
344 KB |
Output is correct |
96 |
Correct |
0 ms |
348 KB |
Output is correct |
97 |
Correct |
18 ms |
5468 KB |
Output is correct |
98 |
Correct |
54 ms |
16728 KB |
Output is correct |
99 |
Correct |
94 ms |
16724 KB |
Output is correct |
100 |
Correct |
67 ms |
16464 KB |
Output is correct |
101 |
Correct |
69 ms |
16296 KB |
Output is correct |
102 |
Incorrect |
145 ms |
30040 KB |
Answer gives possible 1 while actual possible 0 |
103 |
Halted |
0 ms |
0 KB |
- |