#include<bits/stdc++.h>
#define ll int
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
using namespace std;
const ll maxn=100005;
set <pll> out[maxn];
ll ans, pa[maxn], sz[maxn];
set <ll> in[maxn], comin[maxn], comout[maxn];
ll Find(ll i)
{
if (pa[i]!=i)
pa[i]=Find(pa[i]);
return pa[i];
}
void addedge(ll a, ll b)
{
ll Pa=Find(a), Pb=Find(b);
if (Pa==Pb) return;
if (comin[Pa].find(Pb)!=comin[Pa].end())
{
if (sz[Pa]<sz[Pb]) swap(Pa, Pb);
vector <pll> upd;
for (pll i:out[Pb])
{
in[i.se].erase(i.fi), ans-=sz[i.se];
if (i.se!=Pa) upd.pb(i);
}
ans+=(ll)in[Pa].size()*sz[Pb];
for (ll i:comin[Pb])
{
comin[Pa].insert(i);
comout[i].erase(Pb);
comout[i].insert(Pa);
}
for (ll i:comout[Pb])
{
comout[Pa].insert(i);
comin[i].erase(Pb);
comin[i].insert(Pa);
}
for (ll i:in[Pb])
{
ll f=Find(i); out[f].erase(mp(i, Pb));
if (f==Pa) ans-=sz[Pb];
else if (in[Pa].find(i)==in[Pa].end())
upd.pb(mp(i, Pa)), ans-=sz[Pb];
else ans-=sz[Pb];
}
ans-=sz[Pa]*(sz[Pa]-1)+sz[Pb]*(sz[Pb]-1);
sz[Pa]+=sz[Pb], pa[Pb]=Pa;
ans+=sz[Pa]*(sz[Pa]-1);
for (pll i:upd) addedge(i.fi, i.se);
}
else if (in[Pb].find(a)==in[Pb].end())
{
comin[Pb].insert(Pa), in[Pb].insert(a);
comout[Pa].insert(Pb), out[Pa].insert({a, Pb});
ans+=sz[Pb];
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll n, m; cin >> n >> m;
for (ll i=1; i<=n; i++)
pa[i]=i, sz[i]=1;
for (ll i=1; i<=m; i++)
{
ll u, v; cin >> u >> v;
addedge(u, v), cout << ans << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
19800 KB |
Output is correct |
2 |
Correct |
5 ms |
19804 KB |
Output is correct |
3 |
Correct |
5 ms |
19800 KB |
Output is correct |
4 |
Correct |
5 ms |
19804 KB |
Output is correct |
5 |
Correct |
5 ms |
19800 KB |
Output is correct |
6 |
Correct |
6 ms |
19804 KB |
Output is correct |
7 |
Correct |
6 ms |
20056 KB |
Output is correct |
8 |
Correct |
6 ms |
20060 KB |
Output is correct |
9 |
Correct |
6 ms |
20056 KB |
Output is correct |
10 |
Correct |
11 ms |
20056 KB |
Output is correct |
11 |
Correct |
5 ms |
19800 KB |
Output is correct |
12 |
Correct |
5 ms |
19800 KB |
Output is correct |
13 |
Correct |
6 ms |
19800 KB |
Output is correct |
14 |
Correct |
5 ms |
19800 KB |
Output is correct |
15 |
Correct |
5 ms |
19804 KB |
Output is correct |
16 |
Correct |
5 ms |
19800 KB |
Output is correct |
17 |
Correct |
5 ms |
19800 KB |
Output is correct |
18 |
Correct |
5 ms |
19800 KB |
Output is correct |
19 |
Correct |
6 ms |
19800 KB |
Output is correct |
20 |
Correct |
6 ms |
19800 KB |
Output is correct |
21 |
Correct |
6 ms |
20056 KB |
Output is correct |
22 |
Correct |
6 ms |
19800 KB |
Output is correct |
23 |
Correct |
6 ms |
20056 KB |
Output is correct |
24 |
Correct |
6 ms |
20056 KB |
Output is correct |
25 |
Correct |
5 ms |
20056 KB |
Output is correct |
26 |
Correct |
5 ms |
19804 KB |
Output is correct |
27 |
Correct |
5 ms |
20056 KB |
Output is correct |
28 |
Correct |
6 ms |
19800 KB |
Output is correct |
29 |
Correct |
6 ms |
19800 KB |
Output is correct |
30 |
Correct |
5 ms |
20056 KB |
Output is correct |
31 |
Correct |
5 ms |
20056 KB |
Output is correct |
32 |
Correct |
5 ms |
19800 KB |
Output is correct |
33 |
Correct |
6 ms |
20056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
19800 KB |
Output is correct |
2 |
Correct |
5 ms |
19804 KB |
Output is correct |
3 |
Correct |
5 ms |
19800 KB |
Output is correct |
4 |
Correct |
5 ms |
19804 KB |
Output is correct |
5 |
Correct |
5 ms |
19800 KB |
Output is correct |
6 |
Correct |
6 ms |
19804 KB |
Output is correct |
7 |
Correct |
6 ms |
20056 KB |
Output is correct |
8 |
Correct |
6 ms |
20060 KB |
Output is correct |
9 |
Correct |
6 ms |
20056 KB |
Output is correct |
10 |
Correct |
11 ms |
20056 KB |
Output is correct |
11 |
Correct |
5 ms |
19800 KB |
Output is correct |
12 |
Correct |
5 ms |
19800 KB |
Output is correct |
13 |
Correct |
6 ms |
19800 KB |
Output is correct |
14 |
Correct |
5 ms |
19800 KB |
Output is correct |
15 |
Correct |
5 ms |
19804 KB |
Output is correct |
16 |
Correct |
5 ms |
19800 KB |
Output is correct |
17 |
Correct |
5 ms |
19800 KB |
Output is correct |
18 |
Correct |
5 ms |
19800 KB |
Output is correct |
19 |
Correct |
6 ms |
19800 KB |
Output is correct |
20 |
Correct |
6 ms |
19800 KB |
Output is correct |
21 |
Correct |
6 ms |
20056 KB |
Output is correct |
22 |
Correct |
6 ms |
19800 KB |
Output is correct |
23 |
Correct |
6 ms |
20056 KB |
Output is correct |
24 |
Correct |
6 ms |
20056 KB |
Output is correct |
25 |
Correct |
5 ms |
20056 KB |
Output is correct |
26 |
Correct |
5 ms |
19804 KB |
Output is correct |
27 |
Correct |
5 ms |
20056 KB |
Output is correct |
28 |
Correct |
6 ms |
19800 KB |
Output is correct |
29 |
Correct |
6 ms |
19800 KB |
Output is correct |
30 |
Correct |
5 ms |
20056 KB |
Output is correct |
31 |
Correct |
5 ms |
20056 KB |
Output is correct |
32 |
Correct |
5 ms |
19800 KB |
Output is correct |
33 |
Correct |
6 ms |
20056 KB |
Output is correct |
34 |
Correct |
7 ms |
20056 KB |
Output is correct |
35 |
Correct |
70 ms |
26384 KB |
Output is correct |
36 |
Correct |
81 ms |
29812 KB |
Output is correct |
37 |
Correct |
81 ms |
30104 KB |
Output is correct |
38 |
Correct |
83 ms |
29400 KB |
Output is correct |
39 |
Correct |
7 ms |
20056 KB |
Output is correct |
40 |
Correct |
8 ms |
20312 KB |
Output is correct |
41 |
Correct |
8 ms |
20312 KB |
Output is correct |
42 |
Correct |
10 ms |
20056 KB |
Output is correct |
43 |
Correct |
8 ms |
20312 KB |
Output is correct |
44 |
Correct |
8 ms |
20316 KB |
Output is correct |
45 |
Correct |
7 ms |
20040 KB |
Output is correct |
46 |
Correct |
7 ms |
20060 KB |
Output is correct |
47 |
Correct |
8 ms |
20312 KB |
Output is correct |
48 |
Correct |
8 ms |
20312 KB |
Output is correct |
49 |
Correct |
16 ms |
21596 KB |
Output is correct |
50 |
Correct |
88 ms |
30292 KB |
Output is correct |
51 |
Correct |
10 ms |
20568 KB |
Output is correct |
52 |
Correct |
70 ms |
27800 KB |
Output is correct |
53 |
Correct |
14 ms |
21336 KB |
Output is correct |
54 |
Correct |
76 ms |
29008 KB |
Output is correct |
55 |
Correct |
12 ms |
20828 KB |
Output is correct |
56 |
Correct |
10 ms |
20824 KB |
Output is correct |
57 |
Correct |
10 ms |
21080 KB |
Output is correct |
58 |
Correct |
10 ms |
21080 KB |
Output is correct |
59 |
Correct |
9 ms |
20312 KB |
Output is correct |
60 |
Correct |
58 ms |
25172 KB |
Output is correct |
61 |
Correct |
9 ms |
20568 KB |
Output is correct |
62 |
Correct |
77 ms |
29264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
19800 KB |
Output is correct |
2 |
Correct |
5 ms |
19804 KB |
Output is correct |
3 |
Correct |
5 ms |
19800 KB |
Output is correct |
4 |
Correct |
5 ms |
19804 KB |
Output is correct |
5 |
Correct |
5 ms |
19800 KB |
Output is correct |
6 |
Correct |
6 ms |
19804 KB |
Output is correct |
7 |
Correct |
6 ms |
20056 KB |
Output is correct |
8 |
Correct |
6 ms |
20060 KB |
Output is correct |
9 |
Correct |
6 ms |
20056 KB |
Output is correct |
10 |
Correct |
11 ms |
20056 KB |
Output is correct |
11 |
Correct |
5 ms |
19800 KB |
Output is correct |
12 |
Correct |
5 ms |
19800 KB |
Output is correct |
13 |
Correct |
6 ms |
19800 KB |
Output is correct |
14 |
Correct |
5 ms |
19800 KB |
Output is correct |
15 |
Correct |
5 ms |
19804 KB |
Output is correct |
16 |
Correct |
5 ms |
19800 KB |
Output is correct |
17 |
Correct |
5 ms |
19800 KB |
Output is correct |
18 |
Correct |
5 ms |
19800 KB |
Output is correct |
19 |
Correct |
6 ms |
19800 KB |
Output is correct |
20 |
Correct |
6 ms |
19800 KB |
Output is correct |
21 |
Correct |
6 ms |
20056 KB |
Output is correct |
22 |
Correct |
6 ms |
19800 KB |
Output is correct |
23 |
Correct |
6 ms |
20056 KB |
Output is correct |
24 |
Correct |
6 ms |
20056 KB |
Output is correct |
25 |
Correct |
5 ms |
20056 KB |
Output is correct |
26 |
Correct |
5 ms |
19804 KB |
Output is correct |
27 |
Correct |
5 ms |
20056 KB |
Output is correct |
28 |
Correct |
6 ms |
19800 KB |
Output is correct |
29 |
Correct |
6 ms |
19800 KB |
Output is correct |
30 |
Correct |
5 ms |
20056 KB |
Output is correct |
31 |
Correct |
5 ms |
20056 KB |
Output is correct |
32 |
Correct |
5 ms |
19800 KB |
Output is correct |
33 |
Correct |
6 ms |
20056 KB |
Output is correct |
34 |
Correct |
7 ms |
20056 KB |
Output is correct |
35 |
Correct |
70 ms |
26384 KB |
Output is correct |
36 |
Correct |
81 ms |
29812 KB |
Output is correct |
37 |
Correct |
81 ms |
30104 KB |
Output is correct |
38 |
Correct |
83 ms |
29400 KB |
Output is correct |
39 |
Correct |
7 ms |
20056 KB |
Output is correct |
40 |
Correct |
8 ms |
20312 KB |
Output is correct |
41 |
Correct |
8 ms |
20312 KB |
Output is correct |
42 |
Correct |
10 ms |
20056 KB |
Output is correct |
43 |
Correct |
8 ms |
20312 KB |
Output is correct |
44 |
Correct |
8 ms |
20316 KB |
Output is correct |
45 |
Correct |
7 ms |
20040 KB |
Output is correct |
46 |
Correct |
7 ms |
20060 KB |
Output is correct |
47 |
Correct |
8 ms |
20312 KB |
Output is correct |
48 |
Correct |
8 ms |
20312 KB |
Output is correct |
49 |
Correct |
16 ms |
21596 KB |
Output is correct |
50 |
Correct |
88 ms |
30292 KB |
Output is correct |
51 |
Correct |
10 ms |
20568 KB |
Output is correct |
52 |
Correct |
70 ms |
27800 KB |
Output is correct |
53 |
Correct |
14 ms |
21336 KB |
Output is correct |
54 |
Correct |
76 ms |
29008 KB |
Output is correct |
55 |
Correct |
12 ms |
20828 KB |
Output is correct |
56 |
Correct |
10 ms |
20824 KB |
Output is correct |
57 |
Correct |
10 ms |
21080 KB |
Output is correct |
58 |
Correct |
10 ms |
21080 KB |
Output is correct |
59 |
Correct |
9 ms |
20312 KB |
Output is correct |
60 |
Correct |
58 ms |
25172 KB |
Output is correct |
61 |
Correct |
9 ms |
20568 KB |
Output is correct |
62 |
Correct |
77 ms |
29264 KB |
Output is correct |
63 |
Correct |
576 ms |
81744 KB |
Output is correct |
64 |
Correct |
588 ms |
81820 KB |
Output is correct |
65 |
Correct |
589 ms |
81788 KB |
Output is correct |
66 |
Incorrect |
123 ms |
33616 KB |
Output isn't correct |
67 |
Halted |
0 ms |
0 KB |
- |