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>
using namespace std;
typedef long long ll;
class UN{
public:
vector<int>P;
UN(int n){P.resize(n);for(int i=0;i<n;i++)P[i]=i;}
int F(int X){return X==P[X]?X:P[X]=F(P[X]);}
void ch(int X,int Y)
{
if(F(X)!=F(Y))
P[P[X]]=P[Y];
}
};
vector<vector<int> >V;
vector<int>ST,NST,VC;
bitset<100000>IS,ch;
int N,M;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin>>N>>M;
UN tr(N);
V.resize(N);
VC.resize(N);
for(int i=0;i<M;i++)
{
int T,O;
cin>>T>>O;T--,O--;
V[T].push_back(O);
}
for(int i=0;i<N;i++)ST.push_back(i),ch.set(i);
while(!ST.empty())
{
ch.reset();
for(auto&I:ST)
{
if(IS.test(I))for(auto&J:V[I])
{
tr.ch(J,I);
if(IS.test(J)==0&&ch.test(J)==0)
{
NST.push_back(J);
ch.set(J);
}IS.set(J);
}
else if(V[I].size()>=2)for(auto&J:V[I])
{
tr.ch(J,V[I][0]);
if(IS.test(J)==0&&ch.test(J)==0)
{
NST.push_back(J);
ch.set(J);
}IS.set(J);
}
}
swap(ST,NST);
NST.resize(0);
}
for(int i=0;i<N;i++)VC[tr.F(i)]++;
ll tot=0;
for(int i=0;i<N;i++)if(tr.F(i)==i)tot+=(ll)VC[tr.P[i]]*(VC[tr.P[i]]-1);
for(int i=0;i<N;i++)if(IS.test(i)==0)tot+=V[i].size();
cout<<tot;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |