// goofy ahh
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
#define OPTM ios_base::sync_with_stdio(0); cin.tie(0);
#define INF int(1e9+7)
#define ln '\n'
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define FOR(i,s,n) for (int i = s; i < n; i++)
#define FORR(i,n,s) for (int i = n; i > s; i--)
#define FORX(u, arr) for (auto u : arr)
#define PB push_back
#define in(v,x) (v.find(x) != v.end())
#define F first
#define S second
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define UM unordered_map
#define US unordered_set
#define PQ priority_queue
#define ALL(v) v.begin(), v.end()
const ll LLINF = 1e18+1;
const int MAXN = 1e3+5;
int n,wn,ma;
int a[MAXN][MAXN], d[MAXN], comp[MAXN];
vector<int> v[MAXN], v1[MAXN], goofy, w;
vector<vector<int>> b;
bool vis[MAXN], z[MAXN];
bool bfs(int start) {
queue<int> q;
FOR(i,0,wn) d[w[i]] = INF;
d[start] = 0;
q.push(start);
while (!q.empty()) {
int s = q.front();
q.pop();
FORX(u,v[s]) {
if (d[s] == 1 && d[u] == 1) {
if (ma == 2) {
goofy = {start,s,u};
return 1;
}
else {
FORX(y,v[u]) {
if (y == s || y == start) continue;
if (a[y][s] == 3 && d[y] == 1) {
goofy = {start,s,u,y};
return 1;
}
}
}
}
if (d[s]+1 < 2 && d[s]+1 < d[u]) {
d[u] = d[s]+1;
q.push(u);
}
}
}
return 0;
}
void dfs(int s) {
if (vis[s]) return;
vis[s] = 1;
w.PB(s);
FORX(u,v1[s]) dfs(u);
}
bool solve() {
goofy.clear();
FOR(i,0,wn) if (bfs(w[i])) break;
if (goofy.empty()) {
FOR(i,0,wn) FOR(j,i+1,wn) if (a[w[i]][w[j]] == ma) return 0;
FOR(i,0,wn-1) b[w[i]][w[i+1]] = b[w[i+1]][w[i]] = 1;
return 1;
}
FORX(u,goofy) z[u] = 1;
FOR(i,0,wn) {
if (z[w[i]]) continue;
bool ok = 1;
FORX(u,goofy) if (a[w[i]][u] != ma) ok = 0;
if (ok) z[w[i]] = 1, goofy.PB(w[i]);
}
// FORX(u,goofy) cout << u << " ";
// cout << ln;
int gn = goofy.size();
FOR(i,0,wn) comp[w[i]] = -1;
FORX(u,goofy) comp[u] = u;
FOR(i,0,wn) {
if (z[w[i]]) continue;
FOR(j,0,wn) {
if (i == j) continue;
if (a[w[i]][w[j]] == 1 && z[w[j]]) {
comp[w[i]] = w[j];
break;
}
}
}
FOR(i,0,wn) {
FOR(j,0,wn) {
if (i == j) continue;
if (a[w[i]][w[j]] == ma && comp[w[i]] == comp[w[j]]) return 0;
if (a[w[i]][w[j]] == 1 && comp[w[i]] != comp[w[j]]) return 0;
}
}
FOR(i,0,gn) b[goofy[i]][goofy[(i+1)%gn]] = b[goofy[(i+1)%gn]][goofy[i]] = 1;
if (ma == 3) b[goofy[0]][goofy[2]] = b[goofy[2]][goofy[0]] = 1;
FOR(i,0,wn) if (!z[w[i]]) b[w[i]][comp[w[i]]] = b[comp[w[i]]][w[i]] = 1;
return 1;
}
bool precheck() {
wn = w.size();
FOR(i,0,wn) {
FOR(j,0,wn) {
if (a[w[i]][w[j]] == 0) return 0;
}
}
bool xd[3] = {0,0,0};
FOR(i,0,wn) {
FOR(j,0,wn) xd[a[w[i]][w[j]]-1] = 1;
}
if (xd[1] && xd[2]) return 0;
ma = xd[2] ? 3 : 2;
FOR(i,0,wn) FOR(j,0,wn) if (a[w[i]][w[j]] == ma) v[w[i]].PB(w[j]), v[w[j]].PB(w[i]);
return 1;
}
void reset() {
FORX(u,w) v[u].clear();
w.clear();
}
int construct(vector<vector<int>> P) {
n = P.size();
b.resize(n,vector<int>(n));
FOR(i,0,n) FOR(j,0,n) a[i][j] = P[i][j];
FOR(i,0,n) FOR(j,i+1,n) if (a[i][j]) v1[i].PB(j), v1[j].PB(i);
FOR(i,0,n) FOR(j,0,n) if (a[i][j] == 3) {build(b); return 1;}
FOR(i,0,n) {
if (vis[i]) continue;
reset();
dfs(i);
if (!precheck()) return 0;
if (!solve()) return 0;
}
build(b);
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
9 ms |
2260 KB |
Output is correct |
7 |
Correct |
161 ms |
30196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
9 ms |
2260 KB |
Output is correct |
7 |
Correct |
161 ms |
30196 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
7 ms |
2004 KB |
Output is correct |
13 |
Correct |
152 ms |
26044 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
1748 KB |
Output is correct |
17 |
Correct |
71 ms |
19020 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
41 ms |
7840 KB |
Output is correct |
21 |
Correct |
152 ms |
26572 KB |
Output is correct |
22 |
Correct |
149 ms |
26060 KB |
Output is correct |
23 |
Correct |
160 ms |
28104 KB |
Output is correct |
24 |
Correct |
151 ms |
26044 KB |
Output is correct |
25 |
Correct |
63 ms |
16592 KB |
Output is correct |
26 |
Correct |
61 ms |
16072 KB |
Output is correct |
27 |
Correct |
161 ms |
29120 KB |
Output is correct |
28 |
Correct |
153 ms |
26048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
7 ms |
2004 KB |
Output is correct |
9 |
Correct |
149 ms |
25932 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
7 ms |
2644 KB |
Output is correct |
13 |
Correct |
169 ms |
38152 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
1748 KB |
Output is correct |
17 |
Correct |
82 ms |
18880 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
40 ms |
7876 KB |
Output is correct |
22 |
Correct |
152 ms |
27580 KB |
Output is correct |
23 |
Correct |
149 ms |
26044 KB |
Output is correct |
24 |
Correct |
167 ms |
35264 KB |
Output is correct |
25 |
Correct |
63 ms |
16060 KB |
Output is correct |
26 |
Correct |
63 ms |
16832 KB |
Output is correct |
27 |
Correct |
154 ms |
26096 KB |
Output is correct |
28 |
Correct |
166 ms |
32192 KB |
Output is correct |
29 |
Correct |
62 ms |
16060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
39 ms |
7780 KB |
Output is correct |
5 |
Correct |
152 ms |
26572 KB |
Output is correct |
6 |
Correct |
153 ms |
26048 KB |
Output is correct |
7 |
Correct |
160 ms |
28096 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
42 ms |
7876 KB |
Output is correct |
10 |
Correct |
155 ms |
27584 KB |
Output is correct |
11 |
Correct |
149 ms |
26044 KB |
Output is correct |
12 |
Correct |
164 ms |
35272 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
39 ms |
7804 KB |
Output is correct |
17 |
Correct |
152 ms |
27548 KB |
Output is correct |
18 |
Correct |
157 ms |
27576 KB |
Output is correct |
19 |
Correct |
161 ms |
26564 KB |
Output is correct |
20 |
Correct |
149 ms |
25996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
9 ms |
2260 KB |
Output is correct |
7 |
Correct |
161 ms |
30196 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
7 ms |
2004 KB |
Output is correct |
13 |
Correct |
152 ms |
26044 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
1748 KB |
Output is correct |
17 |
Correct |
71 ms |
19020 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
41 ms |
7840 KB |
Output is correct |
21 |
Correct |
152 ms |
26572 KB |
Output is correct |
22 |
Correct |
149 ms |
26060 KB |
Output is correct |
23 |
Correct |
160 ms |
28104 KB |
Output is correct |
24 |
Correct |
151 ms |
26044 KB |
Output is correct |
25 |
Correct |
63 ms |
16592 KB |
Output is correct |
26 |
Correct |
61 ms |
16072 KB |
Output is correct |
27 |
Correct |
161 ms |
29120 KB |
Output is correct |
28 |
Correct |
153 ms |
26048 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
7 ms |
2004 KB |
Output is correct |
37 |
Correct |
149 ms |
25932 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
7 ms |
2644 KB |
Output is correct |
41 |
Correct |
169 ms |
38152 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
0 ms |
340 KB |
Output is correct |
44 |
Correct |
3 ms |
1748 KB |
Output is correct |
45 |
Correct |
82 ms |
18880 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
0 ms |
340 KB |
Output is correct |
49 |
Correct |
40 ms |
7876 KB |
Output is correct |
50 |
Correct |
152 ms |
27580 KB |
Output is correct |
51 |
Correct |
149 ms |
26044 KB |
Output is correct |
52 |
Correct |
167 ms |
35264 KB |
Output is correct |
53 |
Correct |
63 ms |
16060 KB |
Output is correct |
54 |
Correct |
63 ms |
16832 KB |
Output is correct |
55 |
Correct |
154 ms |
26096 KB |
Output is correct |
56 |
Correct |
166 ms |
32192 KB |
Output is correct |
57 |
Correct |
62 ms |
16060 KB |
Output is correct |
58 |
Correct |
0 ms |
340 KB |
Output is correct |
59 |
Correct |
0 ms |
340 KB |
Output is correct |
60 |
Correct |
3 ms |
1876 KB |
Output is correct |
61 |
Correct |
70 ms |
20688 KB |
Output is correct |
62 |
Correct |
1 ms |
340 KB |
Output is correct |
63 |
Correct |
0 ms |
340 KB |
Output is correct |
64 |
Correct |
0 ms |
340 KB |
Output is correct |
65 |
Correct |
39 ms |
7852 KB |
Output is correct |
66 |
Correct |
62 ms |
16840 KB |
Output is correct |
67 |
Correct |
62 ms |
17088 KB |
Output is correct |
68 |
Correct |
61 ms |
16332 KB |
Output is correct |
69 |
Correct |
61 ms |
16068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
9 ms |
2260 KB |
Output is correct |
7 |
Correct |
161 ms |
30196 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
7 ms |
2004 KB |
Output is correct |
13 |
Correct |
152 ms |
26044 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
1748 KB |
Output is correct |
17 |
Correct |
71 ms |
19020 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
41 ms |
7840 KB |
Output is correct |
21 |
Correct |
152 ms |
26572 KB |
Output is correct |
22 |
Correct |
149 ms |
26060 KB |
Output is correct |
23 |
Correct |
160 ms |
28104 KB |
Output is correct |
24 |
Correct |
151 ms |
26044 KB |
Output is correct |
25 |
Correct |
63 ms |
16592 KB |
Output is correct |
26 |
Correct |
61 ms |
16072 KB |
Output is correct |
27 |
Correct |
161 ms |
29120 KB |
Output is correct |
28 |
Correct |
153 ms |
26048 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
7 ms |
2004 KB |
Output is correct |
37 |
Correct |
149 ms |
25932 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
7 ms |
2644 KB |
Output is correct |
41 |
Correct |
169 ms |
38152 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
0 ms |
340 KB |
Output is correct |
44 |
Correct |
3 ms |
1748 KB |
Output is correct |
45 |
Correct |
82 ms |
18880 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
0 ms |
340 KB |
Output is correct |
49 |
Correct |
40 ms |
7876 KB |
Output is correct |
50 |
Correct |
152 ms |
27580 KB |
Output is correct |
51 |
Correct |
149 ms |
26044 KB |
Output is correct |
52 |
Correct |
167 ms |
35264 KB |
Output is correct |
53 |
Correct |
63 ms |
16060 KB |
Output is correct |
54 |
Correct |
63 ms |
16832 KB |
Output is correct |
55 |
Correct |
154 ms |
26096 KB |
Output is correct |
56 |
Correct |
166 ms |
32192 KB |
Output is correct |
57 |
Correct |
62 ms |
16060 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
0 ms |
340 KB |
Output is correct |
60 |
Correct |
0 ms |
340 KB |
Output is correct |
61 |
Correct |
39 ms |
7780 KB |
Output is correct |
62 |
Correct |
152 ms |
26572 KB |
Output is correct |
63 |
Correct |
153 ms |
26048 KB |
Output is correct |
64 |
Correct |
160 ms |
28096 KB |
Output is correct |
65 |
Correct |
0 ms |
340 KB |
Output is correct |
66 |
Correct |
42 ms |
7876 KB |
Output is correct |
67 |
Correct |
155 ms |
27584 KB |
Output is correct |
68 |
Correct |
149 ms |
26044 KB |
Output is correct |
69 |
Correct |
164 ms |
35272 KB |
Output is correct |
70 |
Correct |
0 ms |
340 KB |
Output is correct |
71 |
Correct |
0 ms |
340 KB |
Output is correct |
72 |
Correct |
0 ms |
340 KB |
Output is correct |
73 |
Correct |
39 ms |
7804 KB |
Output is correct |
74 |
Correct |
152 ms |
27548 KB |
Output is correct |
75 |
Correct |
157 ms |
27576 KB |
Output is correct |
76 |
Correct |
161 ms |
26564 KB |
Output is correct |
77 |
Correct |
149 ms |
25996 KB |
Output is correct |
78 |
Correct |
0 ms |
340 KB |
Output is correct |
79 |
Correct |
0 ms |
340 KB |
Output is correct |
80 |
Correct |
3 ms |
1876 KB |
Output is correct |
81 |
Correct |
70 ms |
20688 KB |
Output is correct |
82 |
Correct |
1 ms |
340 KB |
Output is correct |
83 |
Correct |
0 ms |
340 KB |
Output is correct |
84 |
Correct |
0 ms |
340 KB |
Output is correct |
85 |
Correct |
39 ms |
7852 KB |
Output is correct |
86 |
Correct |
62 ms |
16840 KB |
Output is correct |
87 |
Correct |
62 ms |
17088 KB |
Output is correct |
88 |
Correct |
61 ms |
16332 KB |
Output is correct |
89 |
Correct |
61 ms |
16068 KB |
Output is correct |
90 |
Incorrect |
0 ms |
340 KB |
Answer gives possible 1 while actual possible 0 |
91 |
Halted |
0 ms |
0 KB |
- |