답안 #1111395

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1111395 2024-11-12T08:07:33 Z simuyu Drivers (BOI24_drivers) C++14
100 / 100
176 ms 23984 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pl pair<long, long>
#define pll pair<ll, ll>
#define f first
#define s second
#define edge pair<ll, pl>
#define query pair<ll, pair<long, pl> >

// edge is <-time, <from, to>>
// query is <-maxt, <idx, <a, b>>>

long parent[200005];
long depth[200005];
bool ress[200005];

long get_root(long i) {
    while (parent[parent[i]] != parent[i]) {
        parent[i] = parent[parent[i]];
        depth[i] --;
        i = parent[i];
    }
    return parent[i];
}

void merge_sets(long i, long j) {
    if (depth[i] > depth[j]) {
        // should add root of i to j, reducing max depth
        long p = get_root(i);
        parent[p] = parent[j]; // this way, they share their root
        depth[p] = depth[j];
        depth[i] = depth[j] + 1;
    } else {
        long p = get_root(j);
        parent[p] = parent[i];
        depth[p] = depth[i];
        depth[j] = depth[i] + 1;
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    long N, M, U;
    cin >> N >> M >> U;

    // initialize UFDS
    for (long i=1; i<=N; i++) {
        parent[i] = i;
        depth[i] = 1;
    }

    priority_queue<edge> edges;
    long x, y, t;
    for (long i=0; i<M; i++) {
        // read in the M edges and sort them by weight
        cin >> x >> y >> t;
        edges.push(edge(-t, pl(x, y)));
    }

    // edge is <time, <from, to>>
    // query is <maxt, <idx, <a, b>>>

    long a, b;
    ll maxt;
    priority_queue<query> queries;
    for (long i=0; i<U; i++) {
        cin >> a >> b >> maxt;
        queries.push(query(-maxt, pair<long, pl>(i, pl(a, b))));
    }

    // process the queries (offline)
    ll next_p;
    while (!queries.empty()) {

        //cout << "SOLVING QUERY: " << queries.top().s.f << endl;

        next_p = -queries.top().f;
        //cout << "NEXT P: " << next_p << endl;

        while (!edges.empty()) {
            // see if we should add more edges to UFDS
            if (-edges.top().f <= next_p) {
                // can and should add
                // add
                //cout << "ADDED EDGE (" << edges.top().s.f << ',' << edges.top().s.s << ") FOR " << -edges.top().f << endl;
                merge_sets(edges.top().s.f, edges.top().s.s);
                edges.pop();
            } else {
                break; // shouldn't add anymore
            }
        }

        /*cout << "PARENTS: ";
        for (int i=1; i<=N; i++) {
            cout << parent[i] << ' ';
        }
        cout << endl;*/


        // now try to find
        if (get_root(queries.top().s.s.f) == get_root(queries.top().s.s.s)) {
            // same root, so it's possible
            //cout << "SAME ROOT YAY" << endl;
            ress[queries.top().s.f] = true;
        } else {
            //cout << "DIFF ROOT NOO" << endl;
            ress[queries.top().s.f] = false;
        }

        queries.pop();

    }


    for (int i=0; i<U; i++) {
        if (ress[i]) {
            cout << "TAIP\n";
        } else {
            cout << "NE\n";
        }
    }


    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 2 ms 2640 KB Output is correct
3 Correct 2 ms 2640 KB Output is correct
4 Correct 1 ms 2384 KB Output is correct
5 Correct 64 ms 13116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 1 ms 2384 KB Output is correct
3 Correct 1 ms 2384 KB Output is correct
4 Correct 2 ms 2552 KB Output is correct
5 Correct 1 ms 2384 KB Output is correct
6 Correct 1 ms 2596 KB Output is correct
7 Correct 2 ms 2640 KB Output is correct
8 Correct 2 ms 2640 KB Output is correct
9 Correct 2 ms 2640 KB Output is correct
10 Correct 2 ms 2640 KB Output is correct
11 Correct 2 ms 2640 KB Output is correct
12 Correct 4 ms 2832 KB Output is correct
13 Correct 2 ms 2640 KB Output is correct
14 Correct 3 ms 2904 KB Output is correct
15 Correct 1 ms 2384 KB Output is correct
16 Correct 1 ms 2384 KB Output is correct
17 Correct 1 ms 2384 KB Output is correct
18 Correct 2 ms 2384 KB Output is correct
19 Correct 7 ms 3404 KB Output is correct
20 Correct 1 ms 2384 KB Output is correct
21 Correct 1 ms 2384 KB Output is correct
22 Correct 1 ms 2384 KB Output is correct
23 Correct 1 ms 2384 KB Output is correct
24 Correct 6 ms 3404 KB Output is correct
25 Correct 1 ms 2384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 2 ms 2640 KB Output is correct
3 Correct 2 ms 2640 KB Output is correct
4 Correct 1 ms 2384 KB Output is correct
5 Correct 64 ms 13116 KB Output is correct
6 Correct 1 ms 2384 KB Output is correct
7 Correct 2 ms 2640 KB Output is correct
8 Correct 2 ms 2640 KB Output is correct
9 Correct 3 ms 2640 KB Output is correct
10 Correct 4 ms 2832 KB Output is correct
11 Correct 3 ms 2896 KB Output is correct
12 Correct 1 ms 2384 KB Output is correct
13 Correct 1 ms 2384 KB Output is correct
14 Correct 1 ms 2552 KB Output is correct
15 Correct 2 ms 2384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 2 ms 2640 KB Output is correct
3 Correct 2 ms 2640 KB Output is correct
4 Correct 1 ms 2384 KB Output is correct
5 Correct 64 ms 13116 KB Output is correct
6 Correct 1 ms 2384 KB Output is correct
7 Correct 1 ms 2384 KB Output is correct
8 Correct 1 ms 2384 KB Output is correct
9 Correct 2 ms 2552 KB Output is correct
10 Correct 1 ms 2384 KB Output is correct
11 Correct 1 ms 2596 KB Output is correct
12 Correct 2 ms 2640 KB Output is correct
13 Correct 2 ms 2640 KB Output is correct
14 Correct 2 ms 2640 KB Output is correct
15 Correct 2 ms 2640 KB Output is correct
16 Correct 2 ms 2640 KB Output is correct
17 Correct 4 ms 2832 KB Output is correct
18 Correct 2 ms 2640 KB Output is correct
19 Correct 3 ms 2904 KB Output is correct
20 Correct 1 ms 2384 KB Output is correct
21 Correct 1 ms 2384 KB Output is correct
22 Correct 1 ms 2384 KB Output is correct
23 Correct 2 ms 2384 KB Output is correct
24 Correct 7 ms 3404 KB Output is correct
25 Correct 1 ms 2384 KB Output is correct
26 Correct 1 ms 2384 KB Output is correct
27 Correct 1 ms 2384 KB Output is correct
28 Correct 1 ms 2384 KB Output is correct
29 Correct 6 ms 3404 KB Output is correct
30 Correct 1 ms 2384 KB Output is correct
31 Correct 1 ms 2384 KB Output is correct
32 Correct 2 ms 2640 KB Output is correct
33 Correct 2 ms 2640 KB Output is correct
34 Correct 3 ms 2640 KB Output is correct
35 Correct 4 ms 2832 KB Output is correct
36 Correct 3 ms 2896 KB Output is correct
37 Correct 1 ms 2384 KB Output is correct
38 Correct 1 ms 2384 KB Output is correct
39 Correct 1 ms 2552 KB Output is correct
40 Correct 2 ms 2384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 2 ms 2640 KB Output is correct
3 Correct 2 ms 2640 KB Output is correct
4 Correct 1 ms 2384 KB Output is correct
5 Correct 64 ms 13116 KB Output is correct
6 Correct 1 ms 2384 KB Output is correct
7 Correct 1 ms 2384 KB Output is correct
8 Correct 1 ms 2384 KB Output is correct
9 Correct 2 ms 2552 KB Output is correct
10 Correct 1 ms 2384 KB Output is correct
11 Correct 1 ms 2596 KB Output is correct
12 Correct 2 ms 2640 KB Output is correct
13 Correct 2 ms 2640 KB Output is correct
14 Correct 2 ms 2640 KB Output is correct
15 Correct 2 ms 2640 KB Output is correct
16 Correct 2 ms 2640 KB Output is correct
17 Correct 4 ms 2832 KB Output is correct
18 Correct 2 ms 2640 KB Output is correct
19 Correct 3 ms 2904 KB Output is correct
20 Correct 1 ms 2384 KB Output is correct
21 Correct 1 ms 2384 KB Output is correct
22 Correct 1 ms 2384 KB Output is correct
23 Correct 2 ms 2384 KB Output is correct
24 Correct 7 ms 3404 KB Output is correct
25 Correct 1 ms 2384 KB Output is correct
26 Correct 1 ms 2384 KB Output is correct
27 Correct 1 ms 2384 KB Output is correct
28 Correct 1 ms 2384 KB Output is correct
29 Correct 6 ms 3404 KB Output is correct
30 Correct 1 ms 2384 KB Output is correct
31 Correct 1 ms 2384 KB Output is correct
32 Correct 2 ms 2640 KB Output is correct
33 Correct 2 ms 2640 KB Output is correct
34 Correct 3 ms 2640 KB Output is correct
35 Correct 4 ms 2832 KB Output is correct
36 Correct 3 ms 2896 KB Output is correct
37 Correct 1 ms 2384 KB Output is correct
38 Correct 1 ms 2384 KB Output is correct
39 Correct 1 ms 2552 KB Output is correct
40 Correct 2 ms 2384 KB Output is correct
41 Correct 176 ms 23980 KB Output is correct
42 Correct 11 ms 3952 KB Output is correct
43 Correct 13 ms 3528 KB Output is correct
44 Correct 21 ms 4040 KB Output is correct
45 Correct 15 ms 4040 KB Output is correct
46 Correct 9 ms 3404 KB Output is correct
47 Correct 120 ms 18360 KB Output is correct
48 Correct 10 ms 4040 KB Output is correct
49 Correct 113 ms 23984 KB Output is correct