#include<bits/stdc++.h>
using namespace std;
int ojc[200000];
int wiel[200000];
map<int,int> mapka[100010];
int wynik=0;
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(mapka[x].size()>mapka[y].size())
swap(x,y);
ojc[x]=y;
/*cout<<"srodek\n";
cout<<wynik<<"\n";
cout<<x<<": "<<mapka[x].size()<<"\n";
cout<<y<<": "<<mapka[y].size()<<"\n";*/
wynik-=mapka[x].size()*wiel[x];
wynik-=mapka[y].size()*wiel[y];
wynik-=wiel[x]*(wiel[x]-1);
wynik-=wiel[y]*(wiel[y]-1);
wiel[y]+=wiel[x];
for(auto it=mapka[x].begin();it!=mapka[x].end();it++)
{
if(find_u(it->first)!=y)
mapka[y][it->first]=1;
}
wynik+=wiel[y]*mapka[y].size();
wynik+=wiel[y]*(wiel[y]-1);
mapka[x].clear();
//cout<<"po srodku\n";
}
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;
if(find_u(x)==find_u(y))
{
cout<<wynik<<"\n";
continue;
}
if(mapka[find_u(x)].find(y)!=mapka[find_u(x)].end())
{
///jest juz krawedz od y do x, robi sie petla
union_f(x,y);
}
else{
if(mapka[find_u(y)].find(x)==mapka[find_u(y)].end())
wynik+=wiel[find_u(y)];
mapka[find_u(y)][x]=1;
}
cout<<wynik<<"\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |