#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")
#define int long long
using namespace std;
const int mod=1e9+7,mxn=1e6+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]);}
int ans;
void merg(int a,int b){
a=find(a),b=find(b);
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);
if(rcon[a].count(i))todo.push({i,a});
}
for(auto i:rcon[b]){
con[i].erase(b);
con[i].insert(a);
if(con[a].count(i))todo.push({i,a});
}
}
int32_t main(){
fastio
int n,m;cin>>n>>m;
ans=0;
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];
if(!con[g].count(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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
142164 KB |
Output is correct |
2 |
Correct |
53 ms |
141952 KB |
Output is correct |
3 |
Correct |
49 ms |
142208 KB |
Output is correct |
4 |
Correct |
48 ms |
142168 KB |
Output is correct |
5 |
Incorrect |
53 ms |
142212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
142164 KB |
Output is correct |
2 |
Correct |
53 ms |
141952 KB |
Output is correct |
3 |
Correct |
49 ms |
142208 KB |
Output is correct |
4 |
Correct |
48 ms |
142168 KB |
Output is correct |
5 |
Incorrect |
53 ms |
142212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
142164 KB |
Output is correct |
2 |
Correct |
53 ms |
141952 KB |
Output is correct |
3 |
Correct |
49 ms |
142208 KB |
Output is correct |
4 |
Correct |
48 ms |
142168 KB |
Output is correct |
5 |
Incorrect |
53 ms |
142212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |