This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 = 2000100, MAXK = 30;
//const ll MOD = 998244353;
const ll INF = 1e17;
const ld PI = asin(1) * 2;
int n, m;
vii seq;
//unordered_map<int, int> toId;
int id1[MAXN], id2[MAXN];
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) {
int w, e;
cin >> w >> e;
seq.pb({w, 2*i});
seq.pb({e, 2*i+1});
}
sort(seq.begin(), seq.end());
FOR(i, 0, 2*n-1) {
//int x = seq[i].f;
int id = seq[i].s;
if (id%2 == 0) {
id /= 2;
id1[id] = i+2;
} else {
id /= 2;
id2[id] = i+2;
}
}
FOR(i, 1, n) {
//int id1 = toId[w[i]];
//int id2 = toId[e[i]];
to[id1[i]-1] = id2[i];
to[id2[i]-1] = id1[i];
// 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;
}
int kk = m/2;
m%=2;
ans += 4*kk;
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... |