Submission #1261005

#TimeUsernameProblemLanguageResultExecution timeMemory
1261005vominhhuy123Sightseeing (NOI14_sightseeing)C++20
15 / 25
1990 ms99532 KiB
#include <bits/stdc++.h>
#define fto(i, a, b) for (int i = (a); i <= (b); ++i)
#define fdto(i, b, a) for (int i = (b); i >= (a); --i)
#define N 500005
#define mod 1000000007

using namespace std;

int n, m, k, kk, c[N], ans[N], p[N], sz[N], hasRoot[N], query[N];
vector<pair<int, int>> edge[100005];
vector<int> need[N];

int root(int u) {
    return p[u] != u ? p[u] = root(p[u]) : u;
}

void join(int a, int b, int w) {
    a = root(a), b = root(b);
    if (a == b) return;
    if (sz[b] > sz[a]) swap(a, b);
    if (hasRoot[b] && !hasRoot[a]) swap(a, b);
    if (hasRoot[a] ^ hasRoot[b]) {
        for (int u : need[b]) if (ans[u] == -1) ans[u] = w, --kk;
    }
    if (need[a].size() < need[b].size()) need[a].swap(need[b]);
    need[a].insert(need[a].end(), need[b].begin(), need[b].end());
    vector<int>().swap(need[b]);
    sz[a] += sz[b];
    p[b] = a;
    hasRoot[a] = (hasRoot[a] || hasRoot[b]);
    return;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    if (fopen("test.inp", "r")) {
        freopen("test.inp", "r", stdin);
        freopen("test.out", "w", stdout);
    }
    scanf("%d %d %d", &n, &m, &k);
    fto(i, 1, n) p[i] = i, sz[i] = 1;
    fto(i, 1, m) {
        int u, v, w;
        scanf("%d %d %d", &u, &v, &w);
        edge[w].emplace_back(u, v);
    }
    fto(i, 1, k) {
        scanf("%d", &query[i]);
        if (!c[query[i]]) {
            c[query[i]] = 1;
            ++kk;
        }
    }
    fto(i, 1, n) ans[i] = -1;
    hasRoot[1] = 1;
    fto(i, 1, n) if (c[i]) need[i].push_back(i);
    fdto(w, 100000, 0) {
        for (pair<int, int> p : edge[w]) {
            int u = p.first, v = p.second;
            join(u, v, w);
            if (kk == 0) break;
        }
        if (kk == 0) break;
    }
    fto(i, 1, k) printf("%d\n", ans[query[i]]);
}

Compilation message (stderr)

sightseeing.cpp: In function 'int main()':
sightseeing.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |         freopen("test.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sightseeing.cpp:38:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |         freopen("test.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
sightseeing.cpp:40:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |     scanf("%d %d %d", &n, &m, &k);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sightseeing.cpp:44:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   44 |         scanf("%d %d %d", &u, &v, &w);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sightseeing.cpp:48:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |         scanf("%d", &query[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...