#include<stdio.h>
#include<vector>
using namespace std;
vector<int>con[200010];
int n,m,x[100010],y[100010],group[100010],ranking[100010],cnt[100010],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[x]==ranking[y])
{
group[y]=x;
ranking[x]++;
}
else if(ranking[x]>ranking[y]) group[y]=x;
else group[x]=y;
}
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] && con[i].size())
{
union_find(i,con[i][0]);
}
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
8236 KB |
Output is correct |
2 |
Correct |
0 ms |
8236 KB |
Output is correct |
3 |
Correct |
0 ms |
8236 KB |
Output is correct |
4 |
Incorrect |
0 ms |
8236 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
8236 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1000 ms |
8760 KB |
Program timed out |
2 |
Halted |
0 ms |
0 KB |
- |