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 <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <string>
#include <numeric>
#include <cmath>
#include <cassert>
#include <array>
#include <numeric>
using namespace std;
using ll = long long;
int dfs2(int v, vector<int> &adj, vector<bool> &used)
{
used[v] = true;
if (adj[v] == -1 || used[adj[v]]) return 1;
return dfs2(adj[v], adj, used) + 1;
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
int n, m;
cin >> n >> m;
vector<pair<int, int>> we(n);
vector<pair<int, int>> closest(2 * n);
for (int i = 0; i < n; ++i)
{
cin >> we[i].first >> we[i].second;
closest[i * 2] = {we[i].first, i * 2};
closest[i * 2 + 1] = {we[i].second, i * 2 + 1};
}
sort(closest.begin(), closest.end());
vector<int> adj(2 * n, -1);
for (int i = 0; i < n; ++i)
{
auto it = upper_bound(closest.begin(), closest.end(), pair(we[i].second, (int)1e9));
if (it != closest.end()) adj[2 * i] = it->second;
it = upper_bound(closest.begin(), closest.end(), pair(we[i].first, (int)1e9));
if (it != closest.end()) adj[2 * i + 1] = it->second;
}
int start = closest.front().second;
vector<bool> used(2 * n);
auto dfs = [&adj, &used](auto self, int v) -> int
{
used[v] = true;
if (adj[v] == -1 || used[adj[v]]) return 1;
return self(self, adj[v]) + 1;
};
int res = dfs2(start, adj, used);
vector<int> comps;
for (int i = 0; i < 2 * n; ++i) if (!used[i]) comps.push_back(dfs2(i, adj, used));
sort(comps.rbegin(), comps.rend());
for (int i = 0; i < min(m, (int)comps.size()); ++i) res += comps[i] + 2;
m -= (int)comps.size();
m = max(m, 0);
res += 2 * (m - m % 2);
res += m % 2;
cout << res;
return 0;
}
Compilation message (stderr)
teleporters.cpp: In function 'int main()':
teleporters.cpp:48:10: warning: variable 'dfs' set but not used [-Wunused-but-set-variable]
48 | auto dfs = [&adj, &used](auto self, int v) -> int
| ^~~
# | 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... |