CF1760G.SlavicG's Favorite Problem
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a weighted tree with n vertices. Recall that a tree is a connected graph without any cycles. A weighted tree is a tree in which each edge has a certain weight. The tree is undirected, it doesn't have a root.
Since trees bore you, you decided to challenge yourself and play a game on the given tree.
In a move, you can travel from a node to one of its neighbors (another node it has a direct edge with).
You start with a variable x which is initially equal to 0. When you pass through edge i, x changes its value to x XOR wi (where wi is the weight of the i-th edge).
Your task is to go from vertex a to vertex b, but you are allowed to enter node b if and only if after traveling to it, the value of x will become 0. In other words, you can travel to node b only by using an edge i such that x XOR wi=0. Once you enter node b the game ends and you win.
Additionally, you can teleport at most once at any point in time to any vertex except vertex b. You can teleport from any vertex, even from a.
Answer with "YES" if you can reach vertex b from a, and "NO" otherwise.
Note that XOR represents the bitwise XOR operation.
你被给定一棵包含 n 个顶点的带权树。回忆一下,树是一种无环的连通图。带权树是指每条边都具有某个权重的树。该树是无向的,没有根节点。
由于树让你感到乏味,你决定挑战自我,在给定的树上进行一场游戏。
在一次移动中,你可以从一个节点移动到其任意一个相邻节点(即与之有直接边相连的另一节点)。
你初始时拥有一个变量 x,其值为 0。当你经过第 i 条边时,x 的值会更新为 x XOR wi(其中 wi 表示第 i 条边的权重)。
你的任务是从顶点 a 出发到达顶点 b,但你仅当抵达 b 后 x 的值恰好变为 0 时,才被允许进入节点 b。换言之,你只能通过某条边 i 进入 b,且必须满足 x XOR wi=0。一旦你进入节点 b,游戏立即结束,你获胜。
此外,你最多可使用一次瞬移:在任意时刻,你可以瞬移到除顶点 b 外的任意顶点(包括从 a 或任意其他顶点瞬移)。
若能从顶点 a 到达顶点 b,输出 "YES";否则输出 "NO"。
注意:XOR 表示按位异或运算。
输入格式
The first line contains a single integer t (1≤t≤1000) — the number of test cases.
The first line of each test case contains three integers n, a, and b (2≤n≤105), (1≤a,b≤n;a=b) — the number of vertices, and the starting and desired ending node respectively.
Each of the next n−1 lines denotes an edge of the tree. Edge i is denoted by three integers ui, vi and wi — the labels of vertices it connects (1≤ui,vi≤n;ui=vi;1≤wi≤109) and the weight of the respective edge.
It is guaranteed that the sum of n over all test cases does not exceed 105.
第一行包含一个整数 t(1≤t≤1000)——测试用例的数量。
每个测试用例的第一行包含三个整数 n、a 和 b(2≤n≤105,1≤a,b≤n;且 a=b)——分别表示顶点数量、起点和目标终点。
接下来的 n−1 行每行描述树中的一条边。第 i 条边由三个整数 ui、vi 和 wi 表示——即该边所连接的两个顶点的编号(1≤ui,vi≤n;ui=vi;1≤wi≤109)以及该边的权重。
保证所有测试用例中 n 的总和不超过 105。
输出格式
For each test case output "YES" if you can reach vertex b, and "NO" otherwise.
对于每个测试用例,若可以到达顶点 b,则输出 “YES”,否则输出 “NO”。
输入输出样例
输入#1
3 5 1 4 1 3 1 2 3 2 4 3 3 3 5 1 2 1 2 1 2 2 6 2 3 1 2 1 2 3 1 3 4 1 4 5 3 5 6 5
输出#1
YES NO YES
说明/提示
For the first test case, we can travel from node 1 to node 3, x changing from 0 to 1, then we travel from node 3 to node 2, x becoming equal to 3. Now, we can teleport to node 3 and travel from node 3 to node 4, reaching node b, since x became equal to 0 in the end, so we should answer "YES".
For the second test case, we have no moves, since we can't teleport to node b and the only move we have is to travel to node 2 which is impossible since x wouldn't be equal to 0 when reaching it, so we should answer "NO".
对于第一个测试用例,我们可以从节点 1 走到节点 3,此时 x 从 0 变为 1;接着从节点 3 走到节点 2,此时 x 变为 3。此时,我们可以传送到节点 3,再从节点 3 走到节点 4(即目标节点 b),最终 x 恰好变为 0,因此应输出 "YES"。
对于第二个测试用例,我们无法进行任何操作:既不能直接传送到节点 b,唯一可行的移动是走到节点 2,但该操作不可行,因为到达节点 2 时 x 不会等于 0。因此应输出 "NO"。
输入解题思路,AI测评打分。不知道怎么写?