Submission #1111568

# Submission time Handle Problem Language Result Execution time Memory
1111568 2024-11-12T09:40:10 Z itslq Drivers (BOI24_drivers) C++17
55 / 100
667 ms 100384 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long

const int MAX = 200005, LOG = 21;

vector<int> depth(MAX, 0), ufdspa(MAX), ufdsrk(MAX);
vector<vector<pair<int, int>>> twok(LOG, vector<pair<int, int>>(MAX, make_pair(-1, INT_MAX)));
vector<vector<pair<int, int>>> adjList(MAX), MSTList(MAX);

int root(int n) {
    if (ufdspa[n] == n) return n;
    return ufdspa[n] = root(ufdspa[n]);
}

void merge(int a, int b) {
    int x = root(a), y = root(b);
    if (x == y) return;
    if (ufdsrk[x] > ufdsrk[y]) ufdspa[y] = x;
    else if (ufdsrk[x] < ufdsrk[y]) ufdspa[x] = y;
    else {
        ufdspa[x] = y;
        ufdsrk[y]++;
    }
}

void rec(int n, int p = -1) {
    for (pair<int, int> adj: MSTList[n]) {
        if (adj.second == p) continue;
        //cout << adj.first << "***\n";
        twok[0][adj.second] = make_pair(n, adj.first);
        depth[adj.second] = depth[n] + 1;
        rec(adj.second, n);
    }
}

pair<int, int> kpar(int x, int k){
    //cout << x << '-' << k << endl;
    int L = 0;
	for (int j = 0; j < LOG; j++){
		if (x == -1) break;
		if (k & (1 << j)) {
            L = max(L, twok[j][x].second);
            x = twok[j][x].first;
		}
	}
	//cout << "kpar returns " << x << " " << L << endl;
	return make_pair(x, L);
}

int ans(int a, int b) {
    if (root(a) != root(b)) return INT_MAX;
    if (depth[a] < depth[b]) swap(a, b);

    pair<int, int> p = kpar(a, depth[a] - depth[b]);
    //cout << depth[a] - depth[b] << "th ";
    a = p.first;
    //cout << "PARENT IS: " << a << '\n';
    int L = p.second;

    if (a == b) return L;

    for (int k = LOG - 1; k >= 0; k--){
        if (twok[k][a].first != twok[k][b].first){
            L = max(L, max(twok[k][a].second, twok[k][b].second));
            a = twok[k][a].first; b = twok[k][b].first;
        }
    }

    return max(L, max(twok[0][a].second, twok[0][b].second));
}

signed main() {
    int N, M, U, X, Y, T, A, B, P;
    pair<int, pair<int, int>> curr;
    cin >> N >> M >> U;
    vector<bool> vis(N);
    for (int i = 0; i < M; i++) {
        cin >> X >> Y >> T;
        --X; --Y;
        adjList[X].emplace_back(T, Y);
        adjList[Y].emplace_back(T, X);
    }
    for (int i = 0; i < N; i++) ufdspa[i] = i;

    priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq;
    for (int k = 0; k < N; k++) {
        if (vis[k]) continue;

        pq = priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>>();
        pq.emplace(0, make_pair(k, k));
        while (!pq.empty()) {
            curr = pq.top();
            pq.pop();

            if (vis[curr.second.second]) continue;
            vis[curr.second.second] = 1;

            if (curr.second.first != k || curr.second.second != k) {
                MSTList[curr.second.first].emplace_back(curr.first, curr.second.second);
                MSTList[curr.second.second].emplace_back(curr.first, curr.second.first);
                merge(curr.second.first, curr.second.second);
            }

            for (pair<int, int> adj: adjList[curr.second.second]) {
                if (vis[adj.second]) continue;
                pq.emplace(adj.first, make_pair(curr.second.second, adj.second));
            }
        }

        rec(k);
    }

    if (U > 10000) {
        while (U--) {
            cin >> A >> B >> P;
            --A; --B;

            if (T > P || root(A) != root(B)) {
                cout << "NE\n";
            } else {
                cout << "TAIP\n";
            }

            //cout << (ans(--A, --B)) << endl;
            //cout << (ans(--A, --B) > P ? "NE" : "TAIP") << endl;
        }
    } else {
        while (U--) {
            cin >> A >> B >> P;
            --A; --B;
            vector<bool> v(N);
            queue<int> q;

            q.push(A);
            v[A] = 1;
            bool f = 0;
            while (!q.empty()) {
                if (q.front() == B) {
                    cout << "TAIP\n";
                    f = 1;
                    break;
                }
                for (pair<int, int> adj: MSTList[q.front()]) {
                    if (v[adj.second] || adj.first > P) continue;
                    q.push(adj.second);
                    v[adj.second] = 1;
                }
                q.pop();
            }

            if (!f) cout << "NE\n";

            //cout << (ans(--A, --B)) << endl;
            //cout << (ans(--A, --B) > P ? "NE" : "TAIP") << endl;
        }
    }

    //for (int i = 0; i < N; i++) cout << pa[i] << " ";
}
/*
8 7 10
1 2 3
1 3 5
1 4 4
3 5 2
4 5 3
6 8 4
6 7 1*/
# Verdict Execution time Memory Grader output
1 Correct 25 ms 80408 KB Output is correct
2 Correct 28 ms 80664 KB Output is correct
3 Correct 28 ms 80760 KB Output is correct
4 Correct 29 ms 80324 KB Output is correct
5 Correct 402 ms 81936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 80408 KB Output is correct
2 Correct 25 ms 80372 KB Output is correct
3 Correct 26 ms 80400 KB Output is correct
4 Correct 28 ms 80408 KB Output is correct
5 Correct 27 ms 80416 KB Output is correct
6 Correct 27 ms 80408 KB Output is correct
7 Correct 28 ms 80528 KB Output is correct
8 Correct 30 ms 80664 KB Output is correct
9 Correct 29 ms 80680 KB Output is correct
10 Correct 35 ms 80664 KB Output is correct
11 Correct 49 ms 80664 KB Output is correct
12 Correct 40 ms 81168 KB Output is correct
13 Correct 29 ms 80664 KB Output is correct
14 Correct 38 ms 80572 KB Output is correct
15 Correct 25 ms 80408 KB Output is correct
16 Correct 26 ms 80416 KB Output is correct
17 Correct 25 ms 80408 KB Output is correct
18 Correct 31 ms 80344 KB Output is correct
19 Correct 620 ms 81512 KB Output is correct
20 Correct 27 ms 80576 KB Output is correct
21 Correct 30 ms 80392 KB Output is correct
22 Correct 35 ms 80400 KB Output is correct
23 Correct 30 ms 80576 KB Output is correct
24 Correct 430 ms 81688 KB Output is correct
25 Correct 27 ms 80144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 80408 KB Output is correct
2 Correct 28 ms 80664 KB Output is correct
3 Correct 28 ms 80760 KB Output is correct
4 Correct 29 ms 80324 KB Output is correct
5 Correct 402 ms 81936 KB Output is correct
6 Correct 26 ms 80336 KB Output is correct
7 Correct 31 ms 80680 KB Output is correct
8 Correct 36 ms 80524 KB Output is correct
9 Correct 45 ms 80664 KB Output is correct
10 Correct 42 ms 81172 KB Output is correct
11 Correct 41 ms 80448 KB Output is correct
12 Correct 29 ms 80408 KB Output is correct
13 Correct 29 ms 80408 KB Output is correct
14 Correct 26 ms 80212 KB Output is correct
15 Correct 26 ms 80408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 80408 KB Output is correct
2 Correct 28 ms 80664 KB Output is correct
3 Correct 28 ms 80760 KB Output is correct
4 Correct 29 ms 80324 KB Output is correct
5 Correct 402 ms 81936 KB Output is correct
6 Correct 25 ms 80408 KB Output is correct
7 Correct 25 ms 80372 KB Output is correct
8 Correct 26 ms 80400 KB Output is correct
9 Correct 28 ms 80408 KB Output is correct
10 Correct 27 ms 80416 KB Output is correct
11 Correct 27 ms 80408 KB Output is correct
12 Correct 28 ms 80528 KB Output is correct
13 Correct 30 ms 80664 KB Output is correct
14 Correct 29 ms 80680 KB Output is correct
15 Correct 35 ms 80664 KB Output is correct
16 Correct 49 ms 80664 KB Output is correct
17 Correct 40 ms 81168 KB Output is correct
18 Correct 29 ms 80664 KB Output is correct
19 Correct 38 ms 80572 KB Output is correct
20 Correct 25 ms 80408 KB Output is correct
21 Correct 26 ms 80416 KB Output is correct
22 Correct 25 ms 80408 KB Output is correct
23 Correct 31 ms 80344 KB Output is correct
24 Correct 620 ms 81512 KB Output is correct
25 Correct 27 ms 80576 KB Output is correct
26 Correct 30 ms 80392 KB Output is correct
27 Correct 35 ms 80400 KB Output is correct
28 Correct 30 ms 80576 KB Output is correct
29 Correct 430 ms 81688 KB Output is correct
30 Correct 27 ms 80144 KB Output is correct
31 Correct 26 ms 80336 KB Output is correct
32 Correct 31 ms 80680 KB Output is correct
33 Correct 36 ms 80524 KB Output is correct
34 Correct 45 ms 80664 KB Output is correct
35 Correct 42 ms 81172 KB Output is correct
36 Correct 41 ms 80448 KB Output is correct
37 Correct 29 ms 80408 KB Output is correct
38 Correct 29 ms 80408 KB Output is correct
39 Correct 26 ms 80212 KB Output is correct
40 Correct 26 ms 80408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 80408 KB Output is correct
2 Correct 28 ms 80664 KB Output is correct
3 Correct 28 ms 80760 KB Output is correct
4 Correct 29 ms 80324 KB Output is correct
5 Correct 402 ms 81936 KB Output is correct
6 Correct 25 ms 80408 KB Output is correct
7 Correct 25 ms 80372 KB Output is correct
8 Correct 26 ms 80400 KB Output is correct
9 Correct 28 ms 80408 KB Output is correct
10 Correct 27 ms 80416 KB Output is correct
11 Correct 27 ms 80408 KB Output is correct
12 Correct 28 ms 80528 KB Output is correct
13 Correct 30 ms 80664 KB Output is correct
14 Correct 29 ms 80680 KB Output is correct
15 Correct 35 ms 80664 KB Output is correct
16 Correct 49 ms 80664 KB Output is correct
17 Correct 40 ms 81168 KB Output is correct
18 Correct 29 ms 80664 KB Output is correct
19 Correct 38 ms 80572 KB Output is correct
20 Correct 25 ms 80408 KB Output is correct
21 Correct 26 ms 80416 KB Output is correct
22 Correct 25 ms 80408 KB Output is correct
23 Correct 31 ms 80344 KB Output is correct
24 Correct 620 ms 81512 KB Output is correct
25 Correct 27 ms 80576 KB Output is correct
26 Correct 30 ms 80392 KB Output is correct
27 Correct 35 ms 80400 KB Output is correct
28 Correct 30 ms 80576 KB Output is correct
29 Correct 430 ms 81688 KB Output is correct
30 Correct 27 ms 80144 KB Output is correct
31 Correct 26 ms 80336 KB Output is correct
32 Correct 31 ms 80680 KB Output is correct
33 Correct 36 ms 80524 KB Output is correct
34 Correct 45 ms 80664 KB Output is correct
35 Correct 42 ms 81172 KB Output is correct
36 Correct 41 ms 80448 KB Output is correct
37 Correct 29 ms 80408 KB Output is correct
38 Correct 29 ms 80408 KB Output is correct
39 Correct 26 ms 80212 KB Output is correct
40 Correct 26 ms 80408 KB Output is correct
41 Incorrect 667 ms 100384 KB Output isn't correct
42 Halted 0 ms 0 KB -