#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){
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++;
else ans--;
}
}
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9684 KB |
Output is correct |
2 |
Correct |
9 ms |
9836 KB |
Output is correct |
3 |
Correct |
9 ms |
9836 KB |
Output is correct |
4 |
Incorrect |
8 ms |
9836 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
10 ms |
10224 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
55 ms |
13088 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |