# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
221784 | anonymous | Teleporters (IOI08_teleporters) | C++14 | 784 ms | 56256 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 <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#define MAXN 1000005
#define fi first
#define se second
using namespace std;
int N, M, Ans, TP[MAXN][2], Pos[2*MAXN];
bool vis[2*MAXN]; //just visited endpoint [i]
vector <int> Cyc;
vector <pair<int,int> > L;
int main() {
//freopen("tpin.txt","r",stdin);
scanf("%d\n%d",&N,&M);
for (int i=1; i<=N; i++) {
scanf("%d %d",&TP[i][0], &TP[i][1]);
L.push_back({TP[i][0], i});
L.push_back({TP[i][1], i});
}
L.push_back({0,0});
sort(L.begin(), L.end());
for (int i=0; i<L.size(); i++) {
Pos[L[i].fi] = i;
}
for (int i=0; i<L.size(); i++) {
if (vis[i]) {continue;}
int sz = 0, cur = i;
while (!vis[cur]) {
vis[cur] = true;
if (cur == L.size()-1) {
Ans += sz;
break;
}
sz += 1;
if (L[cur+1].fi == TP[L[cur+1].se][0]) {
cur = Pos[TP[L[cur+1].se][1]];
} else {
cur = Pos[TP[L[cur+1].se][0]];
}
}
if (i) {Cyc.push_back(sz);}
}
sort(Cyc.begin(), Cyc.end());
while (M && Cyc.size()) {
Ans += Cyc.back() + 2;
M -=1;
Cyc.pop_back();
}
if (M%2) {Ans += 1;}
Ans += 4*(M/2);
printf("%d\n", Ans);
}
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... |