Submission #827463

# Submission time Handle Problem Language Result Execution time Memory
827463 2023-08-16T13:43:26 Z raphaelp Sightseeing (NOI14_sightseeing) C++14
15 / 25
2025 ms 88028 KB
#include <bits/stdc++.h>
using namespace std;
int UF[500000];
int Rank[500000];
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(100000);
    for (int i = 0; i < Tab.size(); i++)
    {
        temp[get<0>(Tab[i])].push_back(Tab[i]);
    }
    Tab.clear();
    for (int i = 0; i < 100000; 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);
    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, 0);
    vector<int> Querries2(Q);
    for (int i = 0; i < Q; i++)
    {
        cin >> Querries2[i];
        Querries2[i]--;
        Querries[Querries2[i]] = 1;
    }
    vector<int> occ(V, 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++)
      |                         ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2876 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 5660 KB Output is correct
2 Correct 42 ms 5188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 2025 ms 88028 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -