#include <bits/stdc++.h>
#include <bits/extc++.h>
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
if(pvaspace) b << " "; pvaspace=true;\
b << pva;\
}\
b << "\n";}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
const ll MOD = 1000000007;
const ll MAX = 2147483647;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
ll ifloor(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a < 0) return (a - b + 1) / b;
else return a / b;
}
ll iceil(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a > 0) return (a + b - 1) / b;
else return a / b;
}
int n = -1;
vector<pii> edge;
vector<vector<int>> g;
struct DSU{
vector<int> dsu;
vector<int> deg, cnt, one, sz;
int cycle = -1;
int con = 0;
void init(){
con = n;
cnt.resize(4);
dsu.resize(n);
deg.resize(n);
one.resize(n);
sz.resize(n, 1);
cnt[0] = n;
for(int i = 0; i < n; i++) dsu[i] = i;
}
int findDSU(int a){
if(dsu[a] != a) dsu[a] = findDSU(dsu[a]);
return dsu[a];
}
void unionDSU(int a, int b){
int pa = findDSU(a);
int pb = findDSU(b);
if(deg[a] == 1) one[pa]--;
if(deg[b] == 1) one[pb]--;
cnt[min(deg[a], 3)]--;
cnt[min(deg[b], 3)]--;
if(pa != pb){
dsu[pb] = pa;
con--;
one[pa] += one[pb];
sz[pa] += sz[pb];
}
deg[a]++; deg[b]++;
if(deg[a] == 1) one[pa]++;
if(deg[b] == 1) one[pa]++;
cnt[min(deg[a], 3)]++;
cnt[min(deg[b], 3)]++;
if(sz[pa] > 1 && one[pa] == 0) cycle = sz[pa];
}
bool ok(){
return cnt[3] == 0 && cnt[1] == (con - cnt[0]) * 2;
}
int cy(){
if(cnt[3] != 0 || cnt[1] + 2 != (con - cnt[0]) * 2) return -1;
return cycle;
}
void print(){
cerr << "DSU " << ok() << " " << cy() << " " << con << "\n";
cerr << "dsu: ";
printv(dsu, cerr);
cerr << "sz: ";
printv(sz, cerr);
cerr << "cnt: ";
printv(cnt, cerr);
cerr << "deg: ";
printv(deg, cerr);
}
};
struct OAO{
int no;
DSU dsu;
explicit OAO(int no = -1): no(no) {
if(n != -1) dsu.init();
for(pii p : edge){
addedge(p.F, p.S);
}
}
void addedge(int u, int v){
if(u == no || v == no) return;
dsu.unionDSU(u, v);
}
};
OAO all;
vector<OAO> oao;
int status = 0;
void neighbor(int v){
if(status >= 1) return;
status = 1;
oao.clear();
oao.eb(OAO(v));
for(int i : g[v]) oao.eb(OAO(i));
}
void only(int v){
if(status >= 2) return;
status = 2;
oao.clear();
oao.eb(OAO(v));
}
void addedge(int u, int v){
g[u].eb(v);
g[v].eb(u);
edge.eb(mp(u, v));
all.addedge(u, v);
for(OAO& oao : oao) oao.addedge(u, v);
if(g[u].size() == 3) neighbor(u);
if(g[u].size() == 4) only(u);
if(g[v].size() == 3) neighbor(v);
if(g[v].size() == 4) only(v);
}
void Init(int N){
n = N;
all.dsu.init();
g.resize(n);
}
void Link(int u, int v){
addedge(u, v);
}
int CountCritical(){
// cerr << "test\n";
// all.dsu.print();
// for(OAO& t : oao){
// cerr << "test " << t.no << "\n";
// t.dsu.print();
// }
if(all.dsu.ok()) return n;
else if(all.dsu.cy() != -1) return all.dsu.cy();
int ans = 0;
for(OAO& t : oao) ans += t.dsu.ok();
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
4 ms |
844 KB |
Output is correct |
3 |
Correct |
3 ms |
844 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
572 KB |
Output is correct |
6 |
Correct |
3 ms |
716 KB |
Output is correct |
7 |
Correct |
2 ms |
844 KB |
Output is correct |
8 |
Correct |
2 ms |
588 KB |
Output is correct |
9 |
Correct |
4 ms |
1100 KB |
Output is correct |
10 |
Correct |
4 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
533 ms |
42140 KB |
Output is correct |
2 |
Correct |
1813 ms |
100332 KB |
Output is correct |
3 |
Correct |
2055 ms |
103244 KB |
Output is correct |
4 |
Correct |
1414 ms |
79624 KB |
Output is correct |
5 |
Correct |
1425 ms |
91960 KB |
Output is correct |
6 |
Correct |
1393 ms |
90156 KB |
Output is correct |
7 |
Correct |
1930 ms |
104096 KB |
Output is correct |
8 |
Correct |
2490 ms |
144480 KB |
Output is correct |
9 |
Correct |
2752 ms |
154520 KB |
Output is correct |
10 |
Correct |
981 ms |
89872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
4 ms |
844 KB |
Output is correct |
3 |
Correct |
3 ms |
844 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
572 KB |
Output is correct |
6 |
Correct |
3 ms |
716 KB |
Output is correct |
7 |
Correct |
2 ms |
844 KB |
Output is correct |
8 |
Correct |
2 ms |
588 KB |
Output is correct |
9 |
Correct |
4 ms |
1100 KB |
Output is correct |
10 |
Correct |
4 ms |
1100 KB |
Output is correct |
11 |
Correct |
5 ms |
1100 KB |
Output is correct |
12 |
Correct |
8 ms |
1848 KB |
Output is correct |
13 |
Correct |
8 ms |
1868 KB |
Output is correct |
14 |
Correct |
5 ms |
1356 KB |
Output is correct |
15 |
Correct |
5 ms |
2380 KB |
Output is correct |
16 |
Correct |
5 ms |
1228 KB |
Output is correct |
17 |
Correct |
8 ms |
1816 KB |
Output is correct |
18 |
Correct |
7 ms |
2508 KB |
Output is correct |
19 |
Correct |
8 ms |
1228 KB |
Output is correct |
20 |
Correct |
8 ms |
1868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
4 ms |
844 KB |
Output is correct |
3 |
Correct |
3 ms |
844 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
572 KB |
Output is correct |
6 |
Correct |
3 ms |
716 KB |
Output is correct |
7 |
Correct |
2 ms |
844 KB |
Output is correct |
8 |
Correct |
2 ms |
588 KB |
Output is correct |
9 |
Correct |
4 ms |
1100 KB |
Output is correct |
10 |
Correct |
4 ms |
1100 KB |
Output is correct |
11 |
Correct |
5 ms |
1100 KB |
Output is correct |
12 |
Correct |
8 ms |
1848 KB |
Output is correct |
13 |
Correct |
8 ms |
1868 KB |
Output is correct |
14 |
Correct |
5 ms |
1356 KB |
Output is correct |
15 |
Correct |
5 ms |
2380 KB |
Output is correct |
16 |
Correct |
5 ms |
1228 KB |
Output is correct |
17 |
Correct |
8 ms |
1816 KB |
Output is correct |
18 |
Correct |
7 ms |
2508 KB |
Output is correct |
19 |
Correct |
8 ms |
1228 KB |
Output is correct |
20 |
Correct |
8 ms |
1868 KB |
Output is correct |
21 |
Correct |
20 ms |
3784 KB |
Output is correct |
22 |
Correct |
33 ms |
5892 KB |
Output is correct |
23 |
Correct |
43 ms |
7444 KB |
Output is correct |
24 |
Correct |
67 ms |
10060 KB |
Output is correct |
25 |
Correct |
21 ms |
10628 KB |
Output is correct |
26 |
Correct |
60 ms |
10316 KB |
Output is correct |
27 |
Correct |
51 ms |
7932 KB |
Output is correct |
28 |
Correct |
150 ms |
13788 KB |
Output is correct |
29 |
Correct |
63 ms |
11332 KB |
Output is correct |
30 |
Correct |
64 ms |
9108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
4 ms |
844 KB |
Output is correct |
3 |
Correct |
3 ms |
844 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
572 KB |
Output is correct |
6 |
Correct |
3 ms |
716 KB |
Output is correct |
7 |
Correct |
2 ms |
844 KB |
Output is correct |
8 |
Correct |
2 ms |
588 KB |
Output is correct |
9 |
Correct |
4 ms |
1100 KB |
Output is correct |
10 |
Correct |
4 ms |
1100 KB |
Output is correct |
11 |
Correct |
533 ms |
42140 KB |
Output is correct |
12 |
Correct |
1813 ms |
100332 KB |
Output is correct |
13 |
Correct |
2055 ms |
103244 KB |
Output is correct |
14 |
Correct |
1414 ms |
79624 KB |
Output is correct |
15 |
Correct |
1425 ms |
91960 KB |
Output is correct |
16 |
Correct |
1393 ms |
90156 KB |
Output is correct |
17 |
Correct |
1930 ms |
104096 KB |
Output is correct |
18 |
Correct |
2490 ms |
144480 KB |
Output is correct |
19 |
Correct |
2752 ms |
154520 KB |
Output is correct |
20 |
Correct |
981 ms |
89872 KB |
Output is correct |
21 |
Correct |
5 ms |
1100 KB |
Output is correct |
22 |
Correct |
8 ms |
1848 KB |
Output is correct |
23 |
Correct |
8 ms |
1868 KB |
Output is correct |
24 |
Correct |
5 ms |
1356 KB |
Output is correct |
25 |
Correct |
5 ms |
2380 KB |
Output is correct |
26 |
Correct |
5 ms |
1228 KB |
Output is correct |
27 |
Correct |
8 ms |
1816 KB |
Output is correct |
28 |
Correct |
7 ms |
2508 KB |
Output is correct |
29 |
Correct |
8 ms |
1228 KB |
Output is correct |
30 |
Correct |
8 ms |
1868 KB |
Output is correct |
31 |
Correct |
20 ms |
3784 KB |
Output is correct |
32 |
Correct |
33 ms |
5892 KB |
Output is correct |
33 |
Correct |
43 ms |
7444 KB |
Output is correct |
34 |
Correct |
67 ms |
10060 KB |
Output is correct |
35 |
Correct |
21 ms |
10628 KB |
Output is correct |
36 |
Correct |
60 ms |
10316 KB |
Output is correct |
37 |
Correct |
51 ms |
7932 KB |
Output is correct |
38 |
Correct |
150 ms |
13788 KB |
Output is correct |
39 |
Correct |
63 ms |
11332 KB |
Output is correct |
40 |
Correct |
64 ms |
9108 KB |
Output is correct |
41 |
Correct |
269 ms |
33508 KB |
Output is correct |
42 |
Correct |
1102 ms |
111852 KB |
Output is correct |
43 |
Correct |
363 ms |
94684 KB |
Output is correct |
44 |
Correct |
1280 ms |
97940 KB |
Output is correct |
45 |
Correct |
1558 ms |
109992 KB |
Output is correct |
46 |
Correct |
893 ms |
76672 KB |
Output is correct |
47 |
Correct |
1287 ms |
78072 KB |
Output is correct |
48 |
Correct |
778 ms |
103956 KB |
Output is correct |
49 |
Correct |
813 ms |
80940 KB |
Output is correct |
50 |
Correct |
993 ms |
79932 KB |
Output is correct |
51 |
Correct |
434 ms |
80828 KB |
Output is correct |
52 |
Correct |
1942 ms |
113892 KB |
Output is correct |
53 |
Correct |
806 ms |
104672 KB |
Output is correct |
54 |
Correct |
2663 ms |
125128 KB |
Output is correct |
55 |
Correct |
1653 ms |
95144 KB |
Output is correct |