#include<bits/stdc++.h>
using namespace std;
#define INF 2000000002
#define MAXN 1000000
int n, m, nxt[MAXN + 3];
bool visited[MAXN + 3];
vector<pair<int, int> > points;
vector<int> cycleLengths;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int w, e;
cin >> w >> e;
points.push_back({w, e});
points.push_back({e, w});
}
memset(nxt, -1, sizeof(nxt));
sort(points.begin(), points.end());
for (int i = 0; i < points.size(); i++) {
auto [x, otherEnd] = points[i];
int idx = upper_bound(points.begin(), points.end(), make_pair(otherEnd, INF)) - points.begin();
nxt[i] = idx;
}
memset(visited, false, sizeof(visited));
// Process the line graph
int numNodesInLineGraph = 0;
int currentNode = 0;
while (currentNode != -1) {
numNodesInLineGraph += 1;
visited[currentNode] = true;
currentNode = nxt[currentNode];
}
// Process the cycles
for (int i = 0; i < 2 * n + 1; i++) {
if (!visited[i]) {
int currentNode = i;
int cycleLength = 0;
while (!visited[currentNode]) {
cycleLength += 1;
visited[currentNode] = true;
currentNode = nxt[currentNode];
}
cycleLengths.push_back(cycleLength);
}
}
sort(cycleLengths.begin(), cycleLengths.end(), greater<int>());
// Connect the line graph with the cycles while possible
for (int i = 0; i < cycleLengths.size() && m > 0; i++, m--) {
numNodesInLineGraph += cycleLengths[i] + 2;
}
// If we can add more teleports..
numNodesInLineGraph += (m/2) * 4 + (m%2);
cout << numNodesInLineGraph - 1 << endl;
}
Compilation message
teleporters.cpp: In function 'int main()':
teleporters.cpp:25:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for (int i = 0; i < points.size(); i++) {
| ~~^~~~~~~~~~~~~~~
teleporters.cpp:63:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for (int i = 0; i < cycleLengths.size() && m > 0; i++, m--) {
| ~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5332 KB |
Output is correct |
2 |
Correct |
11 ms |
5516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5256 KB |
Output is correct |
2 |
Correct |
14 ms |
5660 KB |
Output is correct |
3 |
Correct |
27 ms |
6092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
5612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
5700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
128 ms |
9296 KB |
Output is correct |
2 |
Correct |
303 ms |
15156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
222 ms |
12304 KB |
Output is correct |
2 |
Correct |
450 ms |
19720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
604 ms |
37448 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
887 ms |
49276 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
964 ms |
54932 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |