이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int MAXN = (int) 2e6;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> other(MAXN + 1, -1);
vector<int> a;
for (int i = 0; i < n; i++) {
int w, e;
cin >> w >> e;
other[w] = e; other[e] = w;
a.push_back(w); a.push_back(e);
}
sort(a.begin(), a.end());
vector<int> next(2 * n + 1, -1);
for (int i = 0; i < 2 * n; i++) {
next[i] = upper_bound(a.begin(), a.end(), other[a[i]]) - a.begin();
}
vector<bool> was(2 * n + 1, false); was[0] = true;
queue<int> q; q.push(0);
int ans = 0;
while (!q.empty()) {
int u = q.front(); q.pop();
if (next[u] != -1 && !was[next[u]]) {
ans++;
q.push(next[u]);
was[next[u]] = true;
}
}
vector<int> b;
for (int i = 0; i < 2 * n; i++) {
if (was[i]) {
continue;
}
q.push(i);
was[i] = true;
b.push_back(0);
while (!q.empty()) {
int u = q.front(); q.pop();
b.back()++;
if (next[u] != -1 && !was[next[u]]) {
q.push(next[u]);
was[next[u]] = true;
}
}
}
sort(b.rbegin(), b.rend());
for (int i = 0; i < min((int) b.size(), m); i++) {
ans += b[i] + 2;
}
m -= min((int) b.size(), m);
ans += (m + 1) / 2 + (m / 2) * 3;
cout << ans << '\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... |