이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(0)
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define pb push_back
#define f first
#define s second
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<piii> viii;
const int MAXN = 3000100, MAXK = 30;
//const ll MOD = 998244353;
const ll INF = 1e17;
const ld PI = asin(1) * 2;
int n, m;
int w[MAXN], e[MAXN];
vi seq;
map<int, int> toId;
int to[MAXN];
vi lens;
bool vis[MAXN];
int len = 0;
void dfs (int v) {
vis[v] = true;
len++;
if (!vis[to[v]])
dfs(to[v]);
}
int main()
{
FAST_IO;
cin >> n >> m;
FOR(i, 1, n) cin >> w[i] >> e[i];
FOR(i, 1, n) {
seq.pb(w[i]);
seq.pb(e[i]);
}
sort(seq.begin(), seq.end());
int curId = 1;
for (int x : seq)
toId[x] = ++curId;
FOR(i, 1, n) {
int id1 = toId[w[i]];
int id2 = toId[e[i]];
to[id1-1] = id2;
to[id2-1] = id1;
// cout << " i = " << i << " w = " << w[i] << " e = " << e[i] << " id1 = " << id1 << " id2= " << id2 << endl;
}
//FOR(i, 1, 2*n+1) cout << " i = " << i << " to = " << to[i] << endl;
vis[0] = true;
ll ans = 0;
FOR(i, 1, 2*n+1) {
if (vis[i]) continue;
len = 0;
dfs(i);
if (i == 1) {
len--;
ans = len;
} else {
lens.pb(len);
}
}
// cout << "ans = " << ans << "\n";
sort(lens.begin(), lens.end());
reverse(lens.begin(), lens.end());
for (int x : lens) {
if (m == 0) break;
m--;
ans += x + 2;
// cout << " adding x+2; x=" << x << endl;
}
while (m > 1) {
m -= 2;
ans += 4;
}
if (m == 1) ans ++;
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... |