답안 #95163

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
95163 2019-01-27T19:04:59 Z karma 관광 (NOI14_sightseeing) C++11
25 / 25
1765 ms 211800 KB
#include<bits/stdc++.h>
#define For(i, a, b)  for(int i = a, _b = b; i <= _b; ++i)
#define Ford(i, a, b) for(int i = a, _b = b; i >= _b; --i)
#define FileName      "test"
#define ll            long long
#define ld            long double
#define ull           unsigned long long
#define Print(x)      cerr << #x << "is " << x << '\n';
#define pb            push_back
#define x             first
#define y             second
//#define Karma

using namespace std;

template<typename T> inline void Cin(T &x)
{
    char c;
    T sign = 1;
    x = 0;
    for (c = getchar(); c < '0' || c > '9'; c = getchar())
        if (c == '-') sign = -1;
    for (; c >= '0' && c <= '9'; c = getchar())
        x = x * 10 + c - '0';
    x *= sign;
}
template <typename T> inline void Out(T x) {if(x > 9) Out(x / 10); putchar(x % 10 + '0');}
template <typename T> inline void Cout(T x, char c) {if(x < 0) putchar('-'); x = abs(x); Out(x); putchar(c);}
template <typename T, typename... Args> inline void Cin(T& a, Args&... args) {Cin(a);Cin(args...);}
template <typename T, typename... Args> inline void Cout(T a, char c, Args... args) {Cout(a, c);Cout(args...);}

typedef pair<int, pair<int, int>> TEdge;
const int N = 1e6 + 7;

vector<TEdge> e;
int n, m, q, res[N], lab[N];
vector<int> a[N];

int Find(int u)
{
    return lab[u] < 0? u: lab[u] = Find(lab[u]);
}

void Union(int r, int s)
{
    if(lab[r] > lab[s]) swap(r, s);
    lab[r] += lab[s], lab[s] = r;
}

void DFS(int u, int par)
{
     for(int i: a[u]) {
        int v = e[i].y.x ^ e[i].y.y ^ u;
        if(v == par) continue;
        res[v] = min(res[u], e[i].x);
        DFS(v, u);
     }
}

void Enter()
{
     Cin(n, m, q);
     memset(&lab, -1, sizeof lab);
     e.resize(m);
     for(int i = 0; i < m; ++i) {
        int u, v, w;
        Cin(u, v, w);
        e[i] = {w, {u, v}};
     }
     sort(e.begin(), e.end(), greater<TEdge>());
     for(int i = 0; i < m; ++ i) {
        int u = e[i].y.x, v = e[i].y.y, w = e[i].x;
        int r = Find(u), s = Find(v);
        if(r != s) {
           Union(r, s);
           a[u].pb(i), a[v].pb(i);
        }
     }
     res[1] = int(1e9) + 7;
     DFS(1, 1);
}

void Solve()
{
     while(q --) {
        int x; Cin(x); Cout(res[x], '\n');
     }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
#ifdef Karma
    freopen(FileName".inp", "r", stdin);
    freopen(FileName".out", "w", stdout);
#endif // Karma

    Enter();
    Solve();

    return 0;
}

Compilation message

sightseeing.cpp: In function 'void Enter()':
sightseeing.cpp:72:41: warning: unused variable 'w' [-Wunused-variable]
         int u = e[i].y.x, v = e[i].y.y, w = e[i].x;
                                         ^
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 27896 KB Output is correct
2 Correct 26 ms 27768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 27960 KB Output is correct
2 Correct 26 ms 27768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 30584 KB Output is correct
2 Correct 46 ms 30200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1251 ms 136268 KB Output is correct
2 Correct 1765 ms 211800 KB Output is correct