QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#578403#6679. Not Another Path Query Problemshiqiaqiaya#WA 0ms3696kbC++203.2kb2024-09-20 18:54:372024-09-20 18:54:37

Judging History

This is the latest submission verdict.

  • [2024-09-20 18:54:37]
  • Judged
  • Verdict: WA
  • Time: 0ms
  • Memory: 3696kb
  • [2024-09-20 18:54:37]
  • Submitted

answer

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define int long long
mt19937_64 rd(time(0));
template <class key, class val = null_type, class cmp = less<>>
using rb_tree = tree<key, val, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template <class T, class ... A>
void debug(const T & t, const A & ... a) { cerr << "[" << t, ((cerr << ", " << a), ...), cerr << "]\n"; }
const int N = 1e5 + 10;
int fa[N];
int find(int x) {
    return fa[x] = fa[x] == x ? x : find(fa[x]);
}
void QAQ() {
    int n, m, q , V;
    cin >> n >> m >> q >> V;

    for (int i = 1; i <= n; i++) {
        fa[i] = i;
    }
    vector<array<int, 3>> e(m);
    for (int i = 0; i < m; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        e[i] = {w, u, v};
    }

    vector<array<int, 3>> Q(q);
    vector<int> ans(q);
    for (int i = 0; i < q; i++) {
        int u, v;
        cin >> u >> v;
        Q[i] = {u, v, i};
    }

    if (V == 0) {
        for (int i = 0; i < m; i++) {
            auto [w, u, v] = e[i];
            u = find(u), v = find(v);
            if (u == v) continue;
            fa[u] = v;
        }

        for (auto [u, v, i] : Q) {
            ans[i] = find(u) == find(v);
        }

        for (int i = 0; i < q; i++) {
            cout << (ans[i] ? "Yes" : "No") << "\n";
        }
        
        return;
    }

    int l = 0, r = m;
    int j = 62; 
    while ((V >> j) == 0) j--;
    for (; j >= 0; j--) {
        int v = (V >> j) & 1;
        if (v == 0) {
            int mid = partition(e.begin() + l, e.begin() + r, [&](auto x) {
                return (x[0] >> j) & 1;
            }) - e.begin();
 for (int i = 1; i <= n; i++) {
        fa[i] = i;
    }
            for (int i = l; i < mid; i++) {
                int u = e[i][1], v = e[i][2];
                u = find(u), v = find(v);
                if (u == v) continue;
                fa[u] = v;
            }
            vector<array<int, 3>> tmp;
            for (auto &[u, v, i] : Q) {
                if (find(u) == find(v)) {
                    ans[i] = 1;
                } else {
                    tmp.push_back({u, v, i});
                }
            }
            swap(Q, tmp);
            l = mid;
        } else {
            int mid = partition(e.begin() + l, e.begin() + r, [&](auto x) {
                return (x[0] >> j) & 1;
            }) - e.begin();
            r = mid;
        }
        // debug(j, v, l, r);
    }

for (int i = 1; i <= n; i++) {
        fa[i] = i;
    }

    for (int i = l; i < r; i++) {
        int u = e[i][1], v = e[i][2];
        u = find(u), v = find(v);
        if (u == v) continue;
        fa[u] = v;
    }
    for (auto &[u, v, i] : Q) {
        if (find(u) == find(v)) {
            ans[i] = 1;
        }
    }

    for (int i = 0; i < q; i++) {
        cout << (ans[i] ? "Yes" : "No") << "\n";
    }
}

signed main() {
    int t = 1;
    //cin >> t;
    for (cin.tie(0) -> sync_with_stdio(0), cout << fixed << setprecision(12); t--; QAQ());
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3696kb

input:

9 8 4 5
1 2 8
1 3 7
2 4 1
3 4 14
2 5 9
4 5 7
5 6 6
3 7 15
1 6
2 7
7 6
1 8

output:

Yes
No
Yes
No

result:

ok 4 token(s): yes count is 2, no count is 2

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 3644kb

input:

3 4 1 4
1 2 3
1 2 5
2 3 2
2 3 6
1 3

output:

No

result:

wrong answer expected YES, found NO [1st token]