// Code by Parsa Eslami
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORR(i,a,b) for(int i=a;i>=b;i--)
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define bit(i,j) ((j>>i)&1)
#define pb push_back
#define all(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
#define err(x) cerr<<#x<<" : "<<x<<'\n'
#define dmid int mid=(r+l)/2
#define lc 2*id
#define rc 2*id+1
using namespace std;
const int N=1e5+4;
ll ans;
map<int,int> mp[N];
bool mark[N];
map<int,bool> b[N];
vector<int> adj[N];
vector<int> adj_t[N];
vector<int> x[N];
int dsu[N],in[N];
int n;
int root(int v){
if(dsu[v]<0) return v;
return (dsu[v]=root(dsu[v]));
}
void ADJ(int u0,int v0){
int u=root(u0); int v=root(v0);
if(u==v) return;
adj[u0].pb(v0);
adj_t[v0].pb(u0);
if(!b[u0][v]){ ans+=(-dsu[v]); mp[u][v]++; in[v]++;}
b[u0][v]=1;
deque<int> dq;
if(mp[v][u]&&mp[u][v]){
if(dsu[u]<dsu[v]) swap(u,v);
dq.pb(u);
mark[u]=1;
while(!dq.empty()){
u=dq.front();
mark[u]=0;
dq.pop_front();
if(dsu[u]<dsu[v]) swap(u,v);
ans-=1LL*mp[u][v]*(-dsu[v]);
ans-=1LL*mp[v][u]*(-dsu[u]);
ans+=1LL*2*(-dsu[u])*(-dsu[v]);
in[v]-=mp[u][v]; in[u]-=mp[v][u];
ans+=1LL*in[u]*(-dsu[v]);
ans+=1LL*in[v]*(-dsu[u]);
in[v]+=in[u];
for(int y:x[u]) for(int w:adj[y]) if(root(w)!=u&&root(w)!=v){
mp[v][root(w)]++;
if(!mark[root(w)]&&mp[root(w)][v]){
mark[root(w)]=1;
dq.pb(root(w));
}
}
for(int y:x[u]) for(int w:adj_t[y]){
if(root(w)==u||root(w)==v) continue;
if(b[w][v]==0) mp[root(w)][v]++;
else ans-=(-dsu[u]-dsu[v]),in[v]--;
b[w][v]=1;
if(!mark[root(w)]&&mp[v][root(w)]){
mark[root(w)]=1;
dq.pb(root(w));
}
}
dsu[v]+=dsu[u]; dsu[u]=v;
for(int w:x[u]) x[v].pb(w);
x[u].clear();
}
}
}
int main(){
iostream::sync_with_stdio(0); cin.tie(0);
fill(dsu,dsu+N,-1);
int m;
cin>>n>>m;
FOR(i,1,n) x[i].pb(i);
FOR(i,1,m){
int u,v; cin>>u>>v;
ADJ(u,v);
cout<<ans<<'\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
17108 KB |
Output is correct |
2 |
Correct |
8 ms |
17108 KB |
Output is correct |
3 |
Correct |
8 ms |
17108 KB |
Output is correct |
4 |
Correct |
9 ms |
17108 KB |
Output is correct |
5 |
Correct |
9 ms |
17108 KB |
Output is correct |
6 |
Incorrect |
11 ms |
17108 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
17108 KB |
Output is correct |
2 |
Correct |
8 ms |
17108 KB |
Output is correct |
3 |
Correct |
8 ms |
17108 KB |
Output is correct |
4 |
Correct |
9 ms |
17108 KB |
Output is correct |
5 |
Correct |
9 ms |
17108 KB |
Output is correct |
6 |
Incorrect |
11 ms |
17108 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
17108 KB |
Output is correct |
2 |
Correct |
8 ms |
17108 KB |
Output is correct |
3 |
Correct |
8 ms |
17108 KB |
Output is correct |
4 |
Correct |
9 ms |
17108 KB |
Output is correct |
5 |
Correct |
9 ms |
17108 KB |
Output is correct |
6 |
Incorrect |
11 ms |
17108 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |