Submission #409638

# Submission time Handle Problem Language Result Execution time Memory
409638 2021-05-21T08:55:11 Z kwongweng Swapping Cities (APIO20_swap) C++17
7 / 100
124 ms 17232 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define ROF(i, a, b) for(int i = a; i >= b; i--)
#define ms memset
#define pb push_back
#define F first
#define S second

vector<pair<int, ii> > edges;
int n, m;
vi p(100001), sz(100001, 1), s(100001), e(100001), line(100001, -1), sub(100001);
vi d(100001), g[100001];

int get(int a){
    if (p[a] == a) return a;
    return get(p[a]);
}

void Union(int a, int b, int w){
    int c = get(a); int d = get(b);
    if (c == d){
        if (line[c] == -1) line[c] = w;
        return;
    }
    if (sz[c] < sz[d]){
        swap(c, d); swap(a, b);
    }
    p[d] = c;
    sz[c] += sz[d];
    sub[d] = w;
    if (line[c] == -1 && line[d] == -1){
        if (s[c] == a && b == s[d]){
            s[c] = e[d]; return;
        }
        if (s[c] == a && b == e[d]){
            s[c] = s[d]; return;
        }
        if (e[c] == a && b == s[d]){
            e[c] = e[d]; return;
        }
        if (e[c] == a && b == e[d]){
            e[c] = s[d]; return;
        }
        line[c] = w; return;
    }
    if (line[c] == -1) line[c] = line[d];
}

void init(int N, int M, vi U, vi V, vi W) {
    n = N; m = M;
    FOR(i, 0, m){
        edges.pb({W[i], {U[i], V[i]}});
    }
    FOR(i, 0, n){
        p[i] = i;
        s[i] = i;
        e[i] = i;
    }
    sort(edges.begin(), edges.end());
    FOR(i, 0, m){
        Union(edges[i].S.F, edges[i].S.S, edges[i].F);
    }
    int head;
    FOR(i, 0, n){
        if (p[i] != i){
            g[p[i]].pb(i); continue;
        }
        head = i;
    }
    vi bfs; bfs.pb(head);
    FOR(i, 0, bfs.size()){
        int u = bfs[i];
        for (int v : g[u]){
            d[v] = d[u] + 1;
            bfs.pb(v);
        }
    }
}


int getMinimumFuelCapacity(int x, int y) {
    int u = x; int v = y;
    int maxi = 0;
    if (d[u] < d[v]) swap(u, v);
    while (d[u] > d[v]){
        maxi = max(maxi, sub[u]);
        u = p[u];
    }
    while (u != v){
        maxi = max(maxi, max(sub[u], sub[v]));
        u = p[u]; v = p[v];
    }
    if (p[u] == u && line[u] == -1) return -1;
    return max(maxi, line[u]);
    return -1;
}

Compilation message

swap.cpp: In function 'void init(int, int, vi, vi, vi)':
swap.cpp:8:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 | #define FOR(i, a, b) for(int i = a; i < b; i++)
......
   77 |     FOR(i, 0, bfs.size()){
      |         ~~~~~~~~~~~~~~~~               
swap.cpp:77:5: note: in expansion of macro 'FOR'
   77 |     FOR(i, 0, bfs.size()){
      |     ^~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5324 KB Output is correct
2 Correct 4 ms 5324 KB Output is correct
3 Correct 4 ms 5400 KB Output is correct
4 Incorrect 4 ms 5404 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5324 KB Output is correct
2 Correct 4 ms 5324 KB Output is correct
3 Correct 116 ms 16364 KB Output is correct
4 Correct 116 ms 16416 KB Output is correct
5 Correct 124 ms 17232 KB Output is correct
6 Correct 113 ms 16416 KB Output is correct
7 Correct 121 ms 16760 KB Output is correct
8 Correct 114 ms 16488 KB Output is correct
9 Correct 114 ms 16572 KB Output is correct
10 Correct 115 ms 16480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5324 KB Output is correct
2 Correct 4 ms 5324 KB Output is correct
3 Correct 4 ms 5400 KB Output is correct
4 Incorrect 4 ms 5404 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5324 KB Output is correct
2 Correct 4 ms 5324 KB Output is correct
3 Correct 4 ms 5400 KB Output is correct
4 Incorrect 4 ms 5404 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5324 KB Output is correct
2 Correct 4 ms 5324 KB Output is correct
3 Correct 4 ms 5400 KB Output is correct
4 Incorrect 4 ms 5404 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5324 KB Output is correct
2 Correct 4 ms 5324 KB Output is correct
3 Correct 4 ms 5400 KB Output is correct
4 Incorrect 4 ms 5404 KB Output isn't correct
5 Halted 0 ms 0 KB -