#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define endlfl '\n' << flush
#define quit(x) return (void)(cout << x << endl)
template<typename T> void read(T& x) { cin >> x;}
template<typename T1, typename T2> void read(pair<T1, T2>& p) { read(p.first), read(p.second);}
template<typename T> void read(vector<T>& v) { for (auto& x : v) read(x); }
template<typename T1, typename T2> void read(T1& x, T2& y) { read(x), read(y); }
template<typename T1, typename T2, typename T3> void read(T1& x, T2& y, T3& z) { read(x), read(y), read(z); }
template<typename T1, typename T2, typename T3, typename T4> void read(T1& x, T2& y, T3& z, T4& zz) { read(x), read(y), read(z), read(zz); }
template<typename T> void print(vector<T>& v) { for (auto& x : v) cout << x << ' '; cout << endl; }
#define sz(x) (int)(x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define cmin(a, b) a = min(a, b)
#define cmax(a, b) a = max(a, b)
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define QYES quit("YES")
#define QNO quit("NO")
// #define int long long
#define double long double
const int inf = numeric_limits<int>::max() / 4;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9+7, LOG=25;
int n, cnt;
vector<int> par, sz;
vector<vector<int>> contains;
vector<set<int>> adj;
set<int> critical;
int only=-1;
int find(int x) {
if (x==par[x]) return x;
return par[x] = find(par[x]);
}
bool same(int a, int b) {
return find(a)==find(b);
}
void merge(int a, int b) {
a = find(a), b = find(b);
if (a==b) return;
if (sz[a]<sz[b]) swap(a, b);
par[b] = a;
sz[a] += sz[b];
for (int i=1; i<4; i++) {
contains[a][i] += contains[b][i];
}
}
vector<bool> visited;
bool dfs(int x, int par=-1) {
visited[x]=1;
int cnt=0;
for (auto u: adj[x]) {
if (u==par) continue;
if (cnt==1 || visited[u]) {
return 0;
}
if (!dfs(u, x)) return 0;
cnt++;
}
return 1;
}
bool works(int a, bool real=1, int past=-1) {
if (real) {
for (auto u: adj[a]) {
if (!works(u, 0, a)) return 0;
}
return 1;
}
if (sz(adj[a])>2) return 0;
visited.assign(n, 0);
visited[a]=1;
if (sz(adj[a])>1) return (dfs(*adj[a].begin(), a) && dfs(*next(adj[a].begin()), a));
else if (sz(adj[a])) return dfs(*adj[a].begin(), a);
return 1;
}
void Init(int N_) {
n = N_;
cnt=n;
par.resize(n), sz.resize(n, 1), contains.resize(n, vector<int>(4, 0));
iota(all(par), 0);
adj.resize(n);
for (int i=0; i<n; i++) critical.insert(i);
}
void Link(int a, int b) {
if (critical.empty() || a==only || b==only) return;
if (a>b) swap(a, b);
if (same(a, b) || sz(adj[a])>=2 || sz(adj[b])>=2) {
if (only!=-1) {
critical.clear();
return;
}
if (sz(adj[a])>=3 || sz(adj[b])>=3) {
int cnta=critical.count(a), cntb=critical.count(b);
critical.clear();
if (sz(adj[b])<3 && cnta) critical.insert(a);
else if (sz(adj[a])<3 && cntb) critical.insert(b);
if (!sz(critical)) return;
only=*critical.begin();
par.clear(), sz.clear(); par.resize(n), sz.resize(n, 1); iota(all(par), 0);
for (int i=0; i<n; i++) {
if (i==only) continue;
for (auto u: adj[i]) {
if (u==only) continue;
merge(i, u);
}
if (adj[i].count(only)) adj[i].erase(only);
}
return;
}
contains[find(a)][min(sz(adj[a]), 3)]--, contains[find(b)][min(sz(adj[b]), 3)]--;
contains[find(a)][min(sz(adj[a])+1, 3)]++, contains[find(b)][min(sz(adj[b])+1, 3)]++;
if (sz(critical)==n) {
critical.clear();
for (int i=0; i<n; i++) {
if (i==a || i==b || (find(i)==find(a) && sz(adj[a])<2 && sz(adj[b])<2)) {
critical.insert(i);
} else if ((adj[a].count(i) && sz(adj[b])<2) || (adj[b].count(i) && sz(adj[a])<2)) {
critical.insert(i);
}
}
} else {
set<int> neww;
for (auto u: critical) {
if (u==a || u==b) {
neww.insert(u);
} else if (((adj[a].count(u) && sz(adj[b])<2) || (adj[b].count(u) && sz(adj[a])<2))) {
neww.insert(u);
} else if (find(u)==find(a) || find(u)==find(b)) {
adj[a].insert(b), adj[b].insert(a);
for (auto x: adj[u]) adj[x].erase(u);
if (works(u)) neww.insert(u);
for (auto x: adj[u]) adj[x].insert(u);
adj[a].erase(b), adj[b].erase(a);
}
}
critical=neww;
}
merge(a, b);
adj[a].insert(b), adj[b].insert(a);
return;
}
merge(a, b);
contains[find(a)][sz(adj[a])]--;
contains[find(b)][sz(adj[b])]--;
adj[a].insert(b), adj[b].insert(a);
contains[find(a)][sz(adj[a])]++;
contains[find(b)][sz(adj[b])]++;
}
int CountCritical() {
return sz(critical);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
3 ms |
1360 KB |
Output is correct |
3 |
Correct |
3 ms |
1360 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
3 ms |
1316 KB |
Output is correct |
6 |
Correct |
4 ms |
1616 KB |
Output is correct |
7 |
Correct |
2 ms |
1104 KB |
Output is correct |
8 |
Correct |
3 ms |
1532 KB |
Output is correct |
9 |
Correct |
5 ms |
1360 KB |
Output is correct |
10 |
Correct |
3 ms |
1360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
589 ms |
130376 KB |
Output is correct |
2 |
Correct |
1049 ms |
162888 KB |
Output is correct |
3 |
Correct |
443 ms |
162512 KB |
Output is correct |
4 |
Correct |
1268 ms |
251848 KB |
Output is correct |
5 |
Correct |
1305 ms |
251476 KB |
Output is correct |
6 |
Correct |
1362 ms |
251688 KB |
Output is correct |
7 |
Correct |
446 ms |
162632 KB |
Output is correct |
8 |
Correct |
1018 ms |
206664 KB |
Output is correct |
9 |
Correct |
1125 ms |
234560 KB |
Output is correct |
10 |
Correct |
1048 ms |
251976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
3 ms |
1360 KB |
Output is correct |
3 |
Correct |
3 ms |
1360 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
3 ms |
1316 KB |
Output is correct |
6 |
Correct |
4 ms |
1616 KB |
Output is correct |
7 |
Correct |
2 ms |
1104 KB |
Output is correct |
8 |
Correct |
3 ms |
1532 KB |
Output is correct |
9 |
Correct |
5 ms |
1360 KB |
Output is correct |
10 |
Correct |
3 ms |
1360 KB |
Output is correct |
11 |
Correct |
4 ms |
1360 KB |
Output is correct |
12 |
Correct |
7 ms |
2896 KB |
Output is correct |
13 |
Correct |
7 ms |
2492 KB |
Output is correct |
14 |
Correct |
5 ms |
1872 KB |
Output is correct |
15 |
Correct |
8 ms |
3408 KB |
Output is correct |
16 |
Correct |
7 ms |
2640 KB |
Output is correct |
17 |
Correct |
5 ms |
2076 KB |
Output is correct |
18 |
Correct |
8 ms |
3664 KB |
Output is correct |
19 |
Correct |
7 ms |
2896 KB |
Output is correct |
20 |
Correct |
7 ms |
2552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
3 ms |
1360 KB |
Output is correct |
3 |
Correct |
3 ms |
1360 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
3 ms |
1316 KB |
Output is correct |
6 |
Correct |
4 ms |
1616 KB |
Output is correct |
7 |
Correct |
2 ms |
1104 KB |
Output is correct |
8 |
Correct |
3 ms |
1532 KB |
Output is correct |
9 |
Correct |
5 ms |
1360 KB |
Output is correct |
10 |
Correct |
3 ms |
1360 KB |
Output is correct |
11 |
Correct |
4 ms |
1360 KB |
Output is correct |
12 |
Correct |
7 ms |
2896 KB |
Output is correct |
13 |
Correct |
7 ms |
2492 KB |
Output is correct |
14 |
Correct |
5 ms |
1872 KB |
Output is correct |
15 |
Correct |
8 ms |
3408 KB |
Output is correct |
16 |
Correct |
7 ms |
2640 KB |
Output is correct |
17 |
Correct |
5 ms |
2076 KB |
Output is correct |
18 |
Correct |
8 ms |
3664 KB |
Output is correct |
19 |
Correct |
7 ms |
2896 KB |
Output is correct |
20 |
Correct |
7 ms |
2552 KB |
Output is correct |
21 |
Correct |
30 ms |
11088 KB |
Output is correct |
22 |
Correct |
49 ms |
16868 KB |
Output is correct |
23 |
Correct |
66 ms |
21832 KB |
Output is correct |
24 |
Correct |
48 ms |
15532 KB |
Output is correct |
25 |
Correct |
29 ms |
16356 KB |
Output is correct |
26 |
Correct |
70 ms |
17736 KB |
Output is correct |
27 |
Correct |
78 ms |
20808 KB |
Output is correct |
28 |
Correct |
35 ms |
15440 KB |
Output is correct |
29 |
Correct |
35 ms |
16976 KB |
Output is correct |
30 |
Correct |
73 ms |
26184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
3 ms |
1360 KB |
Output is correct |
3 |
Correct |
3 ms |
1360 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
3 ms |
1316 KB |
Output is correct |
6 |
Correct |
4 ms |
1616 KB |
Output is correct |
7 |
Correct |
2 ms |
1104 KB |
Output is correct |
8 |
Correct |
3 ms |
1532 KB |
Output is correct |
9 |
Correct |
5 ms |
1360 KB |
Output is correct |
10 |
Correct |
3 ms |
1360 KB |
Output is correct |
11 |
Correct |
589 ms |
130376 KB |
Output is correct |
12 |
Correct |
1049 ms |
162888 KB |
Output is correct |
13 |
Correct |
443 ms |
162512 KB |
Output is correct |
14 |
Correct |
1268 ms |
251848 KB |
Output is correct |
15 |
Correct |
1305 ms |
251476 KB |
Output is correct |
16 |
Correct |
1362 ms |
251688 KB |
Output is correct |
17 |
Correct |
446 ms |
162632 KB |
Output is correct |
18 |
Correct |
1018 ms |
206664 KB |
Output is correct |
19 |
Correct |
1125 ms |
234560 KB |
Output is correct |
20 |
Correct |
1048 ms |
251976 KB |
Output is correct |
21 |
Correct |
4 ms |
1360 KB |
Output is correct |
22 |
Correct |
7 ms |
2896 KB |
Output is correct |
23 |
Correct |
7 ms |
2492 KB |
Output is correct |
24 |
Correct |
5 ms |
1872 KB |
Output is correct |
25 |
Correct |
8 ms |
3408 KB |
Output is correct |
26 |
Correct |
7 ms |
2640 KB |
Output is correct |
27 |
Correct |
5 ms |
2076 KB |
Output is correct |
28 |
Correct |
8 ms |
3664 KB |
Output is correct |
29 |
Correct |
7 ms |
2896 KB |
Output is correct |
30 |
Correct |
7 ms |
2552 KB |
Output is correct |
31 |
Correct |
30 ms |
11088 KB |
Output is correct |
32 |
Correct |
49 ms |
16868 KB |
Output is correct |
33 |
Correct |
66 ms |
21832 KB |
Output is correct |
34 |
Correct |
48 ms |
15532 KB |
Output is correct |
35 |
Correct |
29 ms |
16356 KB |
Output is correct |
36 |
Correct |
70 ms |
17736 KB |
Output is correct |
37 |
Correct |
78 ms |
20808 KB |
Output is correct |
38 |
Correct |
35 ms |
15440 KB |
Output is correct |
39 |
Correct |
35 ms |
16976 KB |
Output is correct |
40 |
Correct |
73 ms |
26184 KB |
Output is correct |
41 |
Correct |
341 ms |
103124 KB |
Output is correct |
42 |
Correct |
715 ms |
171336 KB |
Output is correct |
43 |
Correct |
442 ms |
150088 KB |
Output is correct |
44 |
Correct |
390 ms |
157768 KB |
Output is correct |
45 |
Correct |
757 ms |
174408 KB |
Output is correct |
46 |
Correct |
933 ms |
213492 KB |
Output is correct |
47 |
Correct |
1036 ms |
220784 KB |
Output is correct |
48 |
Correct |
382 ms |
165704 KB |
Output is correct |
49 |
Correct |
894 ms |
235536 KB |
Output is correct |
50 |
Correct |
854 ms |
232776 KB |
Output is correct |
51 |
Correct |
380 ms |
128952 KB |
Output is correct |
52 |
Correct |
355 ms |
126536 KB |
Output is correct |
53 |
Correct |
439 ms |
164396 KB |
Output is correct |
54 |
Correct |
1079 ms |
175176 KB |
Output is correct |
55 |
Correct |
1138 ms |
191304 KB |
Output is correct |