#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
struct disj{
int pa[100005], sz[100005];
void init(int n){
for(int i=1; i<=n; i++) pa[i] = i, sz[i] = 1;
}
int find(int x){
if(pa[x] == x) return x;
return pa[x] = find(pa[x]);
}
int size(int x){
return sz[find(x)];
}
int uni(int p, int q){
p = find(p);
q = find(q);
if(p == q) return 0;
pa[q] = p;
sz[p] += sz[q];
return 1;
}
}disj;
bool vis[100005];
int n,m;
vector<int> g[100005], ng[100005];
queue<int> q;
int main(){
scanf("%d %d",&n,&m);
disj.init(n);
for (int i=0; i<m; i++) {
int x,y;
scanf("%d %d",&x,&y);
g[x].push_back(y);
}
for (int i=1; i<=n; i++) {
for (int j=0; j+1<g[i].size(); j++) {
disj.uni(g[i][j],g[i][j+1]);
}
}
for (int i=1; i<=n; i++) {
int pos = i;
while (!vis[pos] && disj.size(pos) != 1) {
vis[pos] = 1;
if(!g[pos].empty()){
disj.uni(g[pos][0],pos);
pos = g[pos][0];
}
}
}
long long ret = 0;
memset(vis,0,sizeof(vis));
for (int i=1; i<=n; i++) {
if(!vis[disj.find(i)]){
vis[disj.find(i)] = 1;
ret += 1ll * disj.size(i) * (disj.size(i) - 1);
}
}
for (int i=1; i<=n; i++) {
for (int &j : g[i]){
if(disj.find(i) != disj.find(j)) ret++;
}
}
printf("%lld",ret);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6780 KB |
Output is correct |
2 |
Correct |
0 ms |
6780 KB |
Output is correct |
3 |
Correct |
0 ms |
6780 KB |
Output is correct |
4 |
Correct |
2 ms |
6780 KB |
Output is correct |
5 |
Correct |
0 ms |
6780 KB |
Output is correct |
6 |
Correct |
0 ms |
6780 KB |
Output is correct |
7 |
Correct |
0 ms |
6780 KB |
Output is correct |
8 |
Correct |
0 ms |
6780 KB |
Output is correct |
9 |
Correct |
0 ms |
6780 KB |
Output is correct |
10 |
Correct |
2 ms |
6780 KB |
Output is correct |
11 |
Correct |
0 ms |
6780 KB |
Output is correct |
12 |
Correct |
0 ms |
6780 KB |
Output is correct |
13 |
Correct |
0 ms |
6780 KB |
Output is correct |
14 |
Correct |
0 ms |
6780 KB |
Output is correct |
15 |
Correct |
0 ms |
6780 KB |
Output is correct |
16 |
Correct |
0 ms |
6780 KB |
Output is correct |
17 |
Correct |
0 ms |
6780 KB |
Output is correct |
18 |
Correct |
0 ms |
6780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6780 KB |
Output is correct |
2 |
Correct |
3 ms |
6780 KB |
Output is correct |
3 |
Correct |
4 ms |
6912 KB |
Output is correct |
4 |
Correct |
43 ms |
7704 KB |
Output is correct |
5 |
Correct |
21 ms |
7440 KB |
Output is correct |
6 |
Correct |
24 ms |
8100 KB |
Output is correct |
7 |
Correct |
0 ms |
6780 KB |
Output is correct |
8 |
Correct |
5 ms |
6780 KB |
Output is correct |
9 |
Correct |
9 ms |
6912 KB |
Output is correct |
10 |
Correct |
20 ms |
7044 KB |
Output is correct |
11 |
Correct |
59 ms |
8100 KB |
Output is correct |
12 |
Correct |
0 ms |
6912 KB |
Output is correct |
13 |
Correct |
0 ms |
6780 KB |
Output is correct |
14 |
Correct |
0 ms |
6780 KB |
Output is correct |
15 |
Correct |
3 ms |
6780 KB |
Output is correct |
16 |
Correct |
11 ms |
7044 KB |
Output is correct |
17 |
Correct |
56 ms |
7968 KB |
Output is correct |
18 |
Correct |
4 ms |
6912 KB |
Output is correct |
19 |
Correct |
2 ms |
6912 KB |
Output is correct |
20 |
Correct |
74 ms |
8100 KB |
Output is correct |
21 |
Correct |
2 ms |
6780 KB |
Output is correct |
22 |
Correct |
1 ms |
6780 KB |
Output is correct |
23 |
Correct |
6 ms |
6912 KB |
Output is correct |
24 |
Correct |
4 ms |
6780 KB |
Output is correct |
25 |
Correct |
2 ms |
6912 KB |
Output is correct |
26 |
Correct |
0 ms |
6780 KB |
Output is correct |
27 |
Correct |
0 ms |
6912 KB |
Output is correct |
28 |
Correct |
0 ms |
6780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
7836 KB |
Output is correct |
2 |
Correct |
97 ms |
9552 KB |
Output is correct |
3 |
Correct |
101 ms |
9552 KB |
Output is correct |
4 |
Correct |
50 ms |
8628 KB |
Output is correct |
5 |
Correct |
25 ms |
7968 KB |
Output is correct |
6 |
Correct |
101 ms |
8760 KB |
Output is correct |
7 |
Correct |
75 ms |
9552 KB |
Output is correct |
8 |
Correct |
72 ms |
9552 KB |
Output is correct |
9 |
Correct |
50 ms |
8760 KB |
Output is correct |
10 |
Correct |
76 ms |
9156 KB |
Output is correct |
11 |
Correct |
107 ms |
9552 KB |
Output is correct |
12 |
Correct |
114 ms |
9816 KB |
Output is correct |
13 |
Correct |
41 ms |
9816 KB |
Output is correct |
14 |
Correct |
61 ms |
8232 KB |
Output is correct |
15 |
Correct |
28 ms |
7968 KB |
Output is correct |
16 |
Correct |
0 ms |
6780 KB |
Output is correct |
17 |
Correct |
3 ms |
6780 KB |
Output is correct |
18 |
Correct |
107 ms |
9156 KB |
Output is correct |
19 |
Correct |
91 ms |
9156 KB |
Output is correct |
20 |
Correct |
56 ms |
8364 KB |
Output is correct |
21 |
Correct |
19 ms |
7836 KB |
Output is correct |
22 |
Correct |
0 ms |
6780 KB |
Output is correct |
23 |
Correct |
38 ms |
7440 KB |
Output is correct |
24 |
Correct |
65 ms |
8364 KB |
Output is correct |
25 |
Correct |
33 ms |
9816 KB |
Output is correct |
26 |
Correct |
41 ms |
10180 KB |
Output is correct |
27 |
Correct |
99 ms |
9816 KB |
Output is correct |
28 |
Correct |
41 ms |
9136 KB |
Output is correct |
29 |
Correct |
34 ms |
9100 KB |
Output is correct |
30 |
Correct |
116 ms |
9552 KB |
Output is correct |
31 |
Correct |
43 ms |
8364 KB |
Output is correct |
32 |
Correct |
24 ms |
9816 KB |
Output is correct |
33 |
Correct |
12 ms |
9816 KB |
Output is correct |
34 |
Correct |
36 ms |
8760 KB |
Output is correct |
35 |
Correct |
47 ms |
9816 KB |
Output is correct |
36 |
Correct |
35 ms |
10028 KB |
Output is correct |
37 |
Correct |
53 ms |
9132 KB |
Output is correct |