#include<bits/stdc++.h>
using namespace std;
#define int long long
set<pair<int,int> > A[200000];
set<int> B[200000];
int ojc[200000],wiel[200000];
int wynik=0;
void rozpatrz(int x,int y);
int find_u(int x)
{
if(ojc[x]==x)
return x;
else return ojc[x]=find_u(ojc[x]);
}
void union_f(int x,int y)
{
x=find_u(x);
y=find_u(y);
if(wiel[x]>wiel[y])
swap(x,y);
for(int i=0;i<2;i++)
{
while(true)
{
auto it=A[x].lower_bound({y,-1});
if(it==A[x].end())
break;
pair<int,int> para=*it;
if(para.first>y)
break;
A[x].erase(para);
B[para.first].erase(para.second);
wynik-=wiel[y];
}
swap(x,y);
}
wynik+=wiel[x]*wiel[y]*2;
vector<pair<int,int> > pomoc;
for(auto i:A[x])
pomoc.push_back({i.second,i.first});
for(auto i : A[x])
{
B[i.first].erase(i.second);
wynik-=wiel[i.first];
}
wynik-=wiel[x]*B[x].size();
wynik+=wiel[x]*B[y].size();
for(auto i:B[x])
A[find_u(i)].erase({x,i});
for(auto i:B[x])
pomoc.push_back({i,x});
wiel[y]+=wiel[x];
ojc[x]=y;
A[x].clear();
B[x].clear();
for(auto i:pomoc)
rozpatrz(i.first,i.second);
}
void rozpatrz(int x,int y)
{
if(find_u(x)==find_u(y))
return;
y=find_u(y);
auto it=A[y].lower_bound({find_u(x),-1});
if(it!=A[y].end())
{
pair<int,int> para=*it;
if(para.first==find_u(x))
{
union_f(x,y);
return;
}
}
if(B[y].find(x)!=B[y].end())
return;
B[y].insert(x);
A[find_u(x)].insert({y,x});
wynik+=wiel[y];
}
int32_t main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
ojc[i]=i,wiel[i]=1;
while(m--)
{
int x,y;
cin>>x>>y;
rozpatrz(x,y);
cout<<wynik<<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
22108 KB |
Output is correct |
2 |
Correct |
5 ms |
22108 KB |
Output is correct |
3 |
Correct |
5 ms |
22108 KB |
Output is correct |
4 |
Correct |
5 ms |
22168 KB |
Output is correct |
5 |
Correct |
4 ms |
22200 KB |
Output is correct |
6 |
Correct |
5 ms |
22104 KB |
Output is correct |
7 |
Correct |
5 ms |
22108 KB |
Output is correct |
8 |
Correct |
5 ms |
22108 KB |
Output is correct |
9 |
Correct |
5 ms |
22108 KB |
Output is correct |
10 |
Correct |
5 ms |
22108 KB |
Output is correct |
11 |
Correct |
5 ms |
22156 KB |
Output is correct |
12 |
Correct |
6 ms |
22360 KB |
Output is correct |
13 |
Correct |
4 ms |
22204 KB |
Output is correct |
14 |
Correct |
5 ms |
22108 KB |
Output is correct |
15 |
Correct |
5 ms |
22104 KB |
Output is correct |
16 |
Correct |
5 ms |
22104 KB |
Output is correct |
17 |
Correct |
5 ms |
22104 KB |
Output is correct |
18 |
Correct |
5 ms |
22104 KB |
Output is correct |
19 |
Correct |
5 ms |
22108 KB |
Output is correct |
20 |
Correct |
5 ms |
22204 KB |
Output is correct |
21 |
Correct |
5 ms |
22160 KB |
Output is correct |
22 |
Correct |
5 ms |
22108 KB |
Output is correct |
23 |
Correct |
5 ms |
22108 KB |
Output is correct |
24 |
Correct |
5 ms |
22108 KB |
Output is correct |
25 |
Correct |
6 ms |
22108 KB |
Output is correct |
26 |
Correct |
5 ms |
22104 KB |
Output is correct |
27 |
Correct |
5 ms |
22156 KB |
Output is correct |
28 |
Correct |
6 ms |
22108 KB |
Output is correct |
29 |
Correct |
5 ms |
22156 KB |
Output is correct |
30 |
Correct |
5 ms |
22104 KB |
Output is correct |
31 |
Correct |
5 ms |
22108 KB |
Output is correct |
32 |
Correct |
5 ms |
22108 KB |
Output is correct |
33 |
Correct |
5 ms |
22104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
22108 KB |
Output is correct |
2 |
Correct |
5 ms |
22108 KB |
Output is correct |
3 |
Correct |
5 ms |
22108 KB |
Output is correct |
4 |
Correct |
5 ms |
22168 KB |
Output is correct |
5 |
Correct |
4 ms |
22200 KB |
Output is correct |
6 |
Correct |
5 ms |
22104 KB |
Output is correct |
7 |
Correct |
5 ms |
22108 KB |
Output is correct |
8 |
Correct |
5 ms |
22108 KB |
Output is correct |
9 |
Correct |
5 ms |
22108 KB |
Output is correct |
10 |
Correct |
5 ms |
22108 KB |
Output is correct |
11 |
Correct |
5 ms |
22156 KB |
Output is correct |
12 |
Correct |
6 ms |
22360 KB |
Output is correct |
13 |
Correct |
4 ms |
22204 KB |
Output is correct |
14 |
Correct |
5 ms |
22108 KB |
Output is correct |
15 |
Correct |
5 ms |
22104 KB |
Output is correct |
16 |
Correct |
5 ms |
22104 KB |
Output is correct |
17 |
Correct |
5 ms |
22104 KB |
Output is correct |
18 |
Correct |
5 ms |
22104 KB |
Output is correct |
19 |
Correct |
5 ms |
22108 KB |
Output is correct |
20 |
Correct |
5 ms |
22204 KB |
Output is correct |
21 |
Correct |
5 ms |
22160 KB |
Output is correct |
22 |
Correct |
5 ms |
22108 KB |
Output is correct |
23 |
Correct |
5 ms |
22108 KB |
Output is correct |
24 |
Correct |
5 ms |
22108 KB |
Output is correct |
25 |
Correct |
6 ms |
22108 KB |
Output is correct |
26 |
Correct |
5 ms |
22104 KB |
Output is correct |
27 |
Correct |
5 ms |
22156 KB |
Output is correct |
28 |
Correct |
6 ms |
22108 KB |
Output is correct |
29 |
Correct |
5 ms |
22156 KB |
Output is correct |
30 |
Correct |
5 ms |
22104 KB |
Output is correct |
31 |
Correct |
5 ms |
22108 KB |
Output is correct |
32 |
Correct |
5 ms |
22108 KB |
Output is correct |
33 |
Correct |
5 ms |
22104 KB |
Output is correct |
34 |
Correct |
7 ms |
22364 KB |
Output is correct |
35 |
Correct |
62 ms |
27656 KB |
Output is correct |
36 |
Correct |
87 ms |
30148 KB |
Output is correct |
37 |
Correct |
80 ms |
30308 KB |
Output is correct |
38 |
Correct |
77 ms |
30032 KB |
Output is correct |
39 |
Correct |
6 ms |
22108 KB |
Output is correct |
40 |
Correct |
6 ms |
22360 KB |
Output is correct |
41 |
Correct |
6 ms |
22364 KB |
Output is correct |
42 |
Correct |
7 ms |
22108 KB |
Output is correct |
43 |
Correct |
6 ms |
22616 KB |
Output is correct |
44 |
Correct |
7 ms |
22364 KB |
Output is correct |
45 |
Correct |
6 ms |
22108 KB |
Output is correct |
46 |
Correct |
6 ms |
22108 KB |
Output is correct |
47 |
Correct |
6 ms |
22364 KB |
Output is correct |
48 |
Correct |
7 ms |
22364 KB |
Output is correct |
49 |
Correct |
12 ms |
22876 KB |
Output is correct |
50 |
Correct |
88 ms |
30352 KB |
Output is correct |
51 |
Correct |
9 ms |
22616 KB |
Output is correct |
52 |
Correct |
77 ms |
29012 KB |
Output is correct |
53 |
Correct |
12 ms |
23132 KB |
Output is correct |
54 |
Correct |
74 ms |
29704 KB |
Output is correct |
55 |
Correct |
9 ms |
22472 KB |
Output is correct |
56 |
Correct |
8 ms |
22720 KB |
Output is correct |
57 |
Correct |
7 ms |
22620 KB |
Output is correct |
58 |
Correct |
7 ms |
22620 KB |
Output is correct |
59 |
Correct |
6 ms |
22196 KB |
Output is correct |
60 |
Correct |
62 ms |
26968 KB |
Output is correct |
61 |
Correct |
8 ms |
22364 KB |
Output is correct |
62 |
Correct |
77 ms |
29864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
22108 KB |
Output is correct |
2 |
Correct |
5 ms |
22108 KB |
Output is correct |
3 |
Correct |
5 ms |
22108 KB |
Output is correct |
4 |
Correct |
5 ms |
22168 KB |
Output is correct |
5 |
Correct |
4 ms |
22200 KB |
Output is correct |
6 |
Correct |
5 ms |
22104 KB |
Output is correct |
7 |
Correct |
5 ms |
22108 KB |
Output is correct |
8 |
Correct |
5 ms |
22108 KB |
Output is correct |
9 |
Correct |
5 ms |
22108 KB |
Output is correct |
10 |
Correct |
5 ms |
22108 KB |
Output is correct |
11 |
Correct |
5 ms |
22156 KB |
Output is correct |
12 |
Correct |
6 ms |
22360 KB |
Output is correct |
13 |
Correct |
4 ms |
22204 KB |
Output is correct |
14 |
Correct |
5 ms |
22108 KB |
Output is correct |
15 |
Correct |
5 ms |
22104 KB |
Output is correct |
16 |
Correct |
5 ms |
22104 KB |
Output is correct |
17 |
Correct |
5 ms |
22104 KB |
Output is correct |
18 |
Correct |
5 ms |
22104 KB |
Output is correct |
19 |
Correct |
5 ms |
22108 KB |
Output is correct |
20 |
Correct |
5 ms |
22204 KB |
Output is correct |
21 |
Correct |
5 ms |
22160 KB |
Output is correct |
22 |
Correct |
5 ms |
22108 KB |
Output is correct |
23 |
Correct |
5 ms |
22108 KB |
Output is correct |
24 |
Correct |
5 ms |
22108 KB |
Output is correct |
25 |
Correct |
6 ms |
22108 KB |
Output is correct |
26 |
Correct |
5 ms |
22104 KB |
Output is correct |
27 |
Correct |
5 ms |
22156 KB |
Output is correct |
28 |
Correct |
6 ms |
22108 KB |
Output is correct |
29 |
Correct |
5 ms |
22156 KB |
Output is correct |
30 |
Correct |
5 ms |
22104 KB |
Output is correct |
31 |
Correct |
5 ms |
22108 KB |
Output is correct |
32 |
Correct |
5 ms |
22108 KB |
Output is correct |
33 |
Correct |
5 ms |
22104 KB |
Output is correct |
34 |
Correct |
7 ms |
22364 KB |
Output is correct |
35 |
Correct |
62 ms |
27656 KB |
Output is correct |
36 |
Correct |
87 ms |
30148 KB |
Output is correct |
37 |
Correct |
80 ms |
30308 KB |
Output is correct |
38 |
Correct |
77 ms |
30032 KB |
Output is correct |
39 |
Correct |
6 ms |
22108 KB |
Output is correct |
40 |
Correct |
6 ms |
22360 KB |
Output is correct |
41 |
Correct |
6 ms |
22364 KB |
Output is correct |
42 |
Correct |
7 ms |
22108 KB |
Output is correct |
43 |
Correct |
6 ms |
22616 KB |
Output is correct |
44 |
Correct |
7 ms |
22364 KB |
Output is correct |
45 |
Correct |
6 ms |
22108 KB |
Output is correct |
46 |
Correct |
6 ms |
22108 KB |
Output is correct |
47 |
Correct |
6 ms |
22364 KB |
Output is correct |
48 |
Correct |
7 ms |
22364 KB |
Output is correct |
49 |
Correct |
12 ms |
22876 KB |
Output is correct |
50 |
Correct |
88 ms |
30352 KB |
Output is correct |
51 |
Correct |
9 ms |
22616 KB |
Output is correct |
52 |
Correct |
77 ms |
29012 KB |
Output is correct |
53 |
Correct |
12 ms |
23132 KB |
Output is correct |
54 |
Correct |
74 ms |
29704 KB |
Output is correct |
55 |
Correct |
9 ms |
22472 KB |
Output is correct |
56 |
Correct |
8 ms |
22720 KB |
Output is correct |
57 |
Correct |
7 ms |
22620 KB |
Output is correct |
58 |
Correct |
7 ms |
22620 KB |
Output is correct |
59 |
Correct |
6 ms |
22196 KB |
Output is correct |
60 |
Correct |
62 ms |
26968 KB |
Output is correct |
61 |
Correct |
8 ms |
22364 KB |
Output is correct |
62 |
Correct |
77 ms |
29864 KB |
Output is correct |
63 |
Correct |
328 ms |
60928 KB |
Output is correct |
64 |
Correct |
334 ms |
60620 KB |
Output is correct |
65 |
Correct |
312 ms |
60496 KB |
Output is correct |
66 |
Correct |
66 ms |
26708 KB |
Output is correct |
67 |
Correct |
128 ms |
31428 KB |
Output is correct |
68 |
Correct |
70 ms |
26704 KB |
Output is correct |
69 |
Correct |
200 ms |
32068 KB |
Output is correct |
70 |
Correct |
73 ms |
26644 KB |
Output is correct |
71 |
Correct |
84 ms |
26684 KB |
Output is correct |
72 |
Correct |
167 ms |
31464 KB |
Output is correct |
73 |
Correct |
180 ms |
31504 KB |
Output is correct |
74 |
Correct |
409 ms |
43092 KB |
Output is correct |
75 |
Correct |
312 ms |
37156 KB |
Output is correct |
76 |
Correct |
429 ms |
41100 KB |
Output is correct |
77 |
Correct |
379 ms |
40964 KB |
Output is correct |
78 |
Correct |
146 ms |
30888 KB |
Output is correct |
79 |
Correct |
257 ms |
34680 KB |
Output is correct |
80 |
Correct |
141 ms |
31260 KB |
Output is correct |
81 |
Correct |
193 ms |
34408 KB |
Output is correct |
82 |
Correct |
401 ms |
47656 KB |
Output is correct |
83 |
Correct |
408 ms |
47728 KB |
Output is correct |
84 |
Correct |
346 ms |
48220 KB |
Output is correct |
85 |
Correct |
327 ms |
48028 KB |
Output is correct |
86 |
Correct |
85 ms |
25788 KB |
Output is correct |
87 |
Correct |
127 ms |
28188 KB |
Output is correct |
88 |
Correct |
149 ms |
31476 KB |
Output is correct |
89 |
Correct |
360 ms |
40276 KB |
Output is correct |