#include <bits/stdc++.h>
using namespace std;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
const int MAXN = 100013;
int N, M;
int dsu[MAXN], siz[MAXN];
vi edge[MAXN];
ll ans;
set<int> in[MAXN]; //vertices into the CLIQUE i.
map<int, set<int> > out[MAXN]; //edges between cliques. out[x][y] stores for all x -> y,what are the actual vertices?
void merge(int u, int v);
int get(int u)
{
return (u == dsu[u] ? u : dsu[u] = get(dsu[u]));
}
void addedge(int u, int v)
{
// cerr << "addedge " << u << ' ' << v << endl;
int x = get(u), y = get(v);
if (x == y || in[y].find(u) != in[y].end())
{
//do nothing.
}
else if (SZ(out[x][y]) > 0 || SZ(out[y][x]) == 0)
{
//maybe x -> y has an edge already?
out[x][y].insert(u);
ans += siz[y];
in[y].insert(u);
}
else
{
for (auto a : out[y][x])
{
// cerr << "in " << x << " erase " << a << endl;
in[x].erase(a);
ans -= siz[x];
}
// cerr << "ans = " << ans << endl;
out[y].erase(x);
//y -> x has an edge already.
merge(x, y);
}
}
void merge(int u, int v)
{
u = get(u);
v = get(v);
if (u == v) return;
// if (get(u) == get(v)) return;
// assert(get(u) == u);
// assert(get(v) == v);
for (int x : out[u][v])
{
in[v].erase(x);
ans -= siz[v];
}
for (int x : out[v][u])
{
in[u].erase(x);
ans -= siz[u];
}
out[u][v].clear();
out[v][u].clear();
// cerr << "merge " << u << ' ' << v << endl;
ans += 2ll * siz[u] * siz[v];
// cerr << "ans = " << ans << endl;
//now the actual merging part.
//x -> both: do nothing
//x -> one: connect it to the other.
//x -> u and v -> x: MERGE AGAIN.
if (SZ(out[u]) + SZ(in[u]) < SZ(out[v]) + SZ(in[v]))
{
swap(u, v);
}
// cerr << "merging " << u << ' ' << v << endl;
set<int> merges;
//this takes care of all edges stemming out of the connected component of v.
for (auto it = out[v].begin(); it != out[v].end();)
{
if ((it -> se).empty())
{
it = out[v].erase(it);
continue;
}
int cc = it -> fi;
// if (cc == u)
// assert(cc != u);
// cerr << v << " -> " << cc << endl;
// cerr << v << " -> " << cc << endl;
if (SZ(out[cc][u]) > 0) //cc is the connected component that this is connected to.
{
//merge.
//disconnect everything from that cc.
for (auto a : it -> se)
{
in[cc].erase(a);
ans -= siz[cc];
}
//disconnected everything in that cc from u.
for (auto a : out[cc][u])
{
in[u].erase(a);
ans -= siz[u];
}
out[cc].erase(u);
it = out[v].erase(it);
merges.insert(cc);
}
else
{
for (auto a : it -> se)
{
out[u][it -> fi].insert(a);
}
it++;
}
}
out[v].clear();
int cnt = 0, cnt1 = 0;
for (int w : in[v])
{
// cerr << "w = " << w << endl;
int cc = get(w);
out[cc].erase(v);
if (merges.find(cc) != merges.end() || SZ(out[u][cc]) > 0)
{
// cerr << "pity " << w << endl;
for (int z : out[u][cc])
{
ans -= siz[cc];
in[cc].erase(z);
}
// cerr << "ans " << ans << endl;
out[u].erase(cc);
merges.insert(cc);
ans -= siz[v];
// cerr << "ans " << ans << endl;
continue;
}
else if (in[u].find(w) != in[u].end())
{
cnt++;
}
else
{
in[u].insert(w);
out[cc][u].insert(w);
ans += siz[u];
cnt1++;
}
}
// cerr << SZ(in[u]) << ' ' << cnt << ' ' << cnt1 << endl;
ans += (SZ(in[u]) - cnt - cnt1) * siz[v];
in[v].clear();
dsu[v] = u;
siz[u] += siz[v];
siz[v] = 0;
for (auto a : merges)
{
merge(a, u);
}
return;
}
int32_t main()
{
cout << fixed << setprecision(12);
cerr << fixed << setprecision(4);
ios_base::sync_with_stdio(false); cin.tie(0);
// freopen("test.out", "w", stdout);
cin >> N >> M;
FOR(i, 0, N)
{
dsu[i] = i;
siz[i] = 1;
}
//store edges of the form clique -> node. they are actually reverses of the actual edges.
//also, store clique -> clique edges.
FOR(i, 0, M)
{
int u, v;
cin >> u >> v; u--; v--;
addedge(u, v);
cout << ans << '\n';
//check if u has an edge to anything in v.
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12032 KB |
Output is correct |
2 |
Correct |
11 ms |
12032 KB |
Output is correct |
3 |
Correct |
11 ms |
12160 KB |
Output is correct |
4 |
Correct |
11 ms |
12240 KB |
Output is correct |
5 |
Correct |
11 ms |
12160 KB |
Output is correct |
6 |
Correct |
12 ms |
12160 KB |
Output is correct |
7 |
Correct |
12 ms |
12160 KB |
Output is correct |
8 |
Correct |
11 ms |
12160 KB |
Output is correct |
9 |
Correct |
11 ms |
12160 KB |
Output is correct |
10 |
Correct |
11 ms |
12160 KB |
Output is correct |
11 |
Correct |
13 ms |
12032 KB |
Output is correct |
12 |
Correct |
11 ms |
12032 KB |
Output is correct |
13 |
Correct |
11 ms |
12032 KB |
Output is correct |
14 |
Correct |
11 ms |
12160 KB |
Output is correct |
15 |
Correct |
11 ms |
12032 KB |
Output is correct |
16 |
Correct |
11 ms |
12160 KB |
Output is correct |
17 |
Correct |
11 ms |
12160 KB |
Output is correct |
18 |
Correct |
11 ms |
12136 KB |
Output is correct |
19 |
Correct |
10 ms |
12160 KB |
Output is correct |
20 |
Correct |
11 ms |
12160 KB |
Output is correct |
21 |
Correct |
12 ms |
12160 KB |
Output is correct |
22 |
Correct |
13 ms |
12032 KB |
Output is correct |
23 |
Correct |
11 ms |
12160 KB |
Output is correct |
24 |
Correct |
11 ms |
12160 KB |
Output is correct |
25 |
Correct |
11 ms |
12160 KB |
Output is correct |
26 |
Correct |
11 ms |
12160 KB |
Output is correct |
27 |
Correct |
11 ms |
12160 KB |
Output is correct |
28 |
Correct |
12 ms |
12160 KB |
Output is correct |
29 |
Correct |
11 ms |
12160 KB |
Output is correct |
30 |
Correct |
11 ms |
12160 KB |
Output is correct |
31 |
Correct |
11 ms |
12160 KB |
Output is correct |
32 |
Correct |
11 ms |
12160 KB |
Output is correct |
33 |
Correct |
11 ms |
12160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12032 KB |
Output is correct |
2 |
Correct |
11 ms |
12032 KB |
Output is correct |
3 |
Correct |
11 ms |
12160 KB |
Output is correct |
4 |
Correct |
11 ms |
12240 KB |
Output is correct |
5 |
Correct |
11 ms |
12160 KB |
Output is correct |
6 |
Correct |
12 ms |
12160 KB |
Output is correct |
7 |
Correct |
12 ms |
12160 KB |
Output is correct |
8 |
Correct |
11 ms |
12160 KB |
Output is correct |
9 |
Correct |
11 ms |
12160 KB |
Output is correct |
10 |
Correct |
11 ms |
12160 KB |
Output is correct |
11 |
Correct |
13 ms |
12032 KB |
Output is correct |
12 |
Correct |
11 ms |
12032 KB |
Output is correct |
13 |
Correct |
11 ms |
12032 KB |
Output is correct |
14 |
Correct |
11 ms |
12160 KB |
Output is correct |
15 |
Correct |
11 ms |
12032 KB |
Output is correct |
16 |
Correct |
11 ms |
12160 KB |
Output is correct |
17 |
Correct |
11 ms |
12160 KB |
Output is correct |
18 |
Correct |
11 ms |
12136 KB |
Output is correct |
19 |
Correct |
10 ms |
12160 KB |
Output is correct |
20 |
Correct |
11 ms |
12160 KB |
Output is correct |
21 |
Correct |
12 ms |
12160 KB |
Output is correct |
22 |
Correct |
13 ms |
12032 KB |
Output is correct |
23 |
Correct |
11 ms |
12160 KB |
Output is correct |
24 |
Correct |
11 ms |
12160 KB |
Output is correct |
25 |
Correct |
11 ms |
12160 KB |
Output is correct |
26 |
Correct |
11 ms |
12160 KB |
Output is correct |
27 |
Correct |
11 ms |
12160 KB |
Output is correct |
28 |
Correct |
12 ms |
12160 KB |
Output is correct |
29 |
Correct |
11 ms |
12160 KB |
Output is correct |
30 |
Correct |
11 ms |
12160 KB |
Output is correct |
31 |
Correct |
11 ms |
12160 KB |
Output is correct |
32 |
Correct |
11 ms |
12160 KB |
Output is correct |
33 |
Correct |
11 ms |
12160 KB |
Output is correct |
34 |
Correct |
13 ms |
12288 KB |
Output is correct |
35 |
Correct |
103 ms |
17084 KB |
Output is correct |
36 |
Correct |
127 ms |
21496 KB |
Output is correct |
37 |
Correct |
134 ms |
21624 KB |
Output is correct |
38 |
Correct |
137 ms |
20860 KB |
Output is correct |
39 |
Correct |
14 ms |
12288 KB |
Output is correct |
40 |
Correct |
14 ms |
12416 KB |
Output is correct |
41 |
Correct |
14 ms |
12544 KB |
Output is correct |
42 |
Correct |
14 ms |
12288 KB |
Output is correct |
43 |
Correct |
15 ms |
12416 KB |
Output is correct |
44 |
Correct |
15 ms |
12416 KB |
Output is correct |
45 |
Correct |
14 ms |
12288 KB |
Output is correct |
46 |
Correct |
16 ms |
12288 KB |
Output is correct |
47 |
Correct |
16 ms |
12416 KB |
Output is correct |
48 |
Correct |
14 ms |
12416 KB |
Output is correct |
49 |
Correct |
23 ms |
13824 KB |
Output is correct |
50 |
Correct |
131 ms |
21752 KB |
Output is correct |
51 |
Correct |
19 ms |
12800 KB |
Output is correct |
52 |
Correct |
108 ms |
18528 KB |
Output is correct |
53 |
Correct |
22 ms |
13696 KB |
Output is correct |
54 |
Correct |
120 ms |
20088 KB |
Output is correct |
55 |
Correct |
18 ms |
13184 KB |
Output is correct |
56 |
Correct |
17 ms |
13184 KB |
Output is correct |
57 |
Correct |
18 ms |
13696 KB |
Output is correct |
58 |
Correct |
17 ms |
13696 KB |
Output is correct |
59 |
Correct |
14 ms |
12160 KB |
Output is correct |
60 |
Correct |
89 ms |
14584 KB |
Output is correct |
61 |
Correct |
16 ms |
12544 KB |
Output is correct |
62 |
Correct |
128 ms |
20676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12032 KB |
Output is correct |
2 |
Correct |
11 ms |
12032 KB |
Output is correct |
3 |
Correct |
11 ms |
12160 KB |
Output is correct |
4 |
Correct |
11 ms |
12240 KB |
Output is correct |
5 |
Correct |
11 ms |
12160 KB |
Output is correct |
6 |
Correct |
12 ms |
12160 KB |
Output is correct |
7 |
Correct |
12 ms |
12160 KB |
Output is correct |
8 |
Correct |
11 ms |
12160 KB |
Output is correct |
9 |
Correct |
11 ms |
12160 KB |
Output is correct |
10 |
Correct |
11 ms |
12160 KB |
Output is correct |
11 |
Correct |
13 ms |
12032 KB |
Output is correct |
12 |
Correct |
11 ms |
12032 KB |
Output is correct |
13 |
Correct |
11 ms |
12032 KB |
Output is correct |
14 |
Correct |
11 ms |
12160 KB |
Output is correct |
15 |
Correct |
11 ms |
12032 KB |
Output is correct |
16 |
Correct |
11 ms |
12160 KB |
Output is correct |
17 |
Correct |
11 ms |
12160 KB |
Output is correct |
18 |
Correct |
11 ms |
12136 KB |
Output is correct |
19 |
Correct |
10 ms |
12160 KB |
Output is correct |
20 |
Correct |
11 ms |
12160 KB |
Output is correct |
21 |
Correct |
12 ms |
12160 KB |
Output is correct |
22 |
Correct |
13 ms |
12032 KB |
Output is correct |
23 |
Correct |
11 ms |
12160 KB |
Output is correct |
24 |
Correct |
11 ms |
12160 KB |
Output is correct |
25 |
Correct |
11 ms |
12160 KB |
Output is correct |
26 |
Correct |
11 ms |
12160 KB |
Output is correct |
27 |
Correct |
11 ms |
12160 KB |
Output is correct |
28 |
Correct |
12 ms |
12160 KB |
Output is correct |
29 |
Correct |
11 ms |
12160 KB |
Output is correct |
30 |
Correct |
11 ms |
12160 KB |
Output is correct |
31 |
Correct |
11 ms |
12160 KB |
Output is correct |
32 |
Correct |
11 ms |
12160 KB |
Output is correct |
33 |
Correct |
11 ms |
12160 KB |
Output is correct |
34 |
Correct |
13 ms |
12288 KB |
Output is correct |
35 |
Correct |
103 ms |
17084 KB |
Output is correct |
36 |
Correct |
127 ms |
21496 KB |
Output is correct |
37 |
Correct |
134 ms |
21624 KB |
Output is correct |
38 |
Correct |
137 ms |
20860 KB |
Output is correct |
39 |
Correct |
14 ms |
12288 KB |
Output is correct |
40 |
Correct |
14 ms |
12416 KB |
Output is correct |
41 |
Correct |
14 ms |
12544 KB |
Output is correct |
42 |
Correct |
14 ms |
12288 KB |
Output is correct |
43 |
Correct |
15 ms |
12416 KB |
Output is correct |
44 |
Correct |
15 ms |
12416 KB |
Output is correct |
45 |
Correct |
14 ms |
12288 KB |
Output is correct |
46 |
Correct |
16 ms |
12288 KB |
Output is correct |
47 |
Correct |
16 ms |
12416 KB |
Output is correct |
48 |
Correct |
14 ms |
12416 KB |
Output is correct |
49 |
Correct |
23 ms |
13824 KB |
Output is correct |
50 |
Correct |
131 ms |
21752 KB |
Output is correct |
51 |
Correct |
19 ms |
12800 KB |
Output is correct |
52 |
Correct |
108 ms |
18528 KB |
Output is correct |
53 |
Correct |
22 ms |
13696 KB |
Output is correct |
54 |
Correct |
120 ms |
20088 KB |
Output is correct |
55 |
Correct |
18 ms |
13184 KB |
Output is correct |
56 |
Correct |
17 ms |
13184 KB |
Output is correct |
57 |
Correct |
18 ms |
13696 KB |
Output is correct |
58 |
Correct |
17 ms |
13696 KB |
Output is correct |
59 |
Correct |
14 ms |
12160 KB |
Output is correct |
60 |
Correct |
89 ms |
14584 KB |
Output is correct |
61 |
Correct |
16 ms |
12544 KB |
Output is correct |
62 |
Correct |
128 ms |
20676 KB |
Output is correct |
63 |
Correct |
581 ms |
99532 KB |
Output is correct |
64 |
Correct |
584 ms |
99576 KB |
Output is correct |
65 |
Correct |
569 ms |
99448 KB |
Output is correct |
66 |
Correct |
258 ms |
24316 KB |
Output is correct |
67 |
Correct |
302 ms |
31736 KB |
Output is correct |
68 |
Correct |
258 ms |
24440 KB |
Output is correct |
69 |
Correct |
481 ms |
32016 KB |
Output is correct |
70 |
Correct |
257 ms |
24312 KB |
Output is correct |
71 |
Correct |
277 ms |
24568 KB |
Output is correct |
72 |
Correct |
342 ms |
31608 KB |
Output is correct |
73 |
Correct |
359 ms |
31644 KB |
Output is correct |
74 |
Correct |
1002 ms |
57592 KB |
Output is correct |
75 |
Correct |
625 ms |
40184 KB |
Output is correct |
76 |
Correct |
724 ms |
53496 KB |
Output is correct |
77 |
Correct |
752 ms |
53624 KB |
Output is correct |
78 |
Correct |
255 ms |
33272 KB |
Output is correct |
79 |
Correct |
433 ms |
35448 KB |
Output is correct |
80 |
Correct |
270 ms |
33144 KB |
Output is correct |
81 |
Correct |
426 ms |
35192 KB |
Output is correct |
82 |
Correct |
713 ms |
70520 KB |
Output is correct |
83 |
Correct |
718 ms |
70520 KB |
Output is correct |
84 |
Correct |
678 ms |
90872 KB |
Output is correct |
85 |
Correct |
677 ms |
90872 KB |
Output is correct |
86 |
Correct |
152 ms |
18940 KB |
Output is correct |
87 |
Correct |
186 ms |
19960 KB |
Output is correct |
88 |
Correct |
356 ms |
31608 KB |
Output is correct |
89 |
Correct |
709 ms |
51320 KB |
Output is correct |