This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
using namespace std;
const int mod=1e9+7,mxn=5e5+5,lg=30,inf=1e18,minf=-1e18;
int pa[mxn+10],sz[mxn+10];
set<int>con[mxn+10],rcon[mxn+10],fol[mxn+10];
// ->, <-, all node that follow in component||component of that node
queue<pii>todo;
int find(int u){return pa[u]==u?u:pa[u]=find(pa[u]);}
ll ans;
void merg(int a,int b){
a=find(a),b=find(b);
if(a==b)return;
if(sz[a]<sz[b])swap(a,b);
ans+=(fol[b].size()*sz[a])+(sz[b]*fol[a].size());
pa[b]=a;
sz[a]+=sz[b];
for(auto i:fol[b]){
if(fol[a].count(i))ans-=sz[a];
else fol[a].insert(i);
}
con[b].erase(a),con[a].erase(b),rcon[a].erase(b),rcon[b].erase(a);
for(auto i:con[b]){
rcon[i].erase(b);
rcon[i].insert(a);
con[a].insert(i);
if(rcon[a].count(i))todo.push({i,a});
}
for(auto i:rcon[b]){
con[i].erase(b);
con[i].insert(a);
rcon[a].insert(i);
if(con[a].count(i))todo.push({i,a});
}
}
int32_t main(){
fastio
int n,m;cin>>n>>m;
for(int i=1;i<=n;i++)pa[i]=i,sz[i]=1,fol[i].insert(i);
while(m--){
int a,b;cin>>a>>b;
b=find(b);
int g=find(a);
if(g==b){
cout<<ans<<'\n';
continue;
}
if(!fol[b].count(a)){
fol[b].insert(a);
ans+=sz[b];
con[g].insert(b);
rcon[b].insert(g);
if(con[b].count(g))todo.push({g,b});
}
while(!todo.empty())merg(todo.front().f,todo.front().s),todo.pop();
cout<<ans<<'\n';
}
return 0;
}
Compilation message (stderr)
joitter2.cpp:33:41: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
33 | const int mod=1e9+7,mxn=5e5+5,lg=30,inf=1e18,minf=-1e18;
| ^~~~
joitter2.cpp:33:51: warning: overflow in conversion from 'double' to 'int' changes value from '-1.0e+18' to '-2147483648' [-Woverflow]
33 | const int mod=1e9+7,mxn=5e5+5,lg=30,inf=1e18,minf=-1e18;
| ^~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |