#include <bits/stdc++.h>
#define all(a) begin(a), end(a)
#define S second
#define F first
using namespace std;
void cc(vector<pair<int, int>> &v) {
vector<int> tmp;
for (auto &[a, b] : v) {
tmp.push_back(a);
tmp.push_back(b);
}
sort(all(tmp));
tmp.erase(unique(all(tmp)), end(tmp));
for (auto &[a, b] : v) {
a = lower_bound(all(tmp), a) - begin(tmp);
b = lower_bound(all(tmp), b) - begin(tmp);
}
}
int const N = 1000005;
vector<bool> bio;
vector<int> len;
vector<int> nxt;
void findcyc(int ver, int l, int tar) {
bio[ver] = true;
if (ver == tar) {
len.push_back(l);
return;
}
findcyc(nxt[ver], l + 1, tar);
}
void solve() {
int n, m;
cin >> n >> m;
bio.resize(2 * n);
nxt.resize(2 * n);
vector<pair<int, int>> v;
for (int i = 0; i < n; ++i) {
int a, b;
cin >> a >> b;
v.emplace_back(a, b);
v.emplace_back(b, a);
}
sort(all(v));
cc(v);
for (int i = 0; i < 2 * n; ++i) {
nxt[i] = (v[i].S + 1) % (2 * n);
}
for (int i = 0; i < 2 * n; ++i) {
if (!bio[nxt[i]]) {
findcyc(nxt[i], 1, i);
}
}
sort(begin(len) + 1, end(len), greater<>());
int ans = len[0];
int i = 1;
for (; i <= m && i < len.size(); ++i) {
if (len[i] > 0) {
ans += len[i] + 2;
} else break;
}
int lft = m + 1 - i;
if (lft % 2 == 1) {
lft--;
ans++;
}
ans += 2 * lft;
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
Compilation message
teleporters.cpp: In function 'void solve()':
teleporters.cpp:60:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for (; i <= m && i < len.size(); ++i) {
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 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 |
460 KB |
Output is correct |
2 |
Correct |
9 ms |
844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
588 KB |
Output is correct |
2 |
Correct |
10 ms |
968 KB |
Output is correct |
3 |
Correct |
23 ms |
2964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
1272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
1484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
118 ms |
6660 KB |
Output is correct |
2 |
Correct |
357 ms |
19716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
238 ms |
11468 KB |
Output is correct |
2 |
Correct |
501 ms |
23352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
593 ms |
39656 KB |
Output is correct |
2 |
Correct |
735 ms |
53524 KB |
Output is correct |
3 |
Correct |
704 ms |
64416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
962 ms |
45744 KB |
Output is correct |
2 |
Execution timed out |
1057 ms |
61248 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1052 ms |
48596 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |