답안 #1065222

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1065222 2024-08-19T03:48:49 Z HiepVu217 자매 도시 (APIO20_swap) C++17
0 / 100
253 ms 40824 KB
#include <bits/stdc++.h>
#include "swap.h"
using namespace std;
const int Z = 3e5 + 17;
int n, m, q, lg, c[Z], ans[Z];
int r[Z], d[Z], p[20][Z];
tuple <int, int, int> e[Z];
vector <int> adj[Z];
bool ex[Z];
int root (int u)
{
    if (u == r[u])
    {
        return u;
    }
    return r[u] = root(r[u]);
}
void join (int x, int y, int z)
{
    x = root(x);
    y = root(y);
    ++n, r[n] = n;
    if (x == y)
    {
        ex[n] = 1, r[x] = n;
        adj[n].push_back(x);
    }
    else
    {
        ex[n] = ex[x] | ex[y] | z;
        r[x] = r[y] = n;
        adj[n].push_back(x);
        adj[n].push_back(y);
    }
}
int lca (int u, int v)
{
    if (d[u] < d[v])
    {
        swap (u, v);
    }
    for (int i = lg; i >= 0; --i)
    {
        if (d[p[i][u]] >= d[v])
        {
            u = p[i][u];
        }
    }
    if (u == v)
    {
        return u;
    }
    for (int i = lg; i >= 0; --i)
    {
        if (p[i][u] != p[i][v])
        {
            u = p[i][u], v = p[i][v];
        }
    }
    return p[0][u];
}
void dfs (int u, int pr)
{
    d[u] = d[pr] + 1, p[0][u] = pr;
    for (int i = 1; i <= lg; ++i)
    {
        p[i][u] = p[i - 1][p[i - 1][u]];
    }
    for (int v: adj[u])
    {
        if (v != pr)
        {
            dfs (v, u);
        }
    }
}
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W)
{
    n = N, m = M;
    lg = __lg(n + m);
    for (int i = 0; i < m; ++i)
    {
        int u = U[i], v = V[i], w = W[i];
        ++u, ++v;
        e[i] = {w, u, v};
    }
    for (int i = 1; i <= n; ++i)
    {
        r[i] = i;
    }
    sort (e + 1, e + m + 1);
    for (int i = 1; i <= m; ++i)
    {
        auto [w, u, v] = e[i];
        ++c[u], ++c[v];
        int z = (c[u] > 2) | (c[v] > 2);
        join (u, v, z);
        ans[n] = w;
    }
    for (int i = n; i >= 0; --i)
    {
        if (!d[i])
        {
            dfs (i, 0);
        }
    }
}
int getMinimumFuelCapacity(int u, int v)
{
    ++u, ++v;
    int z = lca (u, v);
    if (ex[z])
    {
        return ans[z];
    }
    for (int i = lg; i >= 0; --i)
    {
        if (p[i][z] && !ex[p[i][z]])
        {
            z = p[i][z];
        }
    }
    z = p[0][z];
    if (!z)
    {
        return -1;
    }
    return ans[z];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7512 KB Output is correct
2 Correct 3 ms 7512 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
4 Correct 3 ms 7516 KB Output is correct
5 Correct 3 ms 7772 KB Output is correct
6 Correct 3 ms 7768 KB Output is correct
7 Correct 5 ms 7772 KB Output is correct
8 Correct 4 ms 7772 KB Output is correct
9 Correct 60 ms 27224 KB Output is correct
10 Correct 66 ms 31652 KB Output is correct
11 Correct 64 ms 31312 KB Output is correct
12 Correct 64 ms 32888 KB Output is correct
13 Correct 57 ms 35204 KB Output is correct
14 Correct 64 ms 27824 KB Output is correct
15 Correct 176 ms 36064 KB Output is correct
16 Correct 178 ms 35328 KB Output is correct
17 Correct 194 ms 36872 KB Output is correct
18 Correct 253 ms 39092 KB Output is correct
19 Incorrect 94 ms 15196 KB Output isn't correct
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7512 KB Output is correct
2 Correct 3 ms 7512 KB Output is correct
3 Incorrect 214 ms 40824 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7512 KB Output is correct
2 Correct 3 ms 7512 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
4 Correct 3 ms 7516 KB Output is correct
5 Correct 3 ms 7772 KB Output is correct
6 Correct 3 ms 7768 KB Output is correct
7 Correct 5 ms 7772 KB Output is correct
8 Correct 4 ms 7772 KB Output is correct
9 Correct 3 ms 7512 KB Output is correct
10 Correct 3 ms 7772 KB Output is correct
11 Correct 4 ms 7764 KB Output is correct
12 Correct 3 ms 7772 KB Output is correct
13 Correct 4 ms 7516 KB Output is correct
14 Correct 4 ms 7516 KB Output is correct
15 Correct 3 ms 7772 KB Output is correct
16 Incorrect 3 ms 7772 KB Output isn't correct
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7512 KB Output is correct
2 Correct 3 ms 7512 KB Output is correct
3 Correct 3 ms 7512 KB Output is correct
4 Correct 3 ms 7516 KB Output is correct
5 Correct 3 ms 7516 KB Output is correct
6 Correct 3 ms 7772 KB Output is correct
7 Correct 3 ms 7768 KB Output is correct
8 Correct 5 ms 7772 KB Output is correct
9 Correct 4 ms 7772 KB Output is correct
10 Correct 60 ms 27224 KB Output is correct
11 Correct 66 ms 31652 KB Output is correct
12 Correct 64 ms 31312 KB Output is correct
13 Correct 64 ms 32888 KB Output is correct
14 Correct 57 ms 35204 KB Output is correct
15 Correct 3 ms 7772 KB Output is correct
16 Correct 4 ms 7764 KB Output is correct
17 Correct 3 ms 7772 KB Output is correct
18 Correct 4 ms 7516 KB Output is correct
19 Correct 4 ms 7516 KB Output is correct
20 Correct 3 ms 7772 KB Output is correct
21 Incorrect 3 ms 7772 KB Output isn't correct
22 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7512 KB Output is correct
2 Correct 3 ms 7512 KB Output is correct
3 Correct 3 ms 7516 KB Output is correct
4 Correct 3 ms 7516 KB Output is correct
5 Correct 3 ms 7772 KB Output is correct
6 Correct 3 ms 7768 KB Output is correct
7 Correct 5 ms 7772 KB Output is correct
8 Correct 4 ms 7772 KB Output is correct
9 Correct 60 ms 27224 KB Output is correct
10 Correct 66 ms 31652 KB Output is correct
11 Correct 64 ms 31312 KB Output is correct
12 Correct 64 ms 32888 KB Output is correct
13 Correct 57 ms 35204 KB Output is correct
14 Correct 64 ms 27824 KB Output is correct
15 Correct 176 ms 36064 KB Output is correct
16 Correct 178 ms 35328 KB Output is correct
17 Correct 194 ms 36872 KB Output is correct
18 Correct 253 ms 39092 KB Output is correct
19 Incorrect 214 ms 40824 KB Output isn't correct
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7512 KB Output is correct
2 Correct 3 ms 7512 KB Output is correct
3 Correct 3 ms 7512 KB Output is correct
4 Correct 3 ms 7516 KB Output is correct
5 Correct 3 ms 7516 KB Output is correct
6 Correct 3 ms 7772 KB Output is correct
7 Correct 3 ms 7768 KB Output is correct
8 Correct 5 ms 7772 KB Output is correct
9 Correct 4 ms 7772 KB Output is correct
10 Correct 60 ms 27224 KB Output is correct
11 Correct 66 ms 31652 KB Output is correct
12 Correct 64 ms 31312 KB Output is correct
13 Correct 64 ms 32888 KB Output is correct
14 Correct 57 ms 35204 KB Output is correct
15 Correct 64 ms 27824 KB Output is correct
16 Correct 176 ms 36064 KB Output is correct
17 Correct 178 ms 35328 KB Output is correct
18 Correct 194 ms 36872 KB Output is correct
19 Correct 253 ms 39092 KB Output is correct
20 Incorrect 214 ms 40824 KB Output isn't correct
21 Halted 0 ms 0 KB -