답안 #1104567

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1104567 2024-10-24T04:05:21 Z TrinhKhanhDung 관광 (NOI14_sightseeing) C++14
25 / 25
1689 ms 179108 KB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18 + 10
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)

using namespace std;

template<class T1, class T2>
    bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}

template<class T1, class T2>
    bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}

template<class T1, class T2>
    void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}

template<class T1, class T2>
    void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}

template<class T>
    void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}

//DON'T BELIEVE ANYONE WILL INSPIRE YOU ->  TRAIN HARDER ->  YOU WILL GET WHAT YOU WANT

struct DSU{
    vector<int> par, Sz;

    DSU(int n = 0){
        par.resize(n + 3, 0);
        Sz.resize(n + 3, 1);
        iota(ALL(par), 0);
    }

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

    bool join(int a, int b){
        a = root(a);
        b = root(b);
        if(a == b) return false;
        if(Sz[a] < Sz[b]) swap(a, b);
        Sz[a] += Sz[b];
        par[b] = a;
        return true;
    }
};

const int MAX = 5e5 + 10;

int N, M, Q;
vector<pair<int, int>> adj[MAX];
int ans[MAX];
vector<array<int, 3>> edges;

void input(){
    cin >> N >> M >> Q;
    for(int i = 1; i <= M; i++){
        int u, v, c; cin >> u >> v >> c;
        edges.push_back({c, u, v});
    }
}

void dfs(int u = 1, int p = -1){
    for(auto x: adj[u]){
        int v = x.fi, c = x.se;
        if(v == p) continue;
        ans[v] = min(ans[u], c);
        dfs(v, u);
    }
}

void solve(){
    sort(ALL(edges), greater<array<int, 3>>());
    DSU dsu(N);
    for(auto x: edges){
        if(dsu.join(x[1], x[2])){
            adj[x[1]].push_back({x[2], x[0]});
            adj[x[2]].push_back({x[1], x[0]});
        }
    }
    ans[1] = INF;
    dfs();
    while(Q--){
        int x; cin >> x;
        cout << ans[x] << '\n';
    }
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
     // freopen("task.inp","r",stdin);
     // freopen("task.out","w",stdout);

    input();
    solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 13904 KB Output is correct
2 Correct 3 ms 13904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 13904 KB Output is correct
2 Correct 4 ms 13904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 16392 KB Output is correct
2 Correct 19 ms 16072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 986 ms 70144 KB Output is correct
2 Correct 1689 ms 179108 KB Output is correct