#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)
{
Free[u]=true;
for(int64_t i=0;i<g[u].size();i++)
{
V.push_back(g[u][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=1;j<V.size();j++)
{
int64_t u=Find(V[0]);
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:26: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:55:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int64_t j=1;j<V.size();j++)
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2684 KB |
Output is correct |
2 |
Correct |
3 ms |
2684 KB |
Output is correct |
3 |
Correct |
3 ms |
2836 KB |
Output is correct |
4 |
Correct |
3 ms |
2836 KB |
Output is correct |
5 |
Correct |
3 ms |
2908 KB |
Output is correct |
6 |
Correct |
3 ms |
2908 KB |
Output is correct |
7 |
Correct |
3 ms |
2908 KB |
Output is correct |
8 |
Correct |
4 ms |
3004 KB |
Output is correct |
9 |
Correct |
4 ms |
3184 KB |
Output is correct |
10 |
Correct |
3 ms |
3184 KB |
Output is correct |
11 |
Correct |
3 ms |
3184 KB |
Output is correct |
12 |
Correct |
4 ms |
3184 KB |
Output is correct |
13 |
Correct |
3 ms |
3184 KB |
Output is correct |
14 |
Correct |
4 ms |
3184 KB |
Output is correct |
15 |
Correct |
3 ms |
3184 KB |
Output is correct |
16 |
Incorrect |
3 ms |
3184 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
3316 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
7532 KB |
Output is correct |
2 |
Incorrect |
92 ms |
11240 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |