답안 #1091638

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1091638 2024-09-21T16:05:15 Z raphaelp 관광 (NOI14_sightseeing) C++14
15 / 25
2434 ms 144976 KB
#include <bits/stdc++.h>
using namespace std;

vector<int> p;
int find(int x) { return (p[x] == x) ? x : p[x] = find(p[x]); }
int main()
{
    int N, M, Q;
    cin >> N >> M >> Q;
    vector<vector<pair<int, int>>> buckets(1000010);
    for (int i = 0; i < M; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        a--, b--;
        if (c > 100000)
            cout << p[10e10];
        buckets[c].push_back({a, b});
    }
    p.assign(N, 0);
    for (int i = 0; i < N; i++)
        p[i] = i;
    vector<int> dist(N);
    vector<vector<int>> contains(N);
    for (int i = 0; i < N; i++)
        contains[i].push_back(i);
    for (int i = 100000; i >= 0; i--)
    {
        for (int j = 0; j < buckets[i].size(); j++)
        {
            int x = find(buckets[i][j].first), y = find(buckets[i][j].second);
            if (x == y)
                continue;
            if (contains[x].size() > contains[y].size())
                swap(x, y);
            if (find(0) == x)
                swap(x, y);
            p[x] = y;
            for (auto val : contains[x])
            {
                if (y == find(0))
                    dist[val] = i;
                contains[y].push_back(val);
            }
        }
    }

    for (int i = 0; i < Q; i++)
    {
        int x;
        cin >> x;
        cout << dist[x - 1] << '\n';
    }
}

Compilation message

sightseeing.cpp: In function 'int main()':
sightseeing.cpp:29:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |         for (int j = 0; j < buckets[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23900 KB Output is correct
2 Correct 14 ms 23900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23896 KB Output is correct
2 Correct 11 ms 23900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 28748 KB Output is correct
2 Correct 65 ms 28240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2434 ms 144976 KB Output is correct
2 Runtime error 29 ms 47964 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -