#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
ll n,m,ans;
ll comp[100005];
vector<ll> dsu[100005];
set<ll> par[100005];
set<pll> edgenod[100005];
set<ll> edgecomp[100005];
void dsu_merge(ll c1,ll c2)
{
ll nr1=(int)dsu[c1].size()+(int)par[c1].size()+(int)edgenod[c1].size();
ll nr2=(int)dsu[c2].size()+(int)par[c2].size()+(int)edgenod[c2].size();
if(nr1<nr2)
swap(c1,c2);
ll sz2=dsu[c2].size();
ll sz1=dsu[c1].size();
ll val=(ll)dsu[c1].size()*(ll)dsu[c2].size();
val=val*2LL;
ans+=val;
val=(ll)par[c1].size()*sz2;
ans+=val;
for(ll x:par[c2])
{
edgenod[comp[x]].erase({x,c2});
if(edgecomp[comp[x]].find(c2)!=edgecomp[comp[x]].end())
edgecomp[comp[x]].erase(c2);
if(comp[x]==c1)
{
ans-=sz2;
continue;
}
if(par[c1].find(x)==par[c1].end())
{
par[c1].insert(x);
ans+=sz1;
edgenod[comp[x]].insert({x,c1});
edgecomp[comp[x]].insert(c1);
}
else
ans-=sz2;
}
for(pll p:edgenod[c2])
{
ll x=p.first;
ll c=p.second;
if(par[c].find(x)!=par[c].end())
par[c].erase(x);
if(c==c1)
{
ans-=(sz1+sz2);
continue;
}
edgenod[c1].insert({x,c});
edgecomp[c1].insert(c);
par[c].insert(x);
}
edgenod[c2].clear();
par[c2].clear();
edgecomp[c2].clear();
for(ll i:dsu[c2])
{
dsu[c1].push_back(i);
comp[i]=c1;
}
dsu[c2].clear();
vector<ll> v;
for(ll i:par[c1])
{
v.push_back(comp[i]);
if(v.size()>2000)
break;
}
for(ll i:v)
if(edgecomp[c1].find(i)!=edgecomp[c1].end())
if(edgecomp[i].find(c1)!=edgecomp[i].end())
{
dsu_merge(i,c1);
break;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++)
{
comp[i]=i;
dsu[i].push_back(i);
}
while(m--)
{
ll a,b;
cin>>a>>b;
ll c1=comp[a];
ll c2=comp[b];
if(c1==c2)
{
cout<<ans<<'\n';
continue;
}
if(edgecomp[c2].find(c1)!=edgecomp[c2].end())
{
dsu_merge(c1,c2);
cout<<ans<<'\n';
continue;
}
if(edgenod[c1].find({a,c2})==edgenod[c1].end())
{
ans+=dsu[c2].size();
edgenod[c1].insert({a,c2});
edgecomp[c1].insert(c2);
par[c2].insert(a);
}
cout<<ans<<'\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
4 ms |
16988 KB |
Output is correct |
3 |
Correct |
3 ms |
16984 KB |
Output is correct |
4 |
Correct |
4 ms |
16988 KB |
Output is correct |
5 |
Correct |
3 ms |
16988 KB |
Output is correct |
6 |
Correct |
3 ms |
16824 KB |
Output is correct |
7 |
Correct |
5 ms |
17008 KB |
Output is correct |
8 |
Correct |
4 ms |
17088 KB |
Output is correct |
9 |
Correct |
4 ms |
16988 KB |
Output is correct |
10 |
Correct |
3 ms |
16988 KB |
Output is correct |
11 |
Correct |
5 ms |
16988 KB |
Output is correct |
12 |
Correct |
4 ms |
16988 KB |
Output is correct |
13 |
Correct |
3 ms |
16988 KB |
Output is correct |
14 |
Correct |
4 ms |
16988 KB |
Output is correct |
15 |
Correct |
3 ms |
16988 KB |
Output is correct |
16 |
Correct |
4 ms |
16988 KB |
Output is correct |
17 |
Correct |
3 ms |
16988 KB |
Output is correct |
18 |
Correct |
3 ms |
16984 KB |
Output is correct |
19 |
Correct |
3 ms |
16984 KB |
Output is correct |
20 |
Correct |
4 ms |
16988 KB |
Output is correct |
21 |
Correct |
7 ms |
16988 KB |
Output is correct |
22 |
Correct |
4 ms |
16984 KB |
Output is correct |
23 |
Correct |
5 ms |
16988 KB |
Output is correct |
24 |
Correct |
4 ms |
16988 KB |
Output is correct |
25 |
Correct |
4 ms |
16988 KB |
Output is correct |
26 |
Correct |
4 ms |
16988 KB |
Output is correct |
27 |
Correct |
3 ms |
16984 KB |
Output is correct |
28 |
Correct |
4 ms |
16988 KB |
Output is correct |
29 |
Correct |
4 ms |
16988 KB |
Output is correct |
30 |
Correct |
3 ms |
17012 KB |
Output is correct |
31 |
Correct |
4 ms |
16988 KB |
Output is correct |
32 |
Correct |
4 ms |
16988 KB |
Output is correct |
33 |
Correct |
4 ms |
16988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
4 ms |
16988 KB |
Output is correct |
3 |
Correct |
3 ms |
16984 KB |
Output is correct |
4 |
Correct |
4 ms |
16988 KB |
Output is correct |
5 |
Correct |
3 ms |
16988 KB |
Output is correct |
6 |
Correct |
3 ms |
16824 KB |
Output is correct |
7 |
Correct |
5 ms |
17008 KB |
Output is correct |
8 |
Correct |
4 ms |
17088 KB |
Output is correct |
9 |
Correct |
4 ms |
16988 KB |
Output is correct |
10 |
Correct |
3 ms |
16988 KB |
Output is correct |
11 |
Correct |
5 ms |
16988 KB |
Output is correct |
12 |
Correct |
4 ms |
16988 KB |
Output is correct |
13 |
Correct |
3 ms |
16988 KB |
Output is correct |
14 |
Correct |
4 ms |
16988 KB |
Output is correct |
15 |
Correct |
3 ms |
16988 KB |
Output is correct |
16 |
Correct |
4 ms |
16988 KB |
Output is correct |
17 |
Correct |
3 ms |
16988 KB |
Output is correct |
18 |
Correct |
3 ms |
16984 KB |
Output is correct |
19 |
Correct |
3 ms |
16984 KB |
Output is correct |
20 |
Correct |
4 ms |
16988 KB |
Output is correct |
21 |
Correct |
7 ms |
16988 KB |
Output is correct |
22 |
Correct |
4 ms |
16984 KB |
Output is correct |
23 |
Correct |
5 ms |
16988 KB |
Output is correct |
24 |
Correct |
4 ms |
16988 KB |
Output is correct |
25 |
Correct |
4 ms |
16988 KB |
Output is correct |
26 |
Correct |
4 ms |
16988 KB |
Output is correct |
27 |
Correct |
3 ms |
16984 KB |
Output is correct |
28 |
Correct |
4 ms |
16988 KB |
Output is correct |
29 |
Correct |
4 ms |
16988 KB |
Output is correct |
30 |
Correct |
3 ms |
17012 KB |
Output is correct |
31 |
Correct |
4 ms |
16988 KB |
Output is correct |
32 |
Correct |
4 ms |
16988 KB |
Output is correct |
33 |
Correct |
4 ms |
16988 KB |
Output is correct |
34 |
Correct |
6 ms |
17204 KB |
Output is correct |
35 |
Correct |
70 ms |
24264 KB |
Output is correct |
36 |
Correct |
117 ms |
37972 KB |
Output is correct |
37 |
Correct |
107 ms |
38284 KB |
Output is correct |
38 |
Correct |
105 ms |
37664 KB |
Output is correct |
39 |
Correct |
5 ms |
16984 KB |
Output is correct |
40 |
Correct |
5 ms |
17496 KB |
Output is correct |
41 |
Correct |
5 ms |
17244 KB |
Output is correct |
42 |
Correct |
4 ms |
17176 KB |
Output is correct |
43 |
Correct |
19 ms |
17240 KB |
Output is correct |
44 |
Correct |
18 ms |
17244 KB |
Output is correct |
45 |
Correct |
5 ms |
16988 KB |
Output is correct |
46 |
Correct |
5 ms |
16988 KB |
Output is correct |
47 |
Correct |
6 ms |
17244 KB |
Output is correct |
48 |
Correct |
6 ms |
17292 KB |
Output is correct |
49 |
Correct |
20 ms |
22616 KB |
Output is correct |
50 |
Correct |
108 ms |
37956 KB |
Output is correct |
51 |
Correct |
21 ms |
17500 KB |
Output is correct |
52 |
Correct |
95 ms |
34152 KB |
Output is correct |
53 |
Correct |
21 ms |
23640 KB |
Output is correct |
54 |
Correct |
102 ms |
36692 KB |
Output is correct |
55 |
Correct |
25 ms |
24412 KB |
Output is correct |
56 |
Correct |
22 ms |
24412 KB |
Output is correct |
57 |
Correct |
20 ms |
23388 KB |
Output is correct |
58 |
Correct |
20 ms |
23644 KB |
Output is correct |
59 |
Correct |
5 ms |
17244 KB |
Output is correct |
60 |
Correct |
61 ms |
19552 KB |
Output is correct |
61 |
Correct |
8 ms |
17240 KB |
Output is correct |
62 |
Correct |
104 ms |
37472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
4 ms |
16988 KB |
Output is correct |
3 |
Correct |
3 ms |
16984 KB |
Output is correct |
4 |
Correct |
4 ms |
16988 KB |
Output is correct |
5 |
Correct |
3 ms |
16988 KB |
Output is correct |
6 |
Correct |
3 ms |
16824 KB |
Output is correct |
7 |
Correct |
5 ms |
17008 KB |
Output is correct |
8 |
Correct |
4 ms |
17088 KB |
Output is correct |
9 |
Correct |
4 ms |
16988 KB |
Output is correct |
10 |
Correct |
3 ms |
16988 KB |
Output is correct |
11 |
Correct |
5 ms |
16988 KB |
Output is correct |
12 |
Correct |
4 ms |
16988 KB |
Output is correct |
13 |
Correct |
3 ms |
16988 KB |
Output is correct |
14 |
Correct |
4 ms |
16988 KB |
Output is correct |
15 |
Correct |
3 ms |
16988 KB |
Output is correct |
16 |
Correct |
4 ms |
16988 KB |
Output is correct |
17 |
Correct |
3 ms |
16988 KB |
Output is correct |
18 |
Correct |
3 ms |
16984 KB |
Output is correct |
19 |
Correct |
3 ms |
16984 KB |
Output is correct |
20 |
Correct |
4 ms |
16988 KB |
Output is correct |
21 |
Correct |
7 ms |
16988 KB |
Output is correct |
22 |
Correct |
4 ms |
16984 KB |
Output is correct |
23 |
Correct |
5 ms |
16988 KB |
Output is correct |
24 |
Correct |
4 ms |
16988 KB |
Output is correct |
25 |
Correct |
4 ms |
16988 KB |
Output is correct |
26 |
Correct |
4 ms |
16988 KB |
Output is correct |
27 |
Correct |
3 ms |
16984 KB |
Output is correct |
28 |
Correct |
4 ms |
16988 KB |
Output is correct |
29 |
Correct |
4 ms |
16988 KB |
Output is correct |
30 |
Correct |
3 ms |
17012 KB |
Output is correct |
31 |
Correct |
4 ms |
16988 KB |
Output is correct |
32 |
Correct |
4 ms |
16988 KB |
Output is correct |
33 |
Correct |
4 ms |
16988 KB |
Output is correct |
34 |
Correct |
6 ms |
17204 KB |
Output is correct |
35 |
Correct |
70 ms |
24264 KB |
Output is correct |
36 |
Correct |
117 ms |
37972 KB |
Output is correct |
37 |
Correct |
107 ms |
38284 KB |
Output is correct |
38 |
Correct |
105 ms |
37664 KB |
Output is correct |
39 |
Correct |
5 ms |
16984 KB |
Output is correct |
40 |
Correct |
5 ms |
17496 KB |
Output is correct |
41 |
Correct |
5 ms |
17244 KB |
Output is correct |
42 |
Correct |
4 ms |
17176 KB |
Output is correct |
43 |
Correct |
19 ms |
17240 KB |
Output is correct |
44 |
Correct |
18 ms |
17244 KB |
Output is correct |
45 |
Correct |
5 ms |
16988 KB |
Output is correct |
46 |
Correct |
5 ms |
16988 KB |
Output is correct |
47 |
Correct |
6 ms |
17244 KB |
Output is correct |
48 |
Correct |
6 ms |
17292 KB |
Output is correct |
49 |
Correct |
20 ms |
22616 KB |
Output is correct |
50 |
Correct |
108 ms |
37956 KB |
Output is correct |
51 |
Correct |
21 ms |
17500 KB |
Output is correct |
52 |
Correct |
95 ms |
34152 KB |
Output is correct |
53 |
Correct |
21 ms |
23640 KB |
Output is correct |
54 |
Correct |
102 ms |
36692 KB |
Output is correct |
55 |
Correct |
25 ms |
24412 KB |
Output is correct |
56 |
Correct |
22 ms |
24412 KB |
Output is correct |
57 |
Correct |
20 ms |
23388 KB |
Output is correct |
58 |
Correct |
20 ms |
23644 KB |
Output is correct |
59 |
Correct |
5 ms |
17244 KB |
Output is correct |
60 |
Correct |
61 ms |
19552 KB |
Output is correct |
61 |
Correct |
8 ms |
17240 KB |
Output is correct |
62 |
Correct |
104 ms |
37472 KB |
Output is correct |
63 |
Correct |
407 ms |
69636 KB |
Output is correct |
64 |
Correct |
418 ms |
69756 KB |
Output is correct |
65 |
Correct |
440 ms |
69568 KB |
Output is correct |
66 |
Correct |
98 ms |
23616 KB |
Output is correct |
67 |
Correct |
191 ms |
34880 KB |
Output is correct |
68 |
Correct |
91 ms |
23748 KB |
Output is correct |
69 |
Execution timed out |
5013 ms |
30444 KB |
Time limit exceeded |
70 |
Halted |
0 ms |
0 KB |
- |