This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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)
| ~~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |