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 <vector>
const int MAXN = 2e6 + 2;
int warp[MAXN], next[MAXN];
bool seen[MAXN];
std::vector<int> cycleSizes;
int floodfill(int pos)
{
if (seen[pos])
return 0;
seen[pos] = true;
return (floodfill(warp[next[pos]]) + 1);
}
int main()
{
std::ios::sync_with_stdio(false);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
std::fill_n(warp, MAXN, -1);
warp[0] = 0;
warp[MAXN - 1] = MAXN - 1;
int teleporters, canAdd;
std::cin >> teleporters >> canAdd;
for (int iTel = 0; iTel < teleporters; iTel++)
{
int west, east;
std::cin >> west >> east;
warp[west] = east;
warp[east] = west;
}
int lastSeen = MAXN - 1;
next[MAXN - 1] = MAXN - 1;
for (int iPos = MAXN - 2; iPos > -1; iPos--)
if (warp[iPos] != -1)
{
next[iPos] = lastSeen;
lastSeen = iPos;
}
int score = floodfill(0) - 2;
for (int iPos = 1; iPos < MAXN - 1; iPos++)
if (!seen[iPos] && warp[iPos] != -1)
cycleSizes.emplace_back(floodfill(iPos));
std::sort(cycleSizes.rbegin(), cycleSizes.rend());
for (int iCyc = 0; iCyc < (int)cycleSizes.size() && canAdd > 0; iCyc++)
{
score += cycleSizes[iCyc] + 2;
canAdd--;
}
std::cout << score + canAdd * 2 - canAdd % 2 << '\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... |