Submission #1323903

#TimeUsernameProblemLanguageResultExecution timeMemory
1323903raqin_shahrierDrivers (BOI24_drivers)C++20
55 / 100
48 ms3416 KiB
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int MAXN = 100005;
const int LOG = 19; // Safe for 10^5

struct Edge {
    int u, v, t;
};

struct NodeEdge {
    int to, weight;
};

// DSU for Kruskal's
int parent_dsu[MAXN];
int find_set(int v) {
    if (v == parent_dsu[v]) return v;
    return parent_dsu[v] = find_set(parent_dsu[v]);
}

vector<NodeEdge> adj[MAXN];
int up[MAXN][LOG], mx[MAXN][LOG], depth[MAXN], root_id[MAXN];

// Standard DFS (Safe with Kruskal's because it limits edges to N-1)
void dfs(int u, int p, int w, int d, int r) {
    depth[u] = d;
    root_id[u] = r;
    up[u][0] = p;
    mx[u][0] = w;

    for (int i = 1; i < LOG; i++) {
        up[u][i] = up[up[u][i - 1]][i - 1];
        mx[u][i] = max(mx[u][i - 1], mx[up[u][i - 1]][i - 1]);
    }

    for (auto& edge : adj[u]) {
        if (edge.to != p) {
            dfs(edge.to, u, edge.weight, d + 1, r);
        }
    }
}

int query_max(int u, int v) {
    if (root_id[u] != root_id[v]) return 2e9 + 7;
    if (depth[u] < depth[v]) swap(u, v);

    int res = 0;
    for (int i = LOG - 1; i >= 0; i--) {
        if (depth[u] - (1 << i) >= depth[v]) {
            res = max(res, mx[u][i]);
            u = up[u][i];
        }
    }
    if (u == v) return res;
    for (int i = LOG - 1; i >= 0; i--) {
        if (up[u][i] != up[v][i]) {
            res = max({res, mx[u][i], mx[v][i]});
            u = up[u][i];
            v = up[v][i];
        }
    }
    return max({res, mx[u][0], mx[v][0]});
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int N, M, U;
    if (!(cin >> N >> M >> U)) return 0;

    vector<Edge> all_edges(M);
    for (int i = 0; i < M; i++) {
        cin >> all_edges[i].u >> all_edges[i].v >> all_edges[i].t;
    }

    // 1. Sort edges to build MST (The Minimax Path property)
    sort(all_edges.begin(), all_edges.end(), [](Edge a, Edge b) {
        return a.t < b.t;
    });

    for (int i = 1; i <= N; i++) parent_dsu[i] = i;

    // 2. Kruskal's
    for (int i = 0; i < M; i++) {
        int a = find_set(all_edges[i].u);
        int b = find_set(all_edges[i].v);
        if (a != b) {
            parent_dsu[a] = b;
            adj[all_edges[i].u].push_back({all_edges[i].v, all_edges[i].t});
            adj[all_edges[i].v].push_back({all_edges[i].u, all_edges[i].t});
        }
    }

    // 3. Precompute
    for (int i = 1; i <= N; i++) {
        if (depth[i] == 0) {
            dfs(i, i, 0, 1, i);
        }
    }

    // 4. Queries
    while (U--) {
        int a, b, p;
        cin >> a >> b >> p;
        if (query_max(a, b) <= p) cout << "TAIP\n";
        else cout << "NE\n";
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...