#include <bits/stdc++.h>
#define int long long
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];
}
signed 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:66:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for(int j=0;j<l[i].size();j++)
| ~^~~~~~~~~~~~
Main.cpp:73:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | if(j < l[i].size() - 1)
| ~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
3 ms |
2772 KB |
Output is correct |
4 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
3 ms |
2772 KB |
Output is correct |
4 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
32 ms |
6356 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
3 ms |
2772 KB |
Output is correct |
4 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |