Submission #649636

#TimeUsernameProblemLanguageResultExecution timeMemory
649636KoalaMuchTeleporters (IOI08_teleporters)C++14
100 / 100
971 ms52492 KiB
#include<bits/stdc++.h> using namespace std; const int N = 2e6+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]; int nxt[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; } int prev = mn; for(int i=mn+1;i<=2000000;i++){ if(exist[i]) nxt[prev] = i,prev=i; } for(int i=0;i<n;i++){ int next = nxt[tele[i].second]; if(next!=0&&fin(next)!=fin(tele[i].first)){ sz[fin(tele[i].first)]+=sz[fin(next)]; p[fin(next)] = fin(tele[i].first); } next = nxt[tele[i].first]; if(next!=0&&fin(next)!=fin(tele[i].second)){ sz[fin(tele[i].second)]+=sz[fin(next)]; p[fin(next)] = 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-(m%2) << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...