이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
void just_do_it();
int main() {
#ifdef KAMIRULEZ
freopen("kamirulez.inp", "r", stdin);
freopen("kamirulez.out", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
just_do_it();
return 0;
}
const int maxn = 1e6 + 20;
pair<int, int> a[maxn * 2];
int adj[maxn * 2];
bool flag[maxn * 2];
int sz = 0;
void dfs(int u) {
flag[u] = true;
sz++;
if (!flag[adj[u]]) {
dfs(adj[u]);
}
}
void just_do_it() {
int n, m;
cin >> n >> m;
n++;
a[1] = make_pair(0, 1);
a[2] = make_pair(maxn * 2, n + 1);
for (int i = 2; i <= n; i++) {
a[i * 2 - 1].second = i;
a[i * 2].second = n + i;
cin >> a[i * 2 - 1].first >> a[i * 2].first;
}
sort(a + 1, a + n * 2 + 1);
for (int i = 1; i <= n * 2 - 1; i++) {
int u = a[i].second;
int v = a[i + 1].second;
if (v > n) {
v -= n;
}
else {
v += n;
}
adj[u] = v;
}
dfs(1);
int res = sz - 1;
vector<int> sizes;
for (int i = 1; i <= n * 2; i++) {
if (i != n + 1 && !flag[i]) {
sz = 0;
dfs(i);
sizes.push_back(sz);
}
}
sort(sizes.begin(), sizes.end(), greater<int>());
for (int i = 0; i < (int)sizes.size() && m > 0; i++) {
res += sizes[i] + 2;
m--;
}
res += 4 * (m / 2) + (m % 2);
cout << res;
}
# | 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... |