bitmasks dfs and similar graphs *1700

Please click on ads to support us..

Python Code:

from collections import Counter
import sys, threading 

input = lambda: sys.stdin.readline().strip()

def main():

    for _ in range(int(input())):

        n, a, b = map(int, input().split())
        
        adj = [[] for _ in range(n + 1)]
        for _ in range(n - 1):
            u, v, w = map(int, input().split())
            adj[u].append((v, w))
            adj[v].append((u, w))

        ok, cnt = False, Counter()
        
        def dfs1(u, p, xor):
            
            cnt[xor] += 1
            for v, w in adj[u]:
                if v == p:
                    continue
                dfs1(v, u, xor ^ w)
        
        for v, w in adj[b]:
            dfs1(v, b, w)

        def dfs2(u, p, xor):
            nonlocal ok 

            if u == b:
                return 

            if cnt[xor]:
                ok = True 
                return 

            for v, w in adj[u]:
                if v == p:
                    continue
                dfs2(v, u, xor ^ w)
        
        dfs2(a, -1, 0)

        print("YES" if ok else "NO")

if __name__ == '__main__':
    
    sys.setrecursionlimit(1 << 30)
    threading.stack_size(1 << 27)

    main_thread = threading.Thread(target=main)
    main_thread.start()
    main_thread.join()
    
    

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long
#define pb push_back
#define sz size()
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
void start(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}
const ll N = 1e5+10;
const ll mod=1e9+7;
const ll inf=1e10;
map<ll,ll> mp;
vector <vector <pair<ll,ll> > > g(N);
ll x,y;
bool flag=false;
void dfs(ll v, ll pr,ll k){
    if(v==y && k!=0) return;
    else if(v==y) flag=true;
    mp[k]++;
    for(auto i : g[v]){
        if(i.fr==pr) continue;
        dfs(i.fr,v,k^i.sc);
    }
}
bool dfs2(ll v,ll pr,ll k){
    if(v!=y && mp[k]>0){
        return true;
    }
    for(auto i : g[v]){
        if(i.fr==pr) continue;
        if(dfs2(i.fr,v,k^i.sc)) return true;
    }
    return false;
}
void solve(){
    ll a,b,i,j;
    ll n,w;
    cin>>n>>x>>y;
    mp.clear();
    for(i=1;i<=n;i++) g[i].clear();
    for(i=1;i<n;i++){
        cin>>a>>b>>w;
        g[b].pb({a,w});
        g[a].pb({b,w});
    }
    flag=false;
    dfs(x,-1,0);
    if(dfs2(y,-1,0) || flag) cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
}
main(){
    //fre("");
    start();
    ll t=1;
    cin>>t;
    while(t--)solve();

}


Comments

Submit
0 Comments
More Questions

1651D - Nearest Excluded Points
599A - Patrick and Shopping
237A - Free Cash
1615B - And It's Non-Zero
1619E - MEX and Increments
34B - Sale
1436A - Reorder
1363C - Game On Leaves
1373C - Pluses and Minuses
1173B - Nauuo and Chess
318B - Strings of Power
1625A - Ancient Civilization
864A - Fair Game
1663B - Mike's Sequence
448A - Rewards
1622A - Construct a Rectangle
1620A - Equal or Not Equal
1517A - Sum of 2050
620A - Professor GukiZ's Robot
1342A - Road To Zero
1520A - Do Not Be Distracted
352A - Jeff and Digits
1327A - Sum of Odd Integers
1276A - As Simple as One and Two
812C - Sagheer and Nubian Market
272A - Dima and Friends
1352C - K-th Not Divisible by n
545C - Woodcutters
1528B - Kavi on Pairing Duty
339B - Xenia and Ringroad