? ? 图论细节题
思路:从特殊再到一般,没有传送的话 你可以直接bfs一下看看,有的话就是终点和起点分别bfs
看看能不能得到一样的数值就好了,注意控制开始方向不要过终点以后还要走历完就好了
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 3e5+10;
struct Node{
int a,b,c;
}node[N];
vector<pair<int,int>>g[N];
int n,a,b;
set<int>s;
set<int>s1;
int t;
void dfs(int u,int father,int res){
if(u==b)return;
s.insert(res);
for(auto &t:g[u]){
int son = t.first;
int v = t.second;
//if(son==b)return;
if(son==father)continue;
//s.insert(res^v);
dfs(son,u,res^v);
}
}
void dfs1(int u,int father,int res){
for(auto &t:g[u]){
int son = t.first;
int v = t.second;
if(son==father)continue;
s1.insert(res^v);
dfs1(son,u,res^v);
}
}
void solve()
{
t++;
cin>>n>>a>>b;
for(int i=1;i<=n;i++)g[i].clear();
s.clear();
s1.clear();
for(int i=1;i<n;i++){
int a1,b1,c1;cin>>a1>>b1>>c1;
node[i] = {a1,b1,c1};
g[a1].push_back({b1,c1});
g[b1].push_back({a1,c1});
}
dfs(a,-1,0);
dfs1(b,-1,0);
// for(auto &t:s)cout<<t<<" ";
// cout<<"\n";
// for(auto &t1:s)cout<<t1<<" ";
// cout<<"\n";
//
for(auto &t:s){
if(s1.count(t)){
cout<<"YES"<<"\n";
return;
}
}
cout<<"NO"<<"\n";
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int _;cin>>_;
while(_--)solve();
}