/* in the name of allah */
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define F first
#define S second
#define mk make_pair
typedef long long ll;
const int N=1e5+7;
int n,m,a[N],vis[N];
ll ans,sz[N];
vector<int>st[N];
set<pair<int,int>>v[N];
vector<int>vec[N],cev[N];
void upd(int i,int x){
for(auto u:vec[i]){
v[a[u]].erase(mk(a[i],i));
if(a[u]==x || a[u]==a[i])continue;
v[a[u]].insert(mk(x,i));
}
for(auto u:cev[i]){
v[a[i]].erase(mk(a[u],u));
if(a[u]==x || a[u]==a[i])continue;
v[x].insert(mk(a[u],u));
}
a[i]=x;
}
void merge(int x,int y){
ans-=sz[x]*v[x].size();
ans-=sz[y]*v[y].size();
ans-=sz[x]*(sz[x]-1);
ans-=sz[y]*(sz[y]-1);
if(st[x].size()<st[y].size())swap(x,y);
for(auto i:st[y]){
upd(i,x);
st[x].pb(i);
}
vis[y]=1;
sz[x]+=sz[y];
ans+=sz[x]*(sz[x]-1);
ans+=sz[x]*v[x].size();
for(auto w:v[x]){
if(vis[w.F])a[100000000]=1;
}
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++){
a[i]=i;
sz[i]=1;
st[i].pb(i);
}
int u,V;
while(m--){
cin>>u>>V;
vec[u].pb(V);
cev[V].pb(u);
int x=a[u],y=a[V];
if(x==y){
cout<<ans<<endl;
continue;
}
if(!v[y].count(mk(x,u)))ans+=1ll*(sz[y]);
v[y].insert(mk(x,u));
auto it=v[x].lower_bound(mk(y,0));
if(it!=v[x].end() && it->F==y)merge(x,y);
cout<<ans<<endl;
}
}
Compilation message
joitter2.cpp: In function 'void merge(int, int)':
joitter2.cpp:49:26: warning: array subscript 100000000 is above array bounds of 'int [100007]' [-Warray-bounds]
49 | if(vis[w.F])a[100000000]=1;
| ~~~~~~~~~~~^
joitter2.cpp:13:9: note: while referencing 'a'
13 | int n,m,a[N],vis[N];
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
5 ms |
11988 KB |
Output is correct |
3 |
Incorrect |
6 ms |
11988 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
5 ms |
11988 KB |
Output is correct |
3 |
Incorrect |
6 ms |
11988 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
5 ms |
11988 KB |
Output is correct |
3 |
Incorrect |
6 ms |
11988 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |