#include<iostream>
#include<vector>
#include<set>
#include<deque>
using namespace std;
int P[100005];
vector<int> Elements[100005];
set<int> Followers[100005];
set<int> GroupFollows[100005];
set<int> NF[100005];
int parent(int x)
{
int _x=x;
while(P[x]!=0)
x=P[x];
while(P[_x]!=0)
{
int aux=P[_x];
P[_x]=x;
_x=aux;
}
return x;
}
long long rez;
void unite(int x, int y)
{
rez=rez-1LL*Elements[x].size()*(Elements[x].size()-1);
rez=rez-1LL*Elements[y].size()*(Elements[y].size()-1);
rez=rez-1LL*Elements[x].size()*NF[x].size();
rez=rez-1LL*Elements[y].size()*NF[y].size();
for(auto el:Elements[y])
{
NF[x].erase(el);
Elements[x].push_back(el);
}
Followers[x].erase(y);
Followers[y].erase(x);
GroupFollows[x].erase(y);
GroupFollows[y].erase(x);
for(auto follower:Followers[y])
{
GroupFollows[follower].erase(y);
GroupFollows[follower].insert(x);
}
for(auto gf:GroupFollows[y])
{
Followers[gf].erase(y);
Followers[gf].insert(x);
}
for(auto nf:NF[y])
{
if(parent(nf)!=x)
NF[x].insert(nf);
}
for(auto gf:GroupFollows[y])
GroupFollows[x].insert(gf);
for(auto follower:Followers[y])
Followers[x].insert(follower);
P[y]=x;
rez=rez+1LL*Elements[x].size()*(Elements[x].size()-1);
rez=rez+1LL*Elements[x].size()*NF[x].size();
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,m;
cin>>n>>m;
for(int i=1; i<=n; i++)
Elements[i].push_back(i);
rez=0;
for(int i=1; i<=m; i++)
{
int x,y;
cin>>x>>y;
int px=parent(x);
int py=parent(y);
if(px==py)
{
cout<<rez<<"\n";
continue;
}
if(NF[py].find(x)==NF[py].end())
{
rez+=Elements[py].size();
Followers[py].insert(px);
NF[py].insert(x);
}
GroupFollows[px].insert(py);
deque<pair<int,int> > Q;
if(GroupFollows[py].find(px)!=GroupFollows[py].end())
Q.push_back({px,py});
while(!Q.empty())
{
int _x=parent(Q.front().first);
int _y=parent(Q.front().second);
Q.pop_front();
if(Elements[_x].size()<Elements[_y].size())
swap(_x,_y);
if(_x==_y)
continue;
unite(_x,_y);
for(auto gf:GroupFollows[_y])
{
if(Followers[_x].find(gf)!=Followers[_x].end())
Q.push_back({_x,gf});
}
for(auto follower:Followers[_y])
{
if(follower!=_x && GroupFollows[_x].find(follower)!=GroupFollows[_x].end())
Q.push_back({_x,follower});
}
}
cout<<rez<<"\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
16768 KB |
Output is correct |
2 |
Correct |
14 ms |
16768 KB |
Output is correct |
3 |
Correct |
13 ms |
16768 KB |
Output is correct |
4 |
Correct |
14 ms |
16768 KB |
Output is correct |
5 |
Correct |
14 ms |
16768 KB |
Output is correct |
6 |
Correct |
13 ms |
16768 KB |
Output is correct |
7 |
Correct |
15 ms |
16896 KB |
Output is correct |
8 |
Correct |
14 ms |
16896 KB |
Output is correct |
9 |
Correct |
15 ms |
16896 KB |
Output is correct |
10 |
Correct |
14 ms |
16768 KB |
Output is correct |
11 |
Correct |
14 ms |
16768 KB |
Output is correct |
12 |
Correct |
13 ms |
16768 KB |
Output is correct |
13 |
Correct |
14 ms |
16768 KB |
Output is correct |
14 |
Correct |
14 ms |
16768 KB |
Output is correct |
15 |
Correct |
14 ms |
16768 KB |
Output is correct |
16 |
Correct |
14 ms |
16768 KB |
Output is correct |
17 |
Correct |
13 ms |
16768 KB |
Output is correct |
18 |
Correct |
14 ms |
16768 KB |
Output is correct |
19 |
Correct |
14 ms |
16768 KB |
Output is correct |
20 |
Correct |
16 ms |
16896 KB |
Output is correct |
21 |
Correct |
17 ms |
16896 KB |
Output is correct |
22 |
Correct |
14 ms |
16768 KB |
Output is correct |
23 |
Correct |
14 ms |
16896 KB |
Output is correct |
24 |
Correct |
14 ms |
16768 KB |
Output is correct |
25 |
Correct |
14 ms |
16896 KB |
Output is correct |
26 |
Correct |
14 ms |
16640 KB |
Output is correct |
27 |
Correct |
14 ms |
16768 KB |
Output is correct |
28 |
Correct |
14 ms |
16768 KB |
Output is correct |
29 |
Correct |
14 ms |
16768 KB |
Output is correct |
30 |
Correct |
13 ms |
16768 KB |
Output is correct |
31 |
Correct |
16 ms |
16768 KB |
Output is correct |
32 |
Correct |
14 ms |
16844 KB |
Output is correct |
33 |
Correct |
14 ms |
16896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
16768 KB |
Output is correct |
2 |
Correct |
14 ms |
16768 KB |
Output is correct |
3 |
Correct |
13 ms |
16768 KB |
Output is correct |
4 |
Correct |
14 ms |
16768 KB |
Output is correct |
5 |
Correct |
14 ms |
16768 KB |
Output is correct |
6 |
Correct |
13 ms |
16768 KB |
Output is correct |
7 |
Correct |
15 ms |
16896 KB |
Output is correct |
8 |
Correct |
14 ms |
16896 KB |
Output is correct |
9 |
Correct |
15 ms |
16896 KB |
Output is correct |
10 |
Correct |
14 ms |
16768 KB |
Output is correct |
11 |
Correct |
14 ms |
16768 KB |
Output is correct |
12 |
Correct |
13 ms |
16768 KB |
Output is correct |
13 |
Correct |
14 ms |
16768 KB |
Output is correct |
14 |
Correct |
14 ms |
16768 KB |
Output is correct |
15 |
Correct |
14 ms |
16768 KB |
Output is correct |
16 |
Correct |
14 ms |
16768 KB |
Output is correct |
17 |
Correct |
13 ms |
16768 KB |
Output is correct |
18 |
Correct |
14 ms |
16768 KB |
Output is correct |
19 |
Correct |
14 ms |
16768 KB |
Output is correct |
20 |
Correct |
16 ms |
16896 KB |
Output is correct |
21 |
Correct |
17 ms |
16896 KB |
Output is correct |
22 |
Correct |
14 ms |
16768 KB |
Output is correct |
23 |
Correct |
14 ms |
16896 KB |
Output is correct |
24 |
Correct |
14 ms |
16768 KB |
Output is correct |
25 |
Correct |
14 ms |
16896 KB |
Output is correct |
26 |
Correct |
14 ms |
16640 KB |
Output is correct |
27 |
Correct |
14 ms |
16768 KB |
Output is correct |
28 |
Correct |
14 ms |
16768 KB |
Output is correct |
29 |
Correct |
14 ms |
16768 KB |
Output is correct |
30 |
Correct |
13 ms |
16768 KB |
Output is correct |
31 |
Correct |
16 ms |
16768 KB |
Output is correct |
32 |
Correct |
14 ms |
16844 KB |
Output is correct |
33 |
Correct |
14 ms |
16896 KB |
Output is correct |
34 |
Correct |
16 ms |
17024 KB |
Output is correct |
35 |
Correct |
109 ms |
22648 KB |
Output is correct |
36 |
Correct |
153 ms |
25744 KB |
Output is correct |
37 |
Correct |
143 ms |
25592 KB |
Output is correct |
38 |
Correct |
132 ms |
25080 KB |
Output is correct |
39 |
Correct |
19 ms |
17280 KB |
Output is correct |
40 |
Correct |
18 ms |
17152 KB |
Output is correct |
41 |
Correct |
18 ms |
17152 KB |
Output is correct |
42 |
Correct |
17 ms |
17024 KB |
Output is correct |
43 |
Correct |
19 ms |
17152 KB |
Output is correct |
44 |
Correct |
18 ms |
17152 KB |
Output is correct |
45 |
Correct |
16 ms |
17024 KB |
Output is correct |
46 |
Correct |
17 ms |
17024 KB |
Output is correct |
47 |
Correct |
18 ms |
17152 KB |
Output is correct |
48 |
Correct |
20 ms |
17280 KB |
Output is correct |
49 |
Correct |
26 ms |
18048 KB |
Output is correct |
50 |
Correct |
143 ms |
25592 KB |
Output is correct |
51 |
Correct |
23 ms |
17408 KB |
Output is correct |
52 |
Correct |
124 ms |
23800 KB |
Output is correct |
53 |
Correct |
26 ms |
17920 KB |
Output is correct |
54 |
Correct |
133 ms |
24696 KB |
Output is correct |
55 |
Correct |
20 ms |
17536 KB |
Output is correct |
56 |
Correct |
20 ms |
17536 KB |
Output is correct |
57 |
Correct |
19 ms |
17536 KB |
Output is correct |
58 |
Correct |
21 ms |
17536 KB |
Output is correct |
59 |
Correct |
17 ms |
17152 KB |
Output is correct |
60 |
Correct |
100 ms |
22024 KB |
Output is correct |
61 |
Correct |
20 ms |
17280 KB |
Output is correct |
62 |
Correct |
139 ms |
25040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
16768 KB |
Output is correct |
2 |
Correct |
14 ms |
16768 KB |
Output is correct |
3 |
Correct |
13 ms |
16768 KB |
Output is correct |
4 |
Correct |
14 ms |
16768 KB |
Output is correct |
5 |
Correct |
14 ms |
16768 KB |
Output is correct |
6 |
Correct |
13 ms |
16768 KB |
Output is correct |
7 |
Correct |
15 ms |
16896 KB |
Output is correct |
8 |
Correct |
14 ms |
16896 KB |
Output is correct |
9 |
Correct |
15 ms |
16896 KB |
Output is correct |
10 |
Correct |
14 ms |
16768 KB |
Output is correct |
11 |
Correct |
14 ms |
16768 KB |
Output is correct |
12 |
Correct |
13 ms |
16768 KB |
Output is correct |
13 |
Correct |
14 ms |
16768 KB |
Output is correct |
14 |
Correct |
14 ms |
16768 KB |
Output is correct |
15 |
Correct |
14 ms |
16768 KB |
Output is correct |
16 |
Correct |
14 ms |
16768 KB |
Output is correct |
17 |
Correct |
13 ms |
16768 KB |
Output is correct |
18 |
Correct |
14 ms |
16768 KB |
Output is correct |
19 |
Correct |
14 ms |
16768 KB |
Output is correct |
20 |
Correct |
16 ms |
16896 KB |
Output is correct |
21 |
Correct |
17 ms |
16896 KB |
Output is correct |
22 |
Correct |
14 ms |
16768 KB |
Output is correct |
23 |
Correct |
14 ms |
16896 KB |
Output is correct |
24 |
Correct |
14 ms |
16768 KB |
Output is correct |
25 |
Correct |
14 ms |
16896 KB |
Output is correct |
26 |
Correct |
14 ms |
16640 KB |
Output is correct |
27 |
Correct |
14 ms |
16768 KB |
Output is correct |
28 |
Correct |
14 ms |
16768 KB |
Output is correct |
29 |
Correct |
14 ms |
16768 KB |
Output is correct |
30 |
Correct |
13 ms |
16768 KB |
Output is correct |
31 |
Correct |
16 ms |
16768 KB |
Output is correct |
32 |
Correct |
14 ms |
16844 KB |
Output is correct |
33 |
Correct |
14 ms |
16896 KB |
Output is correct |
34 |
Correct |
16 ms |
17024 KB |
Output is correct |
35 |
Correct |
109 ms |
22648 KB |
Output is correct |
36 |
Correct |
153 ms |
25744 KB |
Output is correct |
37 |
Correct |
143 ms |
25592 KB |
Output is correct |
38 |
Correct |
132 ms |
25080 KB |
Output is correct |
39 |
Correct |
19 ms |
17280 KB |
Output is correct |
40 |
Correct |
18 ms |
17152 KB |
Output is correct |
41 |
Correct |
18 ms |
17152 KB |
Output is correct |
42 |
Correct |
17 ms |
17024 KB |
Output is correct |
43 |
Correct |
19 ms |
17152 KB |
Output is correct |
44 |
Correct |
18 ms |
17152 KB |
Output is correct |
45 |
Correct |
16 ms |
17024 KB |
Output is correct |
46 |
Correct |
17 ms |
17024 KB |
Output is correct |
47 |
Correct |
18 ms |
17152 KB |
Output is correct |
48 |
Correct |
20 ms |
17280 KB |
Output is correct |
49 |
Correct |
26 ms |
18048 KB |
Output is correct |
50 |
Correct |
143 ms |
25592 KB |
Output is correct |
51 |
Correct |
23 ms |
17408 KB |
Output is correct |
52 |
Correct |
124 ms |
23800 KB |
Output is correct |
53 |
Correct |
26 ms |
17920 KB |
Output is correct |
54 |
Correct |
133 ms |
24696 KB |
Output is correct |
55 |
Correct |
20 ms |
17536 KB |
Output is correct |
56 |
Correct |
20 ms |
17536 KB |
Output is correct |
57 |
Correct |
19 ms |
17536 KB |
Output is correct |
58 |
Correct |
21 ms |
17536 KB |
Output is correct |
59 |
Correct |
17 ms |
17152 KB |
Output is correct |
60 |
Correct |
100 ms |
22024 KB |
Output is correct |
61 |
Correct |
20 ms |
17280 KB |
Output is correct |
62 |
Correct |
139 ms |
25040 KB |
Output is correct |
63 |
Correct |
572 ms |
67608 KB |
Output is correct |
64 |
Correct |
581 ms |
67708 KB |
Output is correct |
65 |
Correct |
574 ms |
67964 KB |
Output is correct |
66 |
Correct |
198 ms |
29808 KB |
Output is correct |
67 |
Correct |
409 ms |
38348 KB |
Output is correct |
68 |
Correct |
215 ms |
29848 KB |
Output is correct |
69 |
Correct |
475 ms |
33768 KB |
Output is correct |
70 |
Correct |
201 ms |
29808 KB |
Output is correct |
71 |
Correct |
204 ms |
30060 KB |
Output is correct |
72 |
Correct |
471 ms |
36980 KB |
Output is correct |
73 |
Correct |
464 ms |
37352 KB |
Output is correct |
74 |
Correct |
1077 ms |
65008 KB |
Output is correct |
75 |
Correct |
668 ms |
40428 KB |
Output is correct |
76 |
Correct |
810 ms |
52336 KB |
Output is correct |
77 |
Correct |
814 ms |
52556 KB |
Output is correct |
78 |
Correct |
261 ms |
35576 KB |
Output is correct |
79 |
Correct |
422 ms |
39284 KB |
Output is correct |
80 |
Correct |
269 ms |
34524 KB |
Output is correct |
81 |
Correct |
402 ms |
37744 KB |
Output is correct |
82 |
Correct |
880 ms |
54412 KB |
Output is correct |
83 |
Correct |
869 ms |
54388 KB |
Output is correct |
84 |
Correct |
653 ms |
53432 KB |
Output is correct |
85 |
Correct |
668 ms |
53680 KB |
Output is correct |
86 |
Correct |
255 ms |
34032 KB |
Output is correct |
87 |
Correct |
292 ms |
35948 KB |
Output is correct |
88 |
Correct |
482 ms |
37496 KB |
Output is correct |
89 |
Correct |
782 ms |
50160 KB |
Output is correct |