#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 |
1 |
Correct |
10 ms |
9720 KB |
Output is correct |
2 |
Correct |
8 ms |
9720 KB |
Output is correct |
3 |
Correct |
9 ms |
9748 KB |
Output is correct |
4 |
Correct |
9 ms |
9800 KB |
Output is correct |
5 |
Correct |
8 ms |
9852 KB |
Output is correct |
6 |
Correct |
10 ms |
10020 KB |
Output is correct |
7 |
Correct |
8 ms |
10020 KB |
Output is correct |
8 |
Correct |
10 ms |
10020 KB |
Output is correct |
9 |
Correct |
8 ms |
10020 KB |
Output is correct |
10 |
Correct |
9 ms |
10020 KB |
Output is correct |
11 |
Correct |
9 ms |
10020 KB |
Output is correct |
12 |
Correct |
9 ms |
10156 KB |
Output is correct |
13 |
Correct |
9 ms |
10156 KB |
Output is correct |
14 |
Correct |
9 ms |
10156 KB |
Output is correct |
15 |
Correct |
9 ms |
10156 KB |
Output is correct |
16 |
Incorrect |
8 ms |
10156 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
10292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
11344 KB |
Output is correct |
2 |
Incorrect |
122 ms |
17280 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |