#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+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(a.size()<b.size())
swap(a,b);
if(b.empty())
return a;
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(s[i].begin());
if(s[i].size()==0)continue;
s[p] = Merge(s[i],s[p]);
}
cout<<ans<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9712 KB |
Output is correct |
3 |
Correct |
6 ms |
9940 KB |
Output is correct |
4 |
Correct |
5 ms |
9852 KB |
Output is correct |
5 |
Correct |
6 ms |
9872 KB |
Output is correct |
6 |
Correct |
5 ms |
9856 KB |
Output is correct |
7 |
Correct |
5 ms |
9852 KB |
Output is correct |
8 |
Correct |
6 ms |
9940 KB |
Output is correct |
9 |
Correct |
5 ms |
9820 KB |
Output is correct |
10 |
Correct |
6 ms |
9872 KB |
Output is correct |
11 |
Correct |
6 ms |
9852 KB |
Output is correct |
12 |
Correct |
6 ms |
9848 KB |
Output is correct |
13 |
Correct |
5 ms |
9852 KB |
Output is correct |
14 |
Correct |
5 ms |
9940 KB |
Output is correct |
15 |
Correct |
5 ms |
9812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9712 KB |
Output is correct |
3 |
Correct |
6 ms |
9940 KB |
Output is correct |
4 |
Correct |
5 ms |
9852 KB |
Output is correct |
5 |
Correct |
6 ms |
9872 KB |
Output is correct |
6 |
Correct |
5 ms |
9856 KB |
Output is correct |
7 |
Correct |
5 ms |
9852 KB |
Output is correct |
8 |
Correct |
6 ms |
9940 KB |
Output is correct |
9 |
Correct |
5 ms |
9820 KB |
Output is correct |
10 |
Correct |
6 ms |
9872 KB |
Output is correct |
11 |
Correct |
6 ms |
9852 KB |
Output is correct |
12 |
Correct |
6 ms |
9848 KB |
Output is correct |
13 |
Correct |
5 ms |
9852 KB |
Output is correct |
14 |
Correct |
5 ms |
9940 KB |
Output is correct |
15 |
Correct |
5 ms |
9812 KB |
Output is correct |
16 |
Correct |
566 ms |
540924 KB |
Output is correct |
17 |
Correct |
581 ms |
562396 KB |
Output is correct |
18 |
Correct |
603 ms |
596760 KB |
Output is correct |
19 |
Correct |
616 ms |
596744 KB |
Output is correct |
20 |
Correct |
604 ms |
596796 KB |
Output is correct |
21 |
Correct |
512 ms |
493440 KB |
Output is correct |
22 |
Correct |
563 ms |
541904 KB |
Output is correct |
23 |
Correct |
590 ms |
563280 KB |
Output is correct |
24 |
Correct |
610 ms |
596804 KB |
Output is correct |
25 |
Correct |
601 ms |
596684 KB |
Output is correct |
26 |
Correct |
651 ms |
596708 KB |
Output is correct |
27 |
Correct |
416 ms |
451148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
795 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9712 KB |
Output is correct |
3 |
Correct |
6 ms |
9940 KB |
Output is correct |
4 |
Correct |
5 ms |
9852 KB |
Output is correct |
5 |
Correct |
6 ms |
9872 KB |
Output is correct |
6 |
Correct |
5 ms |
9856 KB |
Output is correct |
7 |
Correct |
5 ms |
9852 KB |
Output is correct |
8 |
Correct |
6 ms |
9940 KB |
Output is correct |
9 |
Correct |
5 ms |
9820 KB |
Output is correct |
10 |
Correct |
6 ms |
9872 KB |
Output is correct |
11 |
Correct |
6 ms |
9852 KB |
Output is correct |
12 |
Correct |
6 ms |
9848 KB |
Output is correct |
13 |
Correct |
5 ms |
9852 KB |
Output is correct |
14 |
Correct |
5 ms |
9940 KB |
Output is correct |
15 |
Correct |
5 ms |
9812 KB |
Output is correct |
16 |
Correct |
566 ms |
540924 KB |
Output is correct |
17 |
Correct |
581 ms |
562396 KB |
Output is correct |
18 |
Correct |
603 ms |
596760 KB |
Output is correct |
19 |
Correct |
616 ms |
596744 KB |
Output is correct |
20 |
Correct |
604 ms |
596796 KB |
Output is correct |
21 |
Correct |
512 ms |
493440 KB |
Output is correct |
22 |
Correct |
563 ms |
541904 KB |
Output is correct |
23 |
Correct |
590 ms |
563280 KB |
Output is correct |
24 |
Correct |
610 ms |
596804 KB |
Output is correct |
25 |
Correct |
601 ms |
596684 KB |
Output is correct |
26 |
Correct |
651 ms |
596708 KB |
Output is correct |
27 |
Correct |
416 ms |
451148 KB |
Output is correct |
28 |
Runtime error |
795 ms |
1048576 KB |
Execution killed with signal 9 |
29 |
Halted |
0 ms |
0 KB |
- |