# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
825533 | Trunkty | Teleporters (IOI08_teleporters) | C++14 | 355 ms | 56452 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
int n,m;
int pos[2000005],nex[2000005];
bool check[2000005];
vector<int> v;
int cnt,ans;
void dfs(int x){
if(check[x]){
return;
}
check[x] = true;
cnt++;
dfs(nex[x]);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for(int i=1;i<=n;i++){
int a,b;
cin >> a >> b;
pos[a] = i;
pos[b] = i+1e6;
}
int bef=0;
for(int i=1;i<=2e6;i++){
if(pos[i]){
nex[bef] = pos[i];
bef = pos[i];
}
}
for(int i=1;i<=n;i++){
swap(nex[i],nex[i+1000000]);
}
dfs(0);
ans = cnt-1LL;
for(int i=1;i<=n;i++){
if(!check[i]){
cnt = 0;
dfs(i);
v.push_back(cnt);
}
if(!check[i+1000000]){
cnt = 0;
dfs(i+1000000);
v.push_back(cnt);
}
}
sort(v.begin(),v.end(),greater<int>());
for(int i=0;i<=m-1;i++){
if(i<v.size()){
ans += v[i];
}
}
ans += m*2LL;
cout << ans << "\n";
return 0;
}
Compilation message (stderr)
# | 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... |