#include <bits/stdc++.h>
using namespace std;
int64_t n,m,res=0,P[100005],Size[100005];
bool Free[100005];
vector<int64_t> V,g[100005];
int64_t Find(int64_t u)
{
if(u==P[u])
return u;
return P[u]=Find(P[u]);
}
void Union(int64_t u,int64_t v)
{
if(Size[u]<Size[v])
swap(u,v);
P[v]=u;
Size[u]+=Size[v];
}
void DFS(int64_t u)
{
V.push_back(u);
Free[u]=true;
for(int64_t i=0;i<g[u].size();i++)
if(Free[g[u][i]]==false)
DFS(g[u][i]);
}
int main()
{
ios_base::sync_with_stdio(false);
//freopen("TEST.INP","r",stdin);
cin>>n>>m;
int64_t u,v;
while(m--)
{
cin>>u>>v;
g[u].push_back(v);
}
for(int64_t i=1;i<=n;i++)
{
P[i]=i;
Size[i]=1;
}
for(int64_t i=1;i<=n;i++)
if(Free[i]==false&&g[i].size()>=2)
{
V.clear();
DFS(i);
for(int64_t j=2;j<V.size();j++)
{
int64_t u=Find(V[1]);
int64_t v=Find(V[j]);
if(u!=v)
Union(u,v);
}
}
for(int64_t i=1;i<=n;i++)
if(P[i]==i)
{
if(Size[i]>1)
res+=Size[i]*(Size[i]-1);
else
res+=g[i].size();
}
cout<<res;
}
Compilation message
friends.cpp: In function 'void DFS(int64_t)':
friends.cpp:27:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int64_t i=0;i<g[u].size();i++)
^
friends.cpp: In function 'int main()':
friends.cpp:53:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int64_t j=2;j<V.size();j++)
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2688 KB |
Output is correct |
2 |
Correct |
3 ms |
2688 KB |
Output is correct |
3 |
Correct |
3 ms |
2836 KB |
Output is correct |
4 |
Correct |
3 ms |
2836 KB |
Output is correct |
5 |
Incorrect |
4 ms |
2852 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
3128 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
58 ms |
5176 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |