G. SlavicG‘s Favorite Problem

    图论细节题

思路:从特殊再到一般,没有传送的话 你可以直接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();
}

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值