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 <cstdio>
#include <cassert>
#include <bitset>
#include <string>
#include <sstream>
#include <algorithm>
#include <set>
#include <numeric>
#include <cmath>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <queue>
#include <numeric>
#include <iomanip>
#define ll long long
using namespace std;
const int MAXN = 400001;
int n,m,vis[MAXN],parent[MAXN],sz[MAXN];
vector<int> adj[MAXN];
int find(int u) { return u == parent[u] ? u : parent[u] = find(parent[u]); }
void mrg(int u,int v){
u = find(u) , v = find(v);
if(u == v)return;
parent[u] = v;
sz[v] += sz[u];
}
void dfs(int here,int root){
vis[here] = 1;
for(auto x:adj[here]){
if(vis[x]) mrg(root,x);
else{
mrg(root,x);
dfs(x,root);
}
}
}
int main(){
ios::sync_with_stdio(false); cin.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++){
parent[i] = i;
sz[i] = 1;
}
for(int i = 1; i <= m; i++){
int u,v; cin >> u >> v;
adj[u].push_back(v);
}
for(int i = 1; i <= n; i++){
if(adj[i].size() >= 2 && !vis[i]){
dfs(i,adj[i].front());
}
}
long long ans = 0;
for(int i = 1; i <= n; i++)if(i == find(i)) ans += 1LL * sz[i] * (sz[i] - 1);
for(int i = 1; i <= n; i++){
for(auto x:adj[i]){
if(find(i) != find(x)) ans++;
}
}
cout<<ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |