답안 #409663

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
409663 2021-05-21T10:12:12 Z kwongweng 자매 도시 (APIO20_swap) C++17
7 / 100
130 ms 13180 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;
    if (line[u] == -1){
        maxi = max(maxi, sub[u]); u = p[u];
    }
    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()){
      |     ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5324 KB Output is correct
2 Correct 4 ms 5324 KB Output is correct
3 Correct 4 ms 5324 KB Output is correct
4 Incorrect 5 ms 5324 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5324 KB Output is correct
2 Correct 4 ms 5324 KB Output is correct
3 Correct 114 ms 12544 KB Output is correct
4 Correct 130 ms 12568 KB Output is correct
5 Correct 120 ms 13180 KB Output is correct
6 Correct 118 ms 12556 KB Output is correct
7 Correct 118 ms 12728 KB Output is correct
8 Correct 114 ms 12664 KB Output is correct
9 Correct 116 ms 12684 KB Output is correct
10 Correct 114 ms 12660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5324 KB Output is correct
2 Correct 4 ms 5324 KB Output is correct
3 Correct 4 ms 5324 KB Output is correct
4 Incorrect 5 ms 5324 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5324 KB Output is correct
2 Correct 4 ms 5324 KB Output is correct
3 Correct 4 ms 5324 KB Output is correct
4 Incorrect 5 ms 5324 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5324 KB Output is correct
2 Correct 4 ms 5324 KB Output is correct
3 Correct 4 ms 5324 KB Output is correct
4 Incorrect 5 ms 5324 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5324 KB Output is correct
2 Correct 4 ms 5324 KB Output is correct
3 Correct 4 ms 5324 KB Output is correct
4 Incorrect 5 ms 5324 KB Output isn't correct
5 Halted 0 ms 0 KB -