#include <bits/stdc++.h>
#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif
using namespace std;
set<pair<int,int>> adj[100000], radj[100000];
int color[100000], cnt[100000];
long long ans;
int find_(int a) {return color[a]=a==color[a] ? a:find_(color[a]);}
void erase_value(int a, int b, set<pair<int,int>> *adj)
{
for(;;) {
auto it=adj[a].lower_bound({b,0});
if(it==adj[a].end() || it->first!=b) break;
adj[a].erase(it);
}
}
void union_(int a, int b)
{
a=find_(a); b=find_(b);
if(a==b) return;
if(adj[a].size()+radj[a].size()<adj[b].size()+radj[b].size()) swap(a,b);
ans-=cnt[a]*(adj[a].size()+cnt[a]-1LL)+cnt[b]*(adj[b].size()+cnt[b]-1LL);
color[b]=a;
cnt[a]+=cnt[b];
erase_value(a,b,adj); erase_value(b,a,adj);
erase_value(a,b,radj); erase_value(b,a,radj);
for(auto n: adj[b]) {
radj[n.first].emplace(a,n.second);
adj[a].insert(n);
}
for(auto n: radj[b]) {
adj[n.first].emplace(a,n.second);
radj[a].insert(n);
}
ans+=cnt[a]*(adj[a].size()+cnt[a]-1LL);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
TEST(freopen("input.txt","r",stdin));
TEST(freopen("output.txt","w",stdout));
TEST(freopen("debug.txt","w",stderr));
int N, M;
cin>>N>>M;
for(int i=0;i<N;i++) cnt[color[i]=i]=1;
while(M--) {
int a, b, x, y;
cin>>a>>b;
if((x=find_(--a))==(y=find_(--b)) || adj[y].find({x,a})!=adj[y].end()) {
cout<<ans<<'\n';
continue;
}
adj[y].emplace(x,a);
radj[x].emplace(y,a);
ans+=cnt[y];
auto it=adj[x].lower_bound({y,0});
if(it!=adj[x].end() && it->first==y) union_(a,b);
cout<<ans<<'\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
9716 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
9716 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
9716 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |