#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
class UN{
public:
vector<int>P;
vector<list<int> >from;
UN(int n){P.resize(n);from.resize(n);for(int i=0;i<n;i++)P[i]=i,from[i].push_back(i);}
int F(int X){return X==P[X]?X:P[X]=F(P[X]);}
bool ch(int X,int Y)
{
if(from[F(X)].size()>from[F(Y)].size())swap(X,Y);
if(F(X)!=F(Y))
{
P[P[X]]=P[Y];
from[Y].insert(from[Y].end(),from[X].begin(),from[X].end());
from[X].clear();
return 1;
}
return 0;
}
};
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 |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
488 KB |
Output is correct |
3 |
Correct |
2 ms |
488 KB |
Output is correct |
4 |
Correct |
2 ms |
496 KB |
Output is correct |
5 |
Correct |
3 ms |
500 KB |
Output is correct |
6 |
Correct |
3 ms |
664 KB |
Output is correct |
7 |
Correct |
3 ms |
928 KB |
Output is correct |
8 |
Correct |
3 ms |
928 KB |
Output is correct |
9 |
Correct |
4 ms |
928 KB |
Output is correct |
10 |
Correct |
4 ms |
928 KB |
Output is correct |
11 |
Correct |
2 ms |
928 KB |
Output is correct |
12 |
Correct |
2 ms |
928 KB |
Output is correct |
13 |
Correct |
3 ms |
928 KB |
Output is correct |
14 |
Correct |
2 ms |
928 KB |
Output is correct |
15 |
Correct |
2 ms |
972 KB |
Output is correct |
16 |
Correct |
2 ms |
972 KB |
Output is correct |
17 |
Correct |
3 ms |
1036 KB |
Output is correct |
18 |
Correct |
3 ms |
1036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1556 KB |
Output is correct |
2 |
Correct |
5 ms |
1796 KB |
Output is correct |
3 |
Correct |
14 ms |
2132 KB |
Output is correct |
4 |
Correct |
48 ms |
4164 KB |
Output is correct |
5 |
Correct |
40 ms |
5020 KB |
Output is correct |
6 |
Correct |
67 ms |
7472 KB |
Output is correct |
7 |
Correct |
4 ms |
7472 KB |
Output is correct |
8 |
Correct |
6 ms |
7472 KB |
Output is correct |
9 |
Correct |
11 ms |
7472 KB |
Output is correct |
10 |
Correct |
22 ms |
7472 KB |
Output is correct |
11 |
Correct |
73 ms |
10224 KB |
Output is correct |
12 |
Correct |
10 ms |
10224 KB |
Output is correct |
13 |
Correct |
7 ms |
10224 KB |
Output is correct |
14 |
Correct |
6 ms |
10224 KB |
Output is correct |
15 |
Correct |
10 ms |
10224 KB |
Output is correct |
16 |
Correct |
15 ms |
10224 KB |
Output is correct |
17 |
Correct |
102 ms |
12620 KB |
Output is correct |
18 |
Correct |
6 ms |
12620 KB |
Output is correct |
19 |
Correct |
7 ms |
12620 KB |
Output is correct |
20 |
Correct |
66 ms |
14788 KB |
Output is correct |
21 |
Correct |
7 ms |
14788 KB |
Output is correct |
22 |
Correct |
6 ms |
14788 KB |
Output is correct |
23 |
Correct |
8 ms |
14788 KB |
Output is correct |
24 |
Correct |
7 ms |
14788 KB |
Output is correct |
25 |
Correct |
7 ms |
14788 KB |
Output is correct |
26 |
Correct |
6 ms |
14788 KB |
Output is correct |
27 |
Correct |
5 ms |
14788 KB |
Output is correct |
28 |
Correct |
6 ms |
14788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
16312 KB |
Output is correct |
2 |
Correct |
180 ms |
29844 KB |
Output is correct |
3 |
Correct |
215 ms |
32336 KB |
Output is correct |
4 |
Correct |
117 ms |
32336 KB |
Output is correct |
5 |
Correct |
60 ms |
32336 KB |
Output is correct |
6 |
Correct |
156 ms |
32336 KB |
Output is correct |
7 |
Correct |
167 ms |
38732 KB |
Output is correct |
8 |
Correct |
126 ms |
41012 KB |
Output is correct |
9 |
Correct |
88 ms |
41176 KB |
Output is correct |
10 |
Correct |
111 ms |
43292 KB |
Output is correct |
11 |
Correct |
184 ms |
46364 KB |
Output is correct |
12 |
Correct |
190 ms |
49004 KB |
Output is correct |
13 |
Correct |
68 ms |
49604 KB |
Output is correct |
14 |
Correct |
87 ms |
49604 KB |
Output is correct |
15 |
Correct |
62 ms |
50336 KB |
Output is correct |
16 |
Correct |
19 ms |
50336 KB |
Output is correct |
17 |
Correct |
20 ms |
50336 KB |
Output is correct |
18 |
Correct |
169 ms |
54252 KB |
Output is correct |
19 |
Correct |
181 ms |
56540 KB |
Output is correct |
20 |
Correct |
106 ms |
56648 KB |
Output is correct |
21 |
Correct |
64 ms |
56648 KB |
Output is correct |
22 |
Correct |
25 ms |
56648 KB |
Output is correct |
23 |
Correct |
37 ms |
56648 KB |
Output is correct |
24 |
Correct |
108 ms |
58608 KB |
Output is correct |
25 |
Correct |
66 ms |
62076 KB |
Output is correct |
26 |
Correct |
104 ms |
63664 KB |
Output is correct |
27 |
Correct |
196 ms |
66064 KB |
Output is correct |
28 |
Correct |
86 ms |
66064 KB |
Output is correct |
29 |
Correct |
86 ms |
67128 KB |
Output is correct |
30 |
Correct |
175 ms |
70300 KB |
Output is correct |
31 |
Correct |
103 ms |
70300 KB |
Output is correct |
32 |
Correct |
61 ms |
72224 KB |
Output is correct |
33 |
Correct |
51 ms |
73348 KB |
Output is correct |
34 |
Correct |
91 ms |
73700 KB |
Output is correct |
35 |
Correct |
53 ms |
75596 KB |
Output is correct |
36 |
Correct |
73 ms |
76844 KB |
Output is correct |
37 |
Correct |
86 ms |
77504 KB |
Output is correct |