#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 = 5000100, 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;
unordered_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;
}
int kk = m/2;
m%=2;
ans += 4*kk;
if (m == 1) ans ++;
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
716 KB |
Output is correct |
2 |
Correct |
9 ms |
1356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
716 KB |
Output is correct |
2 |
Correct |
10 ms |
1788 KB |
Output is correct |
3 |
Correct |
18 ms |
3616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
1800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
2188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
14556 KB |
Output is correct |
2 |
Correct |
343 ms |
33416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
230 ms |
25360 KB |
Output is correct |
2 |
Correct |
538 ms |
52120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
629 ms |
65540 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
471 ms |
65540 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
517 ms |
65540 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |