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"<

相关内容

热门资讯

我来教大家“掼蛋大师辅助器ap... 亲,掼蛋大师这款游戏可以开挂的,确实是有挂的,。但是开挂要下载第三方辅助软件,掼蛋大师的开挂软件,名...
我来教大家“开心联盟辅助器ap... 您好,开心联盟这款游戏可以开挂的,确实是有挂的,需要了解加微{7198902}很多玩家在这款游戏中打...
我来教大家“友愉棋牌辅助器ap... 您好,友愉棋牌这款游戏可以开挂的,确实是有挂的,需要了解加微{7198902}很多玩家在这款游戏中打...
我来教大家“全盛棋牌辅助器ap... 亲,全盛棋牌这款游戏可以开挂的,确实是有挂的,。但是开挂要下载第三方辅助软件,全盛棋牌的开挂软件,名...
我来教大家“情怀三三麻将辅助器... 我来教大家“情怀三三麻将辅助器app官方下载最新版本更新”详细分享开挂一、情怀三三麻将AI软件牌型概...
我来教大家“无锡好运麻将辅助器... 亲,无锡好运麻将这款游戏可以开挂的,确实是有挂的,。但是开挂要下载第三方辅助软件,无锡好运麻将的开挂...
我来教大家“推筒子辅助器app... 您好,推筒子这款游戏可以开挂的,确实是有挂的,需要了解加微{7198902}很多玩家在这款游戏中打牌...
我来教大家“比奇堡辅助器app... 我来教大家“比奇堡辅助器app官方下载最新版本更新”详细分享开挂1、让任何用户在无需AI插件第三方神...
我来教大家“新兴茶苑辅助器ap... 我来教大家“新兴茶苑辅助器app官方下载最新版本更新”详细分享开挂1、让任何用户在无需AI插件第三方...
我来教大家“欢乐互动辅助器ap... 我来教大家“欢乐互动辅助器app官方下载最新版本更新”详细分享开挂1、让任何用户在无需AI插件第三方...
我来教大家“东方互娱辅助器ap... 亲,东方互娱这款游戏可以开挂的,确实是有挂的,。但是开挂要下载第三方辅助软件,东方互娱的开挂软件,名...
我来教大家“天天乐清麻将辅助器... 我来教大家“天天乐清麻将辅助器app官方下载最新版本更新”详细分享开挂一、天天乐清麻将AI软件牌型概...
我来教大家“唐三彩辅助器app... 我来教大家“唐三彩辅助器app官方下载最新版本更新”详细分享开挂一、唐三彩AI软件牌型概率发牌机制”...
我来教大家“Y0辅助器app官... 我来教大家“Y0辅助器app官方下载最新版本更新”详细分享开挂一、Y0AI软件牌型概率发牌机制”必胜...
我来教大家“天天互娱辅助器ap... 亲,天天互娱这款游戏可以开挂的,确实是有挂的,。但是开挂要下载第三方辅助软件,天天互娱的开挂软件,名...
我来教大家“悟空竞技麻将辅助器... 亲,悟空竞技麻将这款游戏可以开挂的,确实是有挂的,。但是开挂要下载第三方辅助软件,悟空竞技麻将的开挂...
我来教大家“情怀开化麻将辅助器... 我来教大家“情怀开化麻将辅助器app官方下载最新版本更新”详细分享开挂一、情怀开化麻将AI软件牌型概...
我来教大家“龙炎互娱辅助器ap... 我来教大家“龙炎互娱辅助器app官方下载最新版本更新”详细分享开挂一、龙炎互娱AI软件牌型概率发牌机...
我来教大家“闲逸碰胡辅助器ap... 亲,闲逸碰胡这款游戏可以开挂的,确实是有挂的,。但是开挂要下载第三方辅助软件,闲逸碰胡的开挂软件,名...
我来教大家“友聚麻将辅助器ap... 我来教大家“友聚麻将辅助器app官方下载最新版本更新”详细分享开挂一、友聚麻将AI软件牌型概率发牌机...