#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e6+3;
int i,j,p,q,n,m,k;
long long ans;
set <int> s[maxn];
set <int> Merge(set <int> a,set <int> b)
{
if(b.empty())
return a;
if(a.empty())
return b;
if(a.size()<b.size())
swap(a,b);
a.insert(b.begin(),b.end());
return a;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m;
for(i=1;i<=m;i++)
{
cin>>p>>q;
s[p].insert(q);
}
for(i=1;i<=n;i++)
{
if(s[i].size()==0)continue;
ans+=(long long)s[i].size();
p = *s[i].begin();s[i].erase(p);
if(s[i].size()==0 && s[p].size()==0)continue;
s[p] = Merge(s[i],s[p]);
}
cout<<ans<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
94216 KB |
Output is correct |
2 |
Correct |
38 ms |
94172 KB |
Output is correct |
3 |
Correct |
46 ms |
94460 KB |
Output is correct |
4 |
Correct |
39 ms |
94352 KB |
Output is correct |
5 |
Correct |
41 ms |
94412 KB |
Output is correct |
6 |
Correct |
44 ms |
94392 KB |
Output is correct |
7 |
Correct |
41 ms |
94472 KB |
Output is correct |
8 |
Correct |
41 ms |
94348 KB |
Output is correct |
9 |
Correct |
46 ms |
94344 KB |
Output is correct |
10 |
Correct |
43 ms |
94368 KB |
Output is correct |
11 |
Correct |
41 ms |
94388 KB |
Output is correct |
12 |
Correct |
41 ms |
94408 KB |
Output is correct |
13 |
Correct |
39 ms |
94408 KB |
Output is correct |
14 |
Correct |
39 ms |
94444 KB |
Output is correct |
15 |
Correct |
41 ms |
94444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
94216 KB |
Output is correct |
2 |
Correct |
38 ms |
94172 KB |
Output is correct |
3 |
Correct |
46 ms |
94460 KB |
Output is correct |
4 |
Correct |
39 ms |
94352 KB |
Output is correct |
5 |
Correct |
41 ms |
94412 KB |
Output is correct |
6 |
Correct |
44 ms |
94392 KB |
Output is correct |
7 |
Correct |
41 ms |
94472 KB |
Output is correct |
8 |
Correct |
41 ms |
94348 KB |
Output is correct |
9 |
Correct |
46 ms |
94344 KB |
Output is correct |
10 |
Correct |
43 ms |
94368 KB |
Output is correct |
11 |
Correct |
41 ms |
94388 KB |
Output is correct |
12 |
Correct |
41 ms |
94408 KB |
Output is correct |
13 |
Correct |
39 ms |
94408 KB |
Output is correct |
14 |
Correct |
39 ms |
94444 KB |
Output is correct |
15 |
Correct |
41 ms |
94444 KB |
Output is correct |
16 |
Correct |
587 ms |
625128 KB |
Output is correct |
17 |
Correct |
605 ms |
646536 KB |
Output is correct |
18 |
Correct |
681 ms |
680992 KB |
Output is correct |
19 |
Correct |
631 ms |
680952 KB |
Output is correct |
20 |
Correct |
646 ms |
680904 KB |
Output is correct |
21 |
Correct |
497 ms |
577968 KB |
Output is correct |
22 |
Correct |
578 ms |
625936 KB |
Output is correct |
23 |
Correct |
597 ms |
647372 KB |
Output is correct |
24 |
Correct |
631 ms |
680944 KB |
Output is correct |
25 |
Correct |
635 ms |
680924 KB |
Output is correct |
26 |
Correct |
681 ms |
680920 KB |
Output is correct |
27 |
Correct |
447 ms |
535628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
781 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
94216 KB |
Output is correct |
2 |
Correct |
38 ms |
94172 KB |
Output is correct |
3 |
Correct |
46 ms |
94460 KB |
Output is correct |
4 |
Correct |
39 ms |
94352 KB |
Output is correct |
5 |
Correct |
41 ms |
94412 KB |
Output is correct |
6 |
Correct |
44 ms |
94392 KB |
Output is correct |
7 |
Correct |
41 ms |
94472 KB |
Output is correct |
8 |
Correct |
41 ms |
94348 KB |
Output is correct |
9 |
Correct |
46 ms |
94344 KB |
Output is correct |
10 |
Correct |
43 ms |
94368 KB |
Output is correct |
11 |
Correct |
41 ms |
94388 KB |
Output is correct |
12 |
Correct |
41 ms |
94408 KB |
Output is correct |
13 |
Correct |
39 ms |
94408 KB |
Output is correct |
14 |
Correct |
39 ms |
94444 KB |
Output is correct |
15 |
Correct |
41 ms |
94444 KB |
Output is correct |
16 |
Correct |
587 ms |
625128 KB |
Output is correct |
17 |
Correct |
605 ms |
646536 KB |
Output is correct |
18 |
Correct |
681 ms |
680992 KB |
Output is correct |
19 |
Correct |
631 ms |
680952 KB |
Output is correct |
20 |
Correct |
646 ms |
680904 KB |
Output is correct |
21 |
Correct |
497 ms |
577968 KB |
Output is correct |
22 |
Correct |
578 ms |
625936 KB |
Output is correct |
23 |
Correct |
597 ms |
647372 KB |
Output is correct |
24 |
Correct |
631 ms |
680944 KB |
Output is correct |
25 |
Correct |
635 ms |
680924 KB |
Output is correct |
26 |
Correct |
681 ms |
680920 KB |
Output is correct |
27 |
Correct |
447 ms |
535628 KB |
Output is correct |
28 |
Runtime error |
781 ms |
1048576 KB |
Execution killed with signal 9 |
29 |
Halted |
0 ms |
0 KB |
- |