이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+5;
set<int> point;
int p[2*N];
int sz[2*N];
int mark[2*N];
pair<int,int> tele[N];
bool exist[2*N];
priority_queue<int> pq;
int fin(int i){
if(p[i]==i) return i;
return p[i] = fin(p[i]);
}
int main(){
/*ios_base::sync_with_stdio(false);
cin.tie(0);*/
int n,m,mn=1e9;
cin >> n >> m;
for(int i=1;i<=2000000;i++) sz[i] = 1,p[i] = i;
for(int i=0;i<n;i++){
cin >> tele[i].first >> tele[i].second;
mn = min(mn,tele[i].first);
exist[tele[i].first] = exist[tele[i].second] = true;
point.insert(tele[i].first);
point.insert(tele[i].second);
}
for(int i=0;i<n;i++){
auto nxt = point.upper_bound(tele[i].second);
if(nxt!=point.end()&&fin(*nxt)!=fin(tele[i].first)){
sz[fin(tele[i].first)]+=sz[fin(*nxt)];
p[fin(*nxt)] = fin(tele[i].first);
}
nxt = point.upper_bound(tele[i].first);
if(nxt!=point.end()&&fin(*nxt)!=fin(tele[i].second)){
sz[fin(tele[i].second)]+=sz[fin(*nxt)];
p[fin(*nxt)] = fin(tele[i].second);
}
}
int ans = sz[fin(mn)];
mark[fin(mn)] = true;
for(int i=1;i<=2000000;i++){
if(!exist[i]||mark[fin(i)]) continue;
pq.push(sz[fin(i)]);
mark[fin(i)] = true;
}
while(!pq.empty()&&m){
ans+=pq.top()+2;
pq.pop();
--m;
}
cout << ans+2*m << "\n";
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... |
# | 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... |
# | 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... |
# | 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... |
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |