#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(),x.end()
#define abcorz ios_base::sync_with_stdio(false);cin.tie(0);
const int N=200005;
const int mod=1e9+7;
const int INF=1e9;
queue<pair<int,int>> q;
namespace{
vector<int> pa(N,-1);
int Find(int k){
return pa[k]<0?k:pa[k]=Find(pa[k]);
}
void onion(int a,int b,vector<set<int>> &adj,vector<set<int>> &to){
a=Find(a),b=Find(b);
if(a==b)return;
if(adj[a].size()>adj[b].size())swap(a,b);
pa[b]+=pa[a];
pa[a]=b;
for(int j:to[a]){
to[b].insert(j);
if(adj[b].find(j)!=adj[b].end()){
q.push({b,j});
}
}
for(int j:adj[a]){
adj[b].insert(j);
if(to[b].find(j)!=to[b].end()){
q.push({b,j});
}
}
}
};
vector<int> adj[N],vis(N);
int dfs(int k){
vis[k]=1;
int cnt=1;
for(int j:adj[k]){
if(vis[j])continue;
cnt+=dfs(j);
}
return cnt;
}
int32_t main(){
abcorz;
int n,m;cin>>n>>m;
vector<set<int>> adj(N),to(N);
for(int j=1;j<=n;j++){
adj[j].insert(j);
to[j].insert(j);
}
for(int i=0;i<m;i++){
int a,b;cin>>a>>b;
adj[Find(b)].insert(a);
to[Find(a)].insert(b);
if(adj[Find(a)].find(b)!=adj[Find(a)].end()){
q.push({a,b});
while(q.size()){
auto [a,b]=q.front();
q.pop();
onion(a,b,adj,to);
}
}
int ans=-n;
for(int j=1;j<=n;j++){
if(Find(j)!=j)continue;
ans+=(-pa[j])*(int)(adj[j].size());
}
cout<<ans<<endl;
}
return 0;
}
/*
5 15
5 3
2 4
4 2
3 2
5 2
4 3
*/
/*
4 4
1 2
2 3
3 2
3 1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
26972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
26972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
26972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |