Submission #699460

# Submission time Handle Problem Language Result Execution time Memory
699460 2023-02-17T03:22:17 Z He_Huanglu Swapping Cities (APIO20_swap) C++17
0 / 100
1 ms 468 KB
#include "swap.h"
#include <bits/stdc++.h>
#define ii pair<int, int>
#define iii pair<int, ii>
#define fi first
#define se second
using namespace std;

const int N = 2e5 + 5;
int n, m, par[N], p1[N], p2[N], past[N], cur[N];
vector <iii> edg;

int root(int u)
{
    return (par[u] < 0) ? u : par[u] = root(par[u]);
}

bool check(int u, int v, int mid)
{
    while (par[u] > 0 && past[u] <= mid) u = par[u];
    while (par[v] > 0 && past[v] <= mid) v = par[v];
    if(u != v) return 0;
    if(cur[u] && cur[u] <= mid) return 1;
    return 0;
}


void join(int u, int v, int w)
{
    u = root(u), v = root(v);
    if(u == v)
    {
        if(!cur[u]) cur[u] = w;
        return ;
    }
    if(par[u] > par[v]) swap(u, v);
    par[u] += par[v];
    past[v] = w;

    if(par[v] == -1 && !cur[u])
    {
        if(p1[u] == u) p1[u] = v;
        else if(p2[u] == u) p2[u] = v;
        else cur[u] = w;
    }
    par[v] = u;

    if(!cur[u] && !cur[v])
    {
             if(p1[u] == p1[v]) p1[u] = p2[v];
        else if(p1[u] == p2[v]) p1[u] = p1[v];
        else if(p2[u] == p1[v]) p2[u] = p2[v];
        else if(p2[u] == p2[v]) p2[u] = p1[v];
        else cur[u] = w;
    }
    else if(!cur[u]) cur[u] = w;
}

void init(int n, int m, vector <int> U, vector <int> V, vector <int> W)
{
    for(int i = 1; i <= n; i++)
    {
        p1[i] = p2[i] = i;
        par[i] = -1;
    }
    for(int i = 0; i < m; i++)
    {
        U[i]++, V[i]++;
        edg.push_back({W[i], {U[i], V[i]}});
    }
    sort(edg.begin(), edg.end());
    for(auto [w, e] : edg) join(e.fi, e.se, w);
}

int getMinimumFuelCapacity(int u, int v)
{
    u++, v++;
    int l = 1, r = edg[edg.size() - 1].fi, mid, ans;
    while (l <= r)
    {
        mid = (l + r) / 2;
        if(check(u, v, mid)) ans = mid, r = mid - 1;
        else l = mid + 1;
    }
}

Compilation message

swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:78:49: warning: variable 'ans' set but not used [-Wunused-but-set-variable]
   78 |     int l = 1, r = edg[edg.size() - 1].fi, mid, ans;
      |                                                 ^~~
swap.cpp:85:1: warning: no return statement in function returning non-void [-Wreturn-type]
   85 | }
      | ^
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -