답안 #827570

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
827570 2023-08-16T14:50:03 Z raphaelp 관광 (NOI14_sightseeing) C++14
15 / 25
3257 ms 133884 KB
#include <bits/stdc++.h>
using namespace std;
int UF[500002];
int Rank[500002];
int num = 0;
int find(int x)
{
    return (x == UF[x]) ? x : (UF[x] = find(UF[x]));
}
bool merge(int a, int b)
{
    a = find(a);
    b = find(b);
    if (a == b)
        return false;
    if (Rank[b] > Rank[a])
        swap(a, b);
    UF[b] = a;
    if (Rank[b] == Rank[a])
        Rank[a]++;
    num++;
    return true;
}
void rec(int x, int w, vector<vector<pair<int, int>>> &Tree, vector<int> &occ, vector<int> &Querries)
{
    occ[x] = 1;
    if (Querries[x] == 1)
        Querries[x] = w;
    for (int i = 0; i < Tree[x].size(); i++)
    {
        if (occ[Tree[x][i].first] == 1)
            continue;
        rec(Tree[x][i].first, min(w, Tree[x][i].second), Tree, occ, Querries);
    }
}
void linsort(vector<tuple<int, int, int>> &Tab)
{
    vector<vector<tuple<int, int, int>>> temp(100003);
    for (int i = 0; i < Tab.size(); i++)
    {
        temp[get<0>(Tab[i])].push_back(Tab[i]);
    }
    Tab.clear();
    for (int i = 0; i < 100003; i++)
    {
        for (int j = 0; j < temp[i].size(); j++)
        {
            Tab.push_back(temp[i][j]);
        }
    }
}
int main()
{
    int V, E, Q;
    cin >> V >> E >> Q;
    for (int i = 0; i <= V; i++)
    {
        UF[i] = i;
        Rank[i] = 0;
    }
    vector<tuple<int, int, int>> Tab;
    for (int i = 0; i < E; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        a--;
        b--;
        Tab.push_back({c, a, b});
    }
    linsort(Tab);
    vector<vector<pair<int, int>>> Tree(V + 1);
    while (num < V - 1)
    {
        int a = get<1>(Tab[Tab.size() - 1]);
        int b = get<2>(Tab[Tab.size() - 1]);
        int w = get<0>(Tab[Tab.size() - 1]);
        if (merge(a, b))
        {
            Tree[a].push_back({b, w});
            Tree[b].push_back({a, w});
        }
        Tab.pop_back();
    }
    vector<int> Querries(V + 1, 0);
    vector<int> Querries2(Q + 1, 0);
    for (int i = 0; i < Q; i++)
    {
        cin >> Querries2[i];
        Querries2[i]--;
        Querries[Querries2[i]] = 1;
    }
    vector<int> occ(V + 1, 0);
    rec(0, 1000000, Tree, occ, Querries);
    for (int i = 0; i < Q; i++)
    {
        cout << Querries[Querries2[i]] << endl;
    }
}

Compilation message

sightseeing.cpp: In function 'void rec(int, int, std::vector<std::vector<std::pair<int, int> > >&, std::vector<int>&, std::vector<int>&)':
sightseeing.cpp:29:23: 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 i = 0; i < Tree[x].size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~
sightseeing.cpp: In function 'void linsort(std::vector<std::tuple<int, int, int> >&)':
sightseeing.cpp:39:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |     for (int i = 0; i < Tab.size(); i++)
      |                     ~~^~~~~~~~~~~~
sightseeing.cpp:46:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |         for (int j = 0; j < temp[i].size(); j++)
      |                         ~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2824 KB Output is correct
2 Correct 3 ms 2624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 5620 KB Output is correct
2 Correct 42 ms 5140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2556 ms 103772 KB Output is correct
2 Runtime error 3257 ms 133884 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -