#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <set>
typedef long long llong;
const int MAXN = 1000000 + 10;
const int INF = 1e9;
int n;
int d[MAXN];
int d2[MAXN];
struct DSU
{
int par[MAXN];
int dep[MAXN];
int sz[MAXN];
void build()
{
for (int i = 1 ; i <= n ; ++i)
{
par[i] = i;
dep[i] = 1;
sz[i] = 1;
}
}
int find(int u)
{
if (u == par[u]) return u;
return par[u] = find(par[u]);
}
void connect(int u, int v)
{
u = find(u);
v = find(v);
if (u == v)
{
assert(false);
return;
}
if (dep[u] < dep[v])
{
std::swap(u, v);
}
if (dep[u] == dep[v])
{
dep[v]++;
}
par[u] = v;
sz[v] += sz[u];
}
bool areConnected(int u, int v)
{
return find(u) == find(v);
}
};
DSU dsu;
DSU dsu2;
int ans;
void Init(int N_)
{
n = N_;
ans = n;
dsu.build();
}
int cycle;
bool isBad;
int cntThree;
int theNode = -1;
std::set <int> toNode;
std::set <int> toNode2;
std::vector <int> three;
std::vector <std::pair <int,int>> added;
std::vector <int> g[MAXN];
void reConnect(int node)
{
dsu.build();
theNode = node;
std::fill(d + 1, d + 1 + n, 0);
for (const auto &[u, v] : added)
{
d[u]++;
d[v]++;
if (u == node)
{
toNode.insert(v);
continue;
}
if (v == node)
{
toNode.insert(u);
continue;
}
if (dsu.areConnected(u, v))
{
isBad = true;
return;
}
dsu.connect(u, v);
}
for (int i = 1 ; i <= n ; ++i)
{
if (d[i] - toNode.count(i) > 2 && i != node)
{
isBad = true;
return;
}
}
}
bool check(int node)
{
dsu2.build();
std::fill(d2 + 1, d2 + 1 + n, 0);
for (const auto &[u, v] : added)
{
d2[u]++;
d2[v]++;
if (u == node)
{
d2[v]--;
continue;
}
if (v == node)
{
d2[u]--;
continue;
}
if (dsu2.areConnected(u, v))
{
return false;
}
dsu2.connect(u, v);
}
for (int i = 1 ; i <= n ; ++i)
{
if (d2[i] > 2 && i != node)
{
return false;
}
}
return true;
}
bool isLenThree = false;
void Link(int u, int v)
{
u++;
v++;
if (isBad)
{
return;
}
added.push_back({u, v});
if (theNode != -1)
{
if (v == theNode)
{
std::swap(u, v);
}
if (u == theNode)
{
d[v]++;
toNode.insert(v);
if (d[v] > 3)
{
isBad = true;
return;
}
} else
{
d[v]++;
d[u]++;
if (d[v] - toNode.count(v) > 2)
{
isBad = true;
return;
}
if (d[u] - toNode.count(u) > 2)
{
isBad = true;
return;
}
if (dsu.areConnected(u, v))
{
isBad = true;
return;
}
dsu.connect(u, v);
}
} else
{
d[u]++;
d[v]++;
if (d[u] == 4 && d[v] == 4)
{
isBad = true;
return;
}
if (d[u] == 4)
{
reConnect(u);
return;
}
if (d[v] == 4)
{
reConnect(v);
return;
}
cntThree += (d[u] == 3);
cntThree += (d[v] == 3);
g[u].push_back(v);
g[v].push_back(u);
if (d[u] == 3)
{
three.push_back(u);
}
if (d[v] == 3)
{
three.push_back(v);
}
if (cntThree > 4)
{
isBad = true;
return;
}
if (cycle && dsu.areConnected(u, v))
{
if (dsu.find(u) != dsu.find(cycle))
{
isBad = true;
return;
}
}
if (!cycle && dsu.areConnected(u, v))
{
for (const int &x : g[u])
{
if (x == v)
{
isLenThree = true;
}
}
cycle = u;
}
if (!dsu.areConnected(u, v))
{
dsu.connect(u, v);
}
if (cntThree != 0 && cycle != 0)
{
std::set <int> good123;
if (cntThree <= 2)
{
for (const int &u : three)
{
good123.insert(u);
for (const int &i : g[u])
good123.insert(i);
}
} else
{
for (const int &u : three)
{
good123.insert(u);
}
}
ans = 0;
for (const int &u : good123)
{
ans += check(u);
}
return;
}
if (cycle)
{
ans = dsu.sz[dsu.find(cycle)];
return;
}
if (cntThree > 4)
{
isBad = true;
return;
}
if (cntThree == 0)
{
ans = n;
return;
}
if (cntThree == 1)
{
ans = 4;
return;
}
ans = 0;
for (const int &i : three)
{
int curr = 0;
for (const int &u : g[i])
{
curr += (d[u] == 3);
}
if (curr == cntThree - 1)
{
ans++;
}
}
if (cntThree == 2)
{
for (const int &u : g[three[0]])
{
for (const int &v : g[three[1]])
{
if (u == v)
{
ans++;
}
}
}
}
}
}
int CountCritical()
{
if (isBad) return 0;
if (theNode != -1) return 1;
return ans;
}
/*
6 14
1 2
-1
1 3
-1
2 3
-1
1 0
-1
3 4
-1
2 5
-1
5 4
-1
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
24016 KB |
Output is correct |
3 |
Correct |
11 ms |
24064 KB |
Output is correct |
4 |
Correct |
11 ms |
23764 KB |
Output is correct |
5 |
Correct |
10 ms |
24020 KB |
Output is correct |
6 |
Correct |
12 ms |
24148 KB |
Output is correct |
7 |
Correct |
12 ms |
23892 KB |
Output is correct |
8 |
Correct |
11 ms |
24020 KB |
Output is correct |
9 |
Correct |
12 ms |
24148 KB |
Output is correct |
10 |
Correct |
16 ms |
24092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
276 ms |
52324 KB |
Output is correct |
2 |
Correct |
404 ms |
59668 KB |
Output is correct |
3 |
Correct |
178 ms |
40316 KB |
Output is correct |
4 |
Correct |
883 ms |
78432 KB |
Output is correct |
5 |
Correct |
913 ms |
78536 KB |
Output is correct |
6 |
Correct |
833 ms |
77204 KB |
Output is correct |
7 |
Correct |
166 ms |
40244 KB |
Output is correct |
8 |
Correct |
792 ms |
74932 KB |
Output is correct |
9 |
Correct |
866 ms |
78408 KB |
Output is correct |
10 |
Correct |
595 ms |
76324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
24016 KB |
Output is correct |
3 |
Correct |
11 ms |
24064 KB |
Output is correct |
4 |
Correct |
11 ms |
23764 KB |
Output is correct |
5 |
Correct |
10 ms |
24020 KB |
Output is correct |
6 |
Correct |
12 ms |
24148 KB |
Output is correct |
7 |
Correct |
12 ms |
23892 KB |
Output is correct |
8 |
Correct |
11 ms |
24020 KB |
Output is correct |
9 |
Correct |
12 ms |
24148 KB |
Output is correct |
10 |
Correct |
16 ms |
24092 KB |
Output is correct |
11 |
Correct |
195 ms |
24212 KB |
Output is correct |
12 |
Correct |
235 ms |
24756 KB |
Output is correct |
13 |
Correct |
185 ms |
24620 KB |
Output is correct |
14 |
Correct |
12 ms |
24148 KB |
Output is correct |
15 |
Correct |
15 ms |
24388 KB |
Output is correct |
16 |
Correct |
13 ms |
24404 KB |
Output is correct |
17 |
Correct |
15 ms |
24148 KB |
Output is correct |
18 |
Correct |
12 ms |
24148 KB |
Output is correct |
19 |
Correct |
13 ms |
24404 KB |
Output is correct |
20 |
Correct |
13 ms |
24404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
24016 KB |
Output is correct |
3 |
Correct |
11 ms |
24064 KB |
Output is correct |
4 |
Correct |
11 ms |
23764 KB |
Output is correct |
5 |
Correct |
10 ms |
24020 KB |
Output is correct |
6 |
Correct |
12 ms |
24148 KB |
Output is correct |
7 |
Correct |
12 ms |
23892 KB |
Output is correct |
8 |
Correct |
11 ms |
24020 KB |
Output is correct |
9 |
Correct |
12 ms |
24148 KB |
Output is correct |
10 |
Correct |
16 ms |
24092 KB |
Output is correct |
11 |
Correct |
195 ms |
24212 KB |
Output is correct |
12 |
Correct |
235 ms |
24756 KB |
Output is correct |
13 |
Correct |
185 ms |
24620 KB |
Output is correct |
14 |
Correct |
12 ms |
24148 KB |
Output is correct |
15 |
Correct |
15 ms |
24388 KB |
Output is correct |
16 |
Correct |
13 ms |
24404 KB |
Output is correct |
17 |
Correct |
15 ms |
24148 KB |
Output is correct |
18 |
Correct |
12 ms |
24148 KB |
Output is correct |
19 |
Correct |
13 ms |
24404 KB |
Output is correct |
20 |
Correct |
13 ms |
24404 KB |
Output is correct |
21 |
Correct |
23 ms |
25788 KB |
Output is correct |
22 |
Correct |
31 ms |
26928 KB |
Output is correct |
23 |
Correct |
37 ms |
27832 KB |
Output is correct |
24 |
Correct |
40 ms |
26372 KB |
Output is correct |
25 |
Correct |
19 ms |
25744 KB |
Output is correct |
26 |
Correct |
32 ms |
26072 KB |
Output is correct |
27 |
Correct |
38 ms |
28400 KB |
Output is correct |
28 |
Correct |
47 ms |
26772 KB |
Output is correct |
29 |
Correct |
27 ms |
25852 KB |
Output is correct |
30 |
Correct |
56 ms |
29220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
24016 KB |
Output is correct |
3 |
Correct |
11 ms |
24064 KB |
Output is correct |
4 |
Correct |
11 ms |
23764 KB |
Output is correct |
5 |
Correct |
10 ms |
24020 KB |
Output is correct |
6 |
Correct |
12 ms |
24148 KB |
Output is correct |
7 |
Correct |
12 ms |
23892 KB |
Output is correct |
8 |
Correct |
11 ms |
24020 KB |
Output is correct |
9 |
Correct |
12 ms |
24148 KB |
Output is correct |
10 |
Correct |
16 ms |
24092 KB |
Output is correct |
11 |
Correct |
276 ms |
52324 KB |
Output is correct |
12 |
Correct |
404 ms |
59668 KB |
Output is correct |
13 |
Correct |
178 ms |
40316 KB |
Output is correct |
14 |
Correct |
883 ms |
78432 KB |
Output is correct |
15 |
Correct |
913 ms |
78536 KB |
Output is correct |
16 |
Correct |
833 ms |
77204 KB |
Output is correct |
17 |
Correct |
166 ms |
40244 KB |
Output is correct |
18 |
Correct |
792 ms |
74932 KB |
Output is correct |
19 |
Correct |
866 ms |
78408 KB |
Output is correct |
20 |
Correct |
595 ms |
76324 KB |
Output is correct |
21 |
Correct |
195 ms |
24212 KB |
Output is correct |
22 |
Correct |
235 ms |
24756 KB |
Output is correct |
23 |
Correct |
185 ms |
24620 KB |
Output is correct |
24 |
Correct |
12 ms |
24148 KB |
Output is correct |
25 |
Correct |
15 ms |
24388 KB |
Output is correct |
26 |
Correct |
13 ms |
24404 KB |
Output is correct |
27 |
Correct |
15 ms |
24148 KB |
Output is correct |
28 |
Correct |
12 ms |
24148 KB |
Output is correct |
29 |
Correct |
13 ms |
24404 KB |
Output is correct |
30 |
Correct |
13 ms |
24404 KB |
Output is correct |
31 |
Correct |
23 ms |
25788 KB |
Output is correct |
32 |
Correct |
31 ms |
26928 KB |
Output is correct |
33 |
Correct |
37 ms |
27832 KB |
Output is correct |
34 |
Correct |
40 ms |
26372 KB |
Output is correct |
35 |
Correct |
19 ms |
25744 KB |
Output is correct |
36 |
Correct |
32 ms |
26072 KB |
Output is correct |
37 |
Correct |
38 ms |
28400 KB |
Output is correct |
38 |
Correct |
47 ms |
26772 KB |
Output is correct |
39 |
Correct |
27 ms |
25852 KB |
Output is correct |
40 |
Correct |
56 ms |
29220 KB |
Output is correct |
41 |
Correct |
159 ms |
41804 KB |
Output is correct |
42 |
Correct |
329 ms |
56184 KB |
Output is correct |
43 |
Correct |
192 ms |
40804 KB |
Output is correct |
44 |
Correct |
422 ms |
53068 KB |
Output is correct |
45 |
Correct |
571 ms |
61412 KB |
Output is correct |
46 |
Correct |
540 ms |
80376 KB |
Output is correct |
47 |
Correct |
718 ms |
82212 KB |
Output is correct |
48 |
Correct |
216 ms |
48268 KB |
Output is correct |
49 |
Correct |
578 ms |
80904 KB |
Output is correct |
50 |
Correct |
601 ms |
80404 KB |
Output is correct |
51 |
Correct |
191 ms |
42512 KB |
Output is correct |
52 |
Correct |
1481 ms |
58076 KB |
Output is correct |
53 |
Correct |
155 ms |
47692 KB |
Output is correct |
54 |
Correct |
762 ms |
79796 KB |
Output is correct |
55 |
Correct |
364 ms |
57208 KB |
Output is correct |