#include <bits/stdc++.h>
using namespace std;
const int nmax = 1e5;
int n,m;
int t[nmax + 5], rang[nmax + 5];
vector<int> l[nmax + 5];
int fr[nmax + 5];
int rep(int x)
{
if(t[x]==x)
{
return x;
}
return rep(t[x]);
}
void uneste(int x, int y)
{
x = rep(x);
y = rep(y);
if(x==y)
{
return;
}
if(rang[x] < rang[y])
{
swap(x,y);
}
t[y] = x;
rang[x] += rang[y];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
#ifdef home
freopen("nr.in","r",stdin);
freopen("nr.out","w",stdout);
#endif // home
cin>>n>>m;
for(int i=1;i<=m;i++)
{
int x,y;
cin>>x>>y;
l[x].push_back(y);
}
for(int i=1;i<=n;i++)
{
sort(l[i].begin(),l[i].end());
}
for(int i=1;i<=n;i++)
{
rang[i] = 1, t[i] = i;
}
for(int i=1;i<=n;i++)
{
for(int j=0;j<l[i].size();j++)
{
int nod = l[i][j];
if(nod < i)
{
continue;
}
if(j < l[i].size() - 1)
{
uneste(l[i][j],l[i][j + 1]);
}
}
}
for(int i=1;i<=n;i++)
{
++fr[rep(i)];
}
int rez = m;
for(int i=1;i<=n;i++)
{
rez += 1LL * fr[i] * (fr[i] - 1) / 2;
}
for(int i=1;i<=n;i++)
{
for(auto it : l[i])
{
if(it > i && rep(i) == rep(it))
{
--rez;
}
}
}
cout<<rez<<'\n';
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:65:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(int j=0;j<l[i].size();j++)
| ~^~~~~~~~~~~~
Main.cpp:72:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | if(j < l[i].size() - 1)
| ~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
28 ms |
4828 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |