#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];
}
int tcn = 0;
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());
vis[i] = 0;
}
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
9720 KB |
Output is correct |
2 |
Correct |
10 ms |
9720 KB |
Output is correct |
3 |
Correct |
8 ms |
9876 KB |
Output is correct |
4 |
Correct |
8 ms |
9876 KB |
Output is correct |
5 |
Correct |
9 ms |
10000 KB |
Output is correct |
6 |
Correct |
9 ms |
10000 KB |
Output is correct |
7 |
Correct |
9 ms |
10000 KB |
Output is correct |
8 |
Correct |
9 ms |
10032 KB |
Output is correct |
9 |
Correct |
9 ms |
10032 KB |
Output is correct |
10 |
Correct |
8 ms |
10032 KB |
Output is correct |
11 |
Correct |
8 ms |
10032 KB |
Output is correct |
12 |
Correct |
8 ms |
10032 KB |
Output is correct |
13 |
Correct |
9 ms |
10032 KB |
Output is correct |
14 |
Correct |
8 ms |
10032 KB |
Output is correct |
15 |
Correct |
8 ms |
10032 KB |
Output is correct |
16 |
Correct |
10 ms |
10032 KB |
Output is correct |
17 |
Correct |
9 ms |
10032 KB |
Output is correct |
18 |
Correct |
9 ms |
10032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
10100 KB |
Output is correct |
2 |
Correct |
10 ms |
10184 KB |
Output is correct |
3 |
Correct |
16 ms |
10632 KB |
Output is correct |
4 |
Correct |
45 ms |
12756 KB |
Output is correct |
5 |
Correct |
38 ms |
13600 KB |
Output is correct |
6 |
Correct |
58 ms |
16068 KB |
Output is correct |
7 |
Correct |
9 ms |
16068 KB |
Output is correct |
8 |
Correct |
12 ms |
16068 KB |
Output is correct |
9 |
Correct |
17 ms |
16068 KB |
Output is correct |
10 |
Correct |
24 ms |
16068 KB |
Output is correct |
11 |
Correct |
59 ms |
18912 KB |
Output is correct |
12 |
Correct |
15 ms |
18912 KB |
Output is correct |
13 |
Correct |
10 ms |
18912 KB |
Output is correct |
14 |
Correct |
13 ms |
18912 KB |
Output is correct |
15 |
Correct |
16 ms |
18912 KB |
Output is correct |
16 |
Correct |
18 ms |
18912 KB |
Output is correct |
17 |
Correct |
59 ms |
21148 KB |
Output is correct |
18 |
Correct |
9 ms |
21148 KB |
Output is correct |
19 |
Correct |
11 ms |
21148 KB |
Output is correct |
20 |
Correct |
61 ms |
23244 KB |
Output is correct |
21 |
Correct |
10 ms |
23244 KB |
Output is correct |
22 |
Correct |
10 ms |
23244 KB |
Output is correct |
23 |
Correct |
12 ms |
23244 KB |
Output is correct |
24 |
Correct |
10 ms |
23244 KB |
Output is correct |
25 |
Correct |
10 ms |
23244 KB |
Output is correct |
26 |
Correct |
10 ms |
23244 KB |
Output is correct |
27 |
Correct |
9 ms |
23244 KB |
Output is correct |
28 |
Correct |
9 ms |
23244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
23336 KB |
Output is correct |
2 |
Correct |
99 ms |
26824 KB |
Output is correct |
3 |
Correct |
93 ms |
29128 KB |
Output is correct |
4 |
Correct |
52 ms |
29128 KB |
Output is correct |
5 |
Correct |
29 ms |
29128 KB |
Output is correct |
6 |
Correct |
80 ms |
31828 KB |
Output is correct |
7 |
Correct |
82 ms |
34812 KB |
Output is correct |
8 |
Correct |
77 ms |
37060 KB |
Output is correct |
9 |
Correct |
54 ms |
37680 KB |
Output is correct |
10 |
Correct |
83 ms |
39724 KB |
Output is correct |
11 |
Correct |
115 ms |
43300 KB |
Output is correct |
12 |
Correct |
104 ms |
46244 KB |
Output is correct |
13 |
Correct |
41 ms |
46244 KB |
Output is correct |
14 |
Correct |
60 ms |
46244 KB |
Output is correct |
15 |
Correct |
28 ms |
46892 KB |
Output is correct |
16 |
Correct |
11 ms |
46892 KB |
Output is correct |
17 |
Correct |
9 ms |
46892 KB |
Output is correct |
18 |
Correct |
92 ms |
50836 KB |
Output is correct |
19 |
Correct |
101 ms |
53144 KB |
Output is correct |
20 |
Correct |
51 ms |
53144 KB |
Output is correct |
21 |
Correct |
30 ms |
53144 KB |
Output is correct |
22 |
Correct |
10 ms |
53144 KB |
Output is correct |
23 |
Correct |
33 ms |
53144 KB |
Output is correct |
24 |
Correct |
63 ms |
55960 KB |
Output is correct |
25 |
Correct |
41 ms |
58084 KB |
Output is correct |
26 |
Correct |
55 ms |
62920 KB |
Output is correct |
27 |
Correct |
104 ms |
63692 KB |
Output is correct |
28 |
Correct |
51 ms |
63692 KB |
Output is correct |
29 |
Correct |
50 ms |
63820 KB |
Output is correct |
30 |
Correct |
111 ms |
67384 KB |
Output is correct |
31 |
Correct |
53 ms |
67384 KB |
Output is correct |
32 |
Correct |
43 ms |
68432 KB |
Output is correct |
33 |
Correct |
40 ms |
69688 KB |
Output is correct |
34 |
Correct |
55 ms |
70140 KB |
Output is correct |
35 |
Correct |
51 ms |
71948 KB |
Output is correct |
36 |
Correct |
45 ms |
74380 KB |
Output is correct |
37 |
Correct |
48 ms |
74380 KB |
Output is correct |