이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |