#include<stdio.h>
#include<vector>
using namespace std;
vector<int>con[200010];
int f,r,n,m,x[100010],y[100010],group[100010],ranking[100010],cnt[100010],arr[100010],w,ch[100010];
long long dap;
int finding(int x)
{
if(x==group[x]) return x;
return finding(group[x]);
}
void union_find(int x,int y)
{
int p,q;
p=finding(x); q=finding(y);
if(ranking[p]==ranking[q]){
group[q]=p;
ranking[p]++;
}
else if(ranking[p]>ranking[q]) group[q]=p;
else group[p]=q;
}
int main()
{
int i,j,k;
scanf("%d %d",&n,&m);
for(i=1;i<=n;i++) group[i]=i;
for(i=1;i<=m;i++){
scanf("%d %d",&x[i],&y[i]);
con[x[i]].push_back(y[i]);
}
for(i=1;i<=n;i++){
k=con[i].size();
for(j=0;j<k-1;j++){
union_find(con[i][j],con[i][j+1]);
ch[con[i][j]]=1;
}
if(j>0) ch[con[i][j]]=1;
}
for(i=1;i<=n;i++){
if(ch[i]) arr[++r]=i;
}
while(f!=r){
f++;
if(con[arr[f]].size()==0) continue;
k=con[arr[f]][0];
union_find(arr[f],k);
if(ch[k]==0) ch[k]=1, arr[++r]=k;
}
for(i=1;i<=n;i++){
group[i]=finding(i);
cnt[group[i]]++;
}
for(i=1;i<=n;i++) dap+=(long long)cnt[i]*(long long)(cnt[i]-1);
for(i=1;i<=m;i++){
if(group[x[i]]!=group[y[i]]) dap++;
}
printf("%lld",dap);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
8628 KB |
Output is correct |
2 |
Correct |
4 ms |
8628 KB |
Output is correct |
3 |
Correct |
0 ms |
8628 KB |
Output is correct |
4 |
Correct |
0 ms |
8628 KB |
Output is correct |
5 |
Correct |
0 ms |
8628 KB |
Output is correct |
6 |
Correct |
0 ms |
8628 KB |
Output is correct |
7 |
Correct |
0 ms |
8628 KB |
Output is correct |
8 |
Correct |
0 ms |
8628 KB |
Output is correct |
9 |
Correct |
0 ms |
8628 KB |
Output is correct |
10 |
Correct |
0 ms |
8628 KB |
Output is correct |
11 |
Correct |
0 ms |
8628 KB |
Output is correct |
12 |
Correct |
0 ms |
8628 KB |
Output is correct |
13 |
Correct |
0 ms |
8628 KB |
Output is correct |
14 |
Correct |
0 ms |
8628 KB |
Output is correct |
15 |
Correct |
0 ms |
8628 KB |
Output is correct |
16 |
Correct |
4 ms |
8628 KB |
Output is correct |
17 |
Correct |
0 ms |
8628 KB |
Output is correct |
18 |
Correct |
0 ms |
8628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
8628 KB |
Output is correct |
2 |
Correct |
0 ms |
8628 KB |
Output is correct |
3 |
Correct |
4 ms |
8760 KB |
Output is correct |
4 |
Correct |
32 ms |
9288 KB |
Output is correct |
5 |
Correct |
24 ms |
9288 KB |
Output is correct |
6 |
Correct |
32 ms |
9288 KB |
Output is correct |
7 |
Correct |
0 ms |
8628 KB |
Output is correct |
8 |
Correct |
8 ms |
8628 KB |
Output is correct |
9 |
Correct |
8 ms |
8760 KB |
Output is correct |
10 |
Correct |
16 ms |
8892 KB |
Output is correct |
11 |
Correct |
36 ms |
9288 KB |
Output is correct |
12 |
Correct |
4 ms |
8760 KB |
Output is correct |
13 |
Correct |
0 ms |
8628 KB |
Output is correct |
14 |
Correct |
0 ms |
8628 KB |
Output is correct |
15 |
Correct |
4 ms |
8628 KB |
Output is correct |
16 |
Correct |
12 ms |
8892 KB |
Output is correct |
17 |
Correct |
28 ms |
9288 KB |
Output is correct |
18 |
Correct |
0 ms |
8760 KB |
Output is correct |
19 |
Correct |
0 ms |
8760 KB |
Output is correct |
20 |
Correct |
40 ms |
9288 KB |
Output is correct |
21 |
Correct |
4 ms |
8628 KB |
Output is correct |
22 |
Correct |
4 ms |
8628 KB |
Output is correct |
23 |
Correct |
8 ms |
8760 KB |
Output is correct |
24 |
Correct |
4 ms |
8628 KB |
Output is correct |
25 |
Correct |
0 ms |
8760 KB |
Output is correct |
26 |
Correct |
0 ms |
8628 KB |
Output is correct |
27 |
Correct |
4 ms |
8760 KB |
Output is correct |
28 |
Correct |
0 ms |
8628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
9156 KB |
Output is correct |
2 |
Incorrect |
48 ms |
10608 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |