Codeforces Round #835 (Div. 4) G. SlavicG‘s Favorite Problem
admin
2024-02-09 18:01:35
0

Codeforces Round #835 (Div. 4) G. SlavicG’s Favorite Problem

Let’s ignore the teleporting, and decide how to find the answer. Note that we don’t need to ever go over an edge more than once, since going over an edge twice cancels out (since aaa XOR a=0a=0a=0 for all a). In other words, the only possible value of xxx equals the XOR of the edges on the unique path from aaa to bbb. We can find it through aaa BFS from aaa, continuing to keep track of XOR s as we move to each adjacent node, and XOR ing it by the weight of the corresponding edge as we travel across it.

Now let’s include the teleport. It means that we travel from a→ca→ca→c, then teleport to ddd, and go from d→bd→bd→b, for some nodes ccc and ddd. Also, we cannot pass bbb on the path from a→ca→ca→c.

Again, note that the value of xxx is fixed on each of the paths from a→ca→ca→c and d→bd→bd→b, since there is aaa unique path between them. Let x1x_1x1​ be the XOR of the first path and x2x_2x2​ be the XOR of the second. Then we need x1x_1x1​ XOR x2=0x2=0x2=0 ⟹⟹⟹ x1=x2x_1=x_2x1​=x2​. So we need to find if there are two nodes ccc, ddd such that the XORs from a and b to those nodes are the same. To do this, we can do our BFS from before, but instead run one BFS from a and another from bbb, and check if any two values are the same.

Make sure not to include nodes past bbb while we look for ccc on our BFS from aaa.

The time complexity is O(nlogn)O(nlogn)O(nlogn).

#include
using namespace std;typedef pair PII;int main()
{int T=1;cin>>T;while(T--){int n,a,b;cin>>n>>a>>b;vector> g(n+1);for(int i=1;i<=n-1;i++){int u,v,w;cin>>u>>v>>w;g[u].push_back({v,w});g[v].push_back({u,w});}vector> f(2,vector(n+1));function dfs=[&](int u,int val,int id,int fa){f[id][u]=val;for(auto &[v,p]:g[u]){if(v==fa || v==b) continue;dfs(v,val^p,id,u);}};dfs(a,0,0,-1);dfs(b,0,1,-1);set st;for(int i=1;i<=n;i++)st.insert(f[0][i]);bool ok=false;for(int i=1;i<=n;i++)if(i!=b && st.count(f[1][i]))ok=true;if(ok) cout<<"YES"<

相关内容

热门资讯

安卓系统如何玩渠道服,渠道服游... 你有没有想过,在安卓系统上玩渠道服,那感觉简直就像是在游戏世界里开挂一样?没错,今天就要来给你揭秘,...
安卓系统等级在哪里查看,安卓系... 你有没有好奇过,你的安卓手机里那些神秘的系统等级到底在哪里可以查看呢?别急,今天就来给你揭秘这个小小...
自己制作安卓系统教程,自制安卓... 亲爱的读者们,你是否曾梦想过摆脱安卓系统的束缚,亲手打造一个只属于你自己的操作系统?别再羡慕那些技术...
安卓系统调整器下载,轻松优化手... 你有没有发现,手机用久了,系统总是有点小问题,比如卡顿啦,电池续航不给力啦,这些小烦恼是不是让你头疼...
怎样升级安卓系统视频,安卓系统... 亲爱的手机控们,你是否也和我一样,对手机系统升级充满了好奇和期待?想象你的安卓手机在经过一番“变身”...
鸿蒙系统和安卓系统哪个广告少,... 你有没有发现,现在手机市场上的操作系统真是五花八门,让人挑花了眼。不过,最近有个话题特别火,那就是鸿...
安卓系统openrec怎么注册... 你有没有想过,想要在安卓系统上体验一把OpenRec的乐趣,却发现注册步骤有点让人摸不着头脑?别急,...
怎样更新车机安卓系统,车机安卓... 亲爱的车主朋友们,你是不是也和我一样,对车机系统里的安卓系统充满了好奇?想要让它焕然一新,变得更加强...
安卓机换系统后照片,照片如何完... 你有没有遇到过这种情况:手机里的照片突然消失得无影无踪,心里那个急啊,就像热锅上的蚂蚁。别担心,今天...
安卓手机定位系统软件,技术原理... 你有没有想过,你的安卓手机里那些神奇的定位系统软件是怎么工作的呢?它们就像你的私人侦探,随时随地告诉...
安卓制作win系统盘,打造Wi... 亲爱的读者,你是否曾想过,将安卓系统的魅力与Windows系统的强大功能完美结合?今天,就让我带你一...
系统警告_您的安卓手机,揭秘潜... 亲爱的手机主人,最近你的安卓手机是不是突然跳出来一个系统警告,让你心头一紧?别慌,今天就来给你详细解...
投屏安卓系统版本,揭秘不同版本... 你有没有想过,家里的电视屏幕那么大,却只能用它来看电视?现在,有了安卓系统,你就可以把手机上的精彩内...
安卓官方系统升级软件,畅享智能... 你有没有发现,你的安卓手机最近是不是变得有点儿“年轻”了?没错,这就是安卓官方系统升级的魅力所在!今...
安卓系统铃声长度是多少,时长差... 你有没有想过,为什么你的手机每次收到消息时,都会响起那熟悉的铃声?是不是好奇过,安卓系统的铃声长度到...
酷派电脑系统安卓,深度解析与全... 亲爱的读者们,你是否曾对那些在电脑世界里游刃有余的酷派电脑系统安卓版心生好奇?今天,就让我带你一起揭...
什么系统可以装安卓软件,基于A... 你有没有想过,你的手机里那些好玩又实用的安卓软件,其实也可以在其他设备上运行呢?没错,这就是今天我们...
制作安卓系统主题软件,安卓系统... 你有没有想过,给你的安卓手机换一个全新的面貌?没错,就是那种一打开手机,就能感受到完全不同的风格和氛...
安卓系统平板怎么截屏,操作指南... 亲爱的平板用户,你是不是也和我一样,有时候想记录下平板上的精彩瞬间,却发现截屏功能有点神秘?别担心,...
安卓系统不推送更新,揭秘背后的... 最近是不是发现你的安卓手机有点儿“懒”啊?更新推送总是慢吞吞的,让人等得花儿都谢了。别急,今天就来给...