# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
110485 | 2019-05-10T22:09:15 Z | MetB | Teleporters (IOI08_teleporters) | C++14 | 0 ms | 0 KB |
#include <iostream> #include <cstdlib> #include <string> #include <set> #include <map> #include <algorithm> #include <bitset> #include <queue> #include <math.h> #include <stack> #include <vector> #include <string.h> #include <random> typedef long long ll; const ll MOD = 1e9 + 7, INF = 1e18; using namespace std; int n, m, mn, ans; pair <int, int> a[3000000]; int nxt[3000001], c[3000000], u[3000000], v[3000001]; int level[3000000], to[3000001]; int main () { cin >> n >> m; for (int i = 0; i < n; i++) { scanf ("%d%d", &a[i].first, &a[i].second); nxt[a[i].first - 1] = 2 * i + 1; nxt[a[i].second - 1] = 2 * i + 2; if (a[mn].first > a[i].first) mn = i; } for (int i = 2000000; i >= 0; i--) if (!nxt[i]) nxt[i] = nxt[i + 1]; to[0] = 2 * mn + 1; for (int i = 0; i < n; i++) { to[2 * i + 1] = nxt[a[i].second]; to[2 * i + 2] = nxt[a[i].first]; } int color = 1; for (int i = 0; i <= 2 * n; i++) { if (u[x]) continue; int x = i; u[x] = color; while (!u[to[x]]) { level[to[x]] = level[x] + 1; u[to[x]] = color; x = to[x]; } if (u[to[x]] == color) { if (color == 1) ans = level[x] - level[to[x]]; else v[level[x] - level[to[x]] + 1]++; } color++; } for (int i = 2 * n; i >= 1 && m; i--) while (v[i]) { m--; v[i]--; ans += i + 2; } cout << ans + m / 2 * 4 + m % 2; }