답안 #652820

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
652820 2022-10-24T16:33:52 Z raysh07 관광 (NOI14_sightseeing) C++14
15 / 25
1567 ms 262144 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 5e6 + 69;
int d[maxn];
int sz[maxn];
int root[maxn];
vector <int> st[maxn];
int ans[maxn];

int findroot(int x)
{
    while (x!=root[x])
    x = root[x];
    return x;
}

void unite(int x, int y)
{
    if (x==y)
    return;
    
    if (sz[x]<sz[y])
    swap(x, y);
    
    sz[x] += sz[y];
    root[y] = x;
    
    for (int i=0; i<(int)(st[y].size()); i++)
    st[x].push_back(st[y][i]);
}

void Solve() 
{
    int v, e, q;
    cin>>v>>e>>q;
    vector <pair<int, pair<int, int>>> edg;
    for (int i=0; i<e; i++)
    {
        int a, b, c;
        cin>>a>>b>>c;
        a--;
        b--;
        edg.push_back(make_pair(c, make_pair(a, b)));
    }
    
    sort(edg.begin(), edg.end(), greater<pair<int, pair<int, int>>>());
    
    for (int i=0; i<v; i++)
    {
        sz[i] = 1;
        root[i] = i;
        st[i].push_back(i);
    }
    
    for (int i=0; i<e; i++)
    {
        int u = edg[i].second.first;
        int v = edg[i].second.second;
        
        u = findroot(u);
        v = findroot(v);
        int w = findroot(0);
        
        if (v==w)
        {
            swap(u, v);
        }
        if (u==w && v!=w)
        {
            //cout<<"YES "<<i+1<<" "<<v+1<<"\n";
            for (int j=0; j<(int)(st[v].size()); j++)
            {
                //cout<<"UPDATED "<<st[v][j] + 1<<" "<<edg[i].first<<"\n";
                ans[st[v][j]] = edg[i].first;
            }
        }
        
        unite(u, v);
    }
    for (int i=0; i<q; i++)
    {
        int x;
        cin>>x;
        x--;
        cout<<ans[x]<<"\n";
    }
}

int32_t main() 
{
    //freopen(".in",  "r", stdin);
    //freopen(".out", "w", stdout);
    auto begin = std::chrono::high_resolution_clock::now();
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    //cin >> t;
    for(int i = 1; i <= t; i++) 
    {
        //cout << "Case #" << i << ": ";
        Solve();
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 117708 KB Output is correct
2 Correct 58 ms 117672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 117864 KB Output is correct
2 Correct 57 ms 117720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 84 ms 122040 KB Output is correct
2 Correct 82 ms 121532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1567 ms 220236 KB Output is correct
2 Runtime error 1232 ms 262144 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -