Submission #407190

# Submission time Handle Problem Language Result Execution time Memory
407190 2021-05-18T15:30:52 Z Aldas25 Teleporters (IOI08_teleporters) C++14
85 / 100
647 ms 65540 KB
#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 = 2000100, 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;

    seq.clear();

    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;
    }

    toId.clear();

    //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 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 3 ms 588 KB Output is correct
2 Correct 8 ms 1356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 716 KB Output is correct
2 Correct 11 ms 1744 KB Output is correct
3 Correct 18 ms 3544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 1832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 2100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 117 ms 14024 KB Output is correct
2 Correct 369 ms 32312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 247 ms 24628 KB Output is correct
2 Correct 572 ms 50992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 647 ms 65540 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 478 ms 65540 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 514 ms 65540 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -