#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
#define fi first
#define se second
const int INF = 1e9+1;
const int P = 1000000007;
const ll LLINF = (ll)1e18+1;
template <typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) { os << p.fi << " " << p.se; return os; }
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) { for(auto i : v) os << i << " "; os << "\n"; return os; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rnd(x, y) uniform_int_distribution<int>(x, y)(rng)
class UnionFind {
private:
vector<int> P;
public:
UnionFind(int n) {
P.resize(n);
for(int i = 0; i < n; i++) P[i] = i;
}
int fin(int x) {
if(x == P[x]) return x;
return P[x] = fin(P[x]);
}
void mer(int x, int y) {
x = fin(x), y = fin(y);
if(x != y) P[x] = y;
}
vector<vector<int>> classify() {
vector<vector<int>> ret(P.size());
for(int i = 0; i < P.size(); i++) ret[fin(i)].push_back(i);
return ret;
}
};
int construct(vector<vector<int>> p) {
int n = p.size();
UnionFind uf0(n), uf1(n);
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(i == j && p[i][j] != 1) return 0;
if(p[i][j] != p[j][i]) return 0;
if(p[i][j] == 3) return 0;
if(p[i][j]) uf0.mer(i, j);
if(p[i][j] == 1) uf1.mer(i, j);
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
int x = uf0.fin(i), y = uf0.fin(j);
if(x == y) {
if(p[i][j] == 0) return 0;
x = uf1.fin(i), y = uf1.fin(j);
if(x == y) {
if(p[i][j] != 1) return 0;
} else if(p[i][j] != 2) return 0;
} else if(p[i][j]) return 0;
}
}
vector<vector<int>> C = uf0.classify(), D = uf1.classify(), ret(n, vector<int>(n, 0));
//cout << " " << C << " " << D;
for(auto &v : C) {
if(v.empty()) continue;
for(auto &i : v) i = uf1.fin(i);
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
if(v.size() == 2) return 0;
if(v.size() == 1) continue;
ret[v.front()][v.back()] = ret[v.back()][v.front()] = 1;
for(int i = 1; i < v.size(); i++) ret[v[i-1]][v[i]] = ret[v[i]][v[i-1]] = 1;
}
for(int i = 0; i < n; i++)
for(int j = 0; j < D[i].size(); j++)
ret[D[i][j]][i] = ret[i][D[i][j]] = 1;
for(int i = 0; i < n; i++) ret[i][i] = 0;
build(ret);
return 1;
}
Compilation message
supertrees.cpp: In member function 'std::vector<std::vector<int> > UnionFind::classify()':
supertrees.cpp:40:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int i = 0; i < P.size(); i++) ret[fin(i)].push_back(i);
| ~~^~~~~~~~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:79:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for(int i = 1; i < v.size(); i++) ret[v[i-1]][v[i]] = ret[v[i]][v[i-1]] = 1;
| ~~^~~~~~~~~~
supertrees.cpp:82:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for(int j = 0; j < D[i].size(); j++)
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
8 ms |
1168 KB |
Output is correct |
7 |
Correct |
176 ms |
22024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
8 ms |
1168 KB |
Output is correct |
7 |
Correct |
176 ms |
22024 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
9 ms |
1108 KB |
Output is correct |
13 |
Correct |
166 ms |
22072 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
596 KB |
Output is correct |
17 |
Correct |
83 ms |
8196 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
43 ms |
5764 KB |
Output is correct |
21 |
Correct |
179 ms |
22148 KB |
Output is correct |
22 |
Correct |
176 ms |
22236 KB |
Output is correct |
23 |
Correct |
197 ms |
22212 KB |
Output is correct |
24 |
Correct |
173 ms |
22184 KB |
Output is correct |
25 |
Correct |
66 ms |
8212 KB |
Output is correct |
26 |
Correct |
88 ms |
8216 KB |
Output is correct |
27 |
Correct |
195 ms |
22240 KB |
Output is correct |
28 |
Correct |
167 ms |
22264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
7 ms |
1276 KB |
Output is correct |
9 |
Correct |
163 ms |
24072 KB |
Output is correct |
10 |
Correct |
0 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
8 ms |
1292 KB |
Output is correct |
13 |
Correct |
173 ms |
24116 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
304 KB |
Output is correct |
16 |
Correct |
4 ms |
596 KB |
Output is correct |
17 |
Correct |
79 ms |
10032 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
43 ms |
6260 KB |
Output is correct |
22 |
Correct |
172 ms |
24276 KB |
Output is correct |
23 |
Correct |
169 ms |
24060 KB |
Output is correct |
24 |
Correct |
184 ms |
24112 KB |
Output is correct |
25 |
Correct |
68 ms |
14200 KB |
Output is correct |
26 |
Correct |
70 ms |
10108 KB |
Output is correct |
27 |
Correct |
186 ms |
23988 KB |
Output is correct |
28 |
Correct |
187 ms |
24112 KB |
Output is correct |
29 |
Correct |
69 ms |
14204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
43 ms |
5736 KB |
Output is correct |
5 |
Correct |
190 ms |
22088 KB |
Output is correct |
6 |
Correct |
171 ms |
22024 KB |
Output is correct |
7 |
Correct |
206 ms |
22072 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
43 ms |
5800 KB |
Output is correct |
10 |
Correct |
168 ms |
22092 KB |
Output is correct |
11 |
Correct |
170 ms |
22076 KB |
Output is correct |
12 |
Correct |
188 ms |
22148 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
42 ms |
5796 KB |
Output is correct |
17 |
Correct |
171 ms |
22156 KB |
Output is correct |
18 |
Correct |
171 ms |
22080 KB |
Output is correct |
19 |
Correct |
171 ms |
22020 KB |
Output is correct |
20 |
Correct |
171 ms |
22156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
8 ms |
1168 KB |
Output is correct |
7 |
Correct |
176 ms |
22024 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
9 ms |
1108 KB |
Output is correct |
13 |
Correct |
166 ms |
22072 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
596 KB |
Output is correct |
17 |
Correct |
83 ms |
8196 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
43 ms |
5764 KB |
Output is correct |
21 |
Correct |
179 ms |
22148 KB |
Output is correct |
22 |
Correct |
176 ms |
22236 KB |
Output is correct |
23 |
Correct |
197 ms |
22212 KB |
Output is correct |
24 |
Correct |
173 ms |
22184 KB |
Output is correct |
25 |
Correct |
66 ms |
8212 KB |
Output is correct |
26 |
Correct |
88 ms |
8216 KB |
Output is correct |
27 |
Correct |
195 ms |
22240 KB |
Output is correct |
28 |
Correct |
167 ms |
22264 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
304 KB |
Output is correct |
36 |
Correct |
7 ms |
1276 KB |
Output is correct |
37 |
Correct |
163 ms |
24072 KB |
Output is correct |
38 |
Correct |
0 ms |
308 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
8 ms |
1292 KB |
Output is correct |
41 |
Correct |
173 ms |
24116 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
0 ms |
304 KB |
Output is correct |
44 |
Correct |
4 ms |
596 KB |
Output is correct |
45 |
Correct |
79 ms |
10032 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
43 ms |
6260 KB |
Output is correct |
50 |
Correct |
172 ms |
24276 KB |
Output is correct |
51 |
Correct |
169 ms |
24060 KB |
Output is correct |
52 |
Correct |
184 ms |
24112 KB |
Output is correct |
53 |
Correct |
68 ms |
14200 KB |
Output is correct |
54 |
Correct |
70 ms |
10108 KB |
Output is correct |
55 |
Correct |
186 ms |
23988 KB |
Output is correct |
56 |
Correct |
187 ms |
24112 KB |
Output is correct |
57 |
Correct |
69 ms |
14204 KB |
Output is correct |
58 |
Correct |
0 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
4 ms |
696 KB |
Output is correct |
61 |
Correct |
84 ms |
10112 KB |
Output is correct |
62 |
Correct |
0 ms |
212 KB |
Output is correct |
63 |
Correct |
1 ms |
212 KB |
Output is correct |
64 |
Correct |
1 ms |
212 KB |
Output is correct |
65 |
Correct |
43 ms |
6220 KB |
Output is correct |
66 |
Correct |
67 ms |
10032 KB |
Output is correct |
67 |
Correct |
66 ms |
10036 KB |
Output is correct |
68 |
Correct |
64 ms |
10104 KB |
Output is correct |
69 |
Correct |
76 ms |
14204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
8 ms |
1168 KB |
Output is correct |
7 |
Correct |
176 ms |
22024 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
9 ms |
1108 KB |
Output is correct |
13 |
Correct |
166 ms |
22072 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
596 KB |
Output is correct |
17 |
Correct |
83 ms |
8196 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
43 ms |
5764 KB |
Output is correct |
21 |
Correct |
179 ms |
22148 KB |
Output is correct |
22 |
Correct |
176 ms |
22236 KB |
Output is correct |
23 |
Correct |
197 ms |
22212 KB |
Output is correct |
24 |
Correct |
173 ms |
22184 KB |
Output is correct |
25 |
Correct |
66 ms |
8212 KB |
Output is correct |
26 |
Correct |
88 ms |
8216 KB |
Output is correct |
27 |
Correct |
195 ms |
22240 KB |
Output is correct |
28 |
Correct |
167 ms |
22264 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
304 KB |
Output is correct |
36 |
Correct |
7 ms |
1276 KB |
Output is correct |
37 |
Correct |
163 ms |
24072 KB |
Output is correct |
38 |
Correct |
0 ms |
308 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
8 ms |
1292 KB |
Output is correct |
41 |
Correct |
173 ms |
24116 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
0 ms |
304 KB |
Output is correct |
44 |
Correct |
4 ms |
596 KB |
Output is correct |
45 |
Correct |
79 ms |
10032 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
43 ms |
6260 KB |
Output is correct |
50 |
Correct |
172 ms |
24276 KB |
Output is correct |
51 |
Correct |
169 ms |
24060 KB |
Output is correct |
52 |
Correct |
184 ms |
24112 KB |
Output is correct |
53 |
Correct |
68 ms |
14200 KB |
Output is correct |
54 |
Correct |
70 ms |
10108 KB |
Output is correct |
55 |
Correct |
186 ms |
23988 KB |
Output is correct |
56 |
Correct |
187 ms |
24112 KB |
Output is correct |
57 |
Correct |
69 ms |
14204 KB |
Output is correct |
58 |
Correct |
0 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
0 ms |
212 KB |
Output is correct |
61 |
Correct |
43 ms |
5736 KB |
Output is correct |
62 |
Correct |
190 ms |
22088 KB |
Output is correct |
63 |
Correct |
171 ms |
22024 KB |
Output is correct |
64 |
Correct |
206 ms |
22072 KB |
Output is correct |
65 |
Correct |
1 ms |
212 KB |
Output is correct |
66 |
Correct |
43 ms |
5800 KB |
Output is correct |
67 |
Correct |
168 ms |
22092 KB |
Output is correct |
68 |
Correct |
170 ms |
22076 KB |
Output is correct |
69 |
Correct |
188 ms |
22148 KB |
Output is correct |
70 |
Correct |
0 ms |
212 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
42 ms |
5796 KB |
Output is correct |
74 |
Correct |
171 ms |
22156 KB |
Output is correct |
75 |
Correct |
171 ms |
22080 KB |
Output is correct |
76 |
Correct |
171 ms |
22020 KB |
Output is correct |
77 |
Correct |
171 ms |
22156 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
212 KB |
Output is correct |
80 |
Correct |
4 ms |
696 KB |
Output is correct |
81 |
Correct |
84 ms |
10112 KB |
Output is correct |
82 |
Correct |
0 ms |
212 KB |
Output is correct |
83 |
Correct |
1 ms |
212 KB |
Output is correct |
84 |
Correct |
1 ms |
212 KB |
Output is correct |
85 |
Correct |
43 ms |
6220 KB |
Output is correct |
86 |
Correct |
67 ms |
10032 KB |
Output is correct |
87 |
Correct |
66 ms |
10036 KB |
Output is correct |
88 |
Correct |
64 ms |
10104 KB |
Output is correct |
89 |
Correct |
76 ms |
14204 KB |
Output is correct |
90 |
Correct |
0 ms |
212 KB |
Output is correct |
91 |
Correct |
1 ms |
212 KB |
Output is correct |
92 |
Correct |
3 ms |
596 KB |
Output is correct |
93 |
Correct |
67 ms |
10104 KB |
Output is correct |
94 |
Correct |
1 ms |
212 KB |
Output is correct |
95 |
Correct |
0 ms |
300 KB |
Output is correct |
96 |
Correct |
1 ms |
212 KB |
Output is correct |
97 |
Correct |
16 ms |
2692 KB |
Output is correct |
98 |
Correct |
63 ms |
10060 KB |
Output is correct |
99 |
Correct |
63 ms |
10032 KB |
Output is correct |
100 |
Correct |
64 ms |
10032 KB |
Output is correct |
101 |
Correct |
62 ms |
10032 KB |
Output is correct |
102 |
Correct |
67 ms |
10032 KB |
Output is correct |
103 |
Correct |
70 ms |
10044 KB |
Output is correct |
104 |
Correct |
92 ms |
10104 KB |
Output is correct |
105 |
Correct |
70 ms |
10116 KB |
Output is correct |