CF1760G.SlavicG's Favorite Problem

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a weighted tree with nn 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 xx which is initially equal to 00. When you pass through edge ii, xx changes its value to x XOR wix ~\mathsf{XOR}~ w_i (where wiw_i is the weight of the ii-th edge).

Your task is to go from vertex aa to vertex bb, but you are allowed to enter node bb if and only if after traveling to it, the value of xx will become 00. In other words, you can travel to node bb only by using an edge ii such that x XOR wi=0x ~\mathsf{XOR}~ w_i = 0. Once you enter node bb the game ends and you win.

Additionally, you can teleport at most once at any point in time to any vertex except vertex bb. You can teleport from any vertex, even from aa.

Answer with "YES" if you can reach vertex bb from aa, and "NO" otherwise.

Note that XOR\mathsf{XOR} represents the bitwise XOR operation.

你被给定一棵包含 nn 个顶点的带权树。回忆一下,树是一种无环的连通图。带权树是指每条边都具有某个权重的树。该树是无向的,没有根节点。

由于树让你感到乏味,你决定挑战自我,在给定的树上进行一场游戏。

在一次移动中,你可以从一个节点移动到其任意一个相邻节点(即与之有直接边相连的另一节点)。

你初始时拥有一个变量 xx,其值为 00。当你经过第 ii 条边时,xx 的值会更新为 x XOR wix ~\mathsf{XOR}~ w_i(其中 wiw_i 表示第 ii 条边的权重)。

你的任务是从顶点 aa 出发到达顶点 bb,但你仅当抵达 bb 后 xx 的值恰好变为 00 时,才被允许进入节点 bb。换言之,你只能通过某条边 ii 进入 bb,且必须满足 x XOR wi=0x ~\mathsf{XOR}~ w_i = 0。一旦你进入节点 bb,游戏立即结束,你获胜。

此外,你最多可使用一次瞬移:在任意时刻,你可以瞬移到除顶点 bb 外的任意顶点(包括从 aa 或任意其他顶点瞬移)。

若能从顶点 aa 到达顶点 bb,输出 "YES";否则输出 "NO"。

注意:XOR\mathsf{XOR} 表示按位异或运算。

输入格式

The first line contains a single integer tt (1≤t≤10001 \leq t \leq 1000) — the number of test cases.

The first line of each test case contains three integers nn, aa, and bb (2≤n≤1052 \leq n \leq 10^5), (1≤a,b≤n;a≠b1 \leq a, b \leq n; a \ne b) — the number of vertices, and the starting and desired ending node respectively.

Each of the next n−1n-1 lines denotes an edge of the tree. Edge ii is denoted by three integers uiu_i, viv_i and wiw_i — the labels of vertices it connects (1≤ui,vi≤n;ui≠vi;1≤wi≤1091 \leq u_i, v_i \leq n; u_i \ne v_i; 1 \leq w_i \leq 10^9) and the weight of the respective edge.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

第一行包含一个整数 tt(1≤t≤10001 \leq t \leq 1000)——测试用例的数量。

每个测试用例的第一行包含三个整数 nn、aa 和 bb(2≤n≤1052 \leq n \leq 10^5,1≤a,b≤n1 \leq a, b \leq n;且 a≠ba \ne b)——分别表示顶点数量、起点和目标终点。

接下来的 n−1n-1 行每行描述树中的一条边。第 ii 条边由三个整数 uiu_i、viv_i 和 wiw_i 表示——即该边所连接的两个顶点的编号(1≤ui,vi≤n1 \leq u_i, v_i \leq n;ui≠viu_i \ne v_i;1≤wi≤1091 \leq w_i \leq 10^9)以及该边的权重。

保证所有测试用例中 nn 的总和不超过 10510^5。

输出格式

For each test case output "YES" if you can reach vertex bb, and "NO" otherwise.

对于每个测试用例,若可以到达顶点 bb,则输出 “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 11 to node 33, xx changing from 00 to 11, then we travel from node 33 to node 22, xx becoming equal to 33. Now, we can teleport to node 33 and travel from node 33 to node 44, reaching node bb, since xx became equal to 00 in the end, so we should answer "YES".

For the second test case, we have no moves, since we can't teleport to node bb and the only move we have is to travel to node 22 which is impossible since xx wouldn't be equal to 00 when reaching it, so we should answer "NO".

对于第一个测试用例,我们可以从节点 11 走到节点 33,此时 xx 从 00 变为 11;接着从节点 33 走到节点 22,此时 xx 变为 33。此时,我们可以传送到节点 33,再从节点 33 走到节点 44(即目标节点 bb),最终 xx 恰好变为 00,因此应输出 "YES"。

对于第二个测试用例,我们无法进行任何操作:既不能直接传送到节点 bb,唯一可行的移动是走到节点 22,但该操作不可行,因为到达节点 22 时 xx 不会等于 00。因此应输出 "NO"。

输入解题思路,AI测评打分。不知道怎么写?

首页