1659E - AND-MEX Walk - CodeForces Solution


bitmasks brute force constructive algorithms dfs and similar dsu graphs *2200

Please click on ads to support us..

C++ Code:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#define lowbit(x) ((x) & (-x))
#define PI acos(-1)
#define el "\n"
#define inf 0x3f3f3f3f
using namespace std;
using ll = long long;
using PII = pair<int, int>;
const int N = 1e5 + 10;
const int mod = 1e9 + 7;

int fa[35][N], is[35][N];

void init(int n) {
    for (int i = 0; i < 31; i ++) {
        for (int j = 1; j <= n; j ++) {
            fa[i][j] = j;
            is[i][j] = 0;
        }
    }
}

int find(int p, int x) {
    if (fa[p][x] == x) return x;
    else return fa[p][x] = find(p, fa[p][x]);
}

void merge(int p, int x, int y) {
    int fx = find(p, x), fy = find(p, y);
    if (fx == fy) return;
    fa[p][fx] = fy;
}   

vector<PII> G[N];

void solve() {
    int n, m;
    cin >> n >> m;
    init(n);
    for (int i = 1; i <= m; i ++) {
        int u, v, w;
        cin >> u >> v >> w;
        G[u].push_back({v, w});
        G[v].push_back({u, w});  
        for (int j = 0; j <= 30; j ++) {
            int x = (w >> j) & 1;
            if (x) merge(j, u, v);
        }
    }
    for (int u = 1; u <= n; u ++) {
        for (auto [v, w] : G[u]) {
            if (w % 2 == 0) {
                for (int k = 1; k <= 30; k ++) {
                    is[k][find(k, u)] = 1;
                    is[k][find(k, v)] = 1;
                }
            }
        }
    }
    int q;
    cin >> q;
    while (q--) {
        int u, v;
        cin >> u >> v;
        int ans = 2;
        for (int i = 0; i <= 30; i ++) {
            if (find(i, u) == find(i, v)) {
                ans = 0;
                break;
            }
        }
        if (ans == 2) {
            for (int k = 1; k <= 30; k ++) {
                if (is[k][find(k, u)]) {
                    ans = 1;
                    break;
                }
            }
        }
        cout << ans << el;
    }
}

int main() {

    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int T = 1;
    // cin >> T;
    while (T--) {
        solve();
    }
    
    return 0;
}

/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/


Comments

Submit
0 Comments
More Questions

275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome