#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 |
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 |
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 |
2 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 |
4 ms |
716 KB |
Output is correct |
2 |
Correct |
11 ms |
1508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
836 KB |
Output is correct |
2 |
Correct |
14 ms |
1816 KB |
Output is correct |
3 |
Correct |
34 ms |
3840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
1908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
2324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
212 ms |
15724 KB |
Output is correct |
2 |
Correct |
683 ms |
41412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
404 ms |
26464 KB |
Output is correct |
2 |
Execution timed out |
1091 ms |
59292 KB |
Time limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
544 ms |
65540 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
627 ms |
65540 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
659 ms |
65540 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |