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

本文介绍了解决Codeforces Round #835 (Div.4) G题的方法。主要讨论了如何在考虑边权异或的情况下找到两个节点之间的路径,并通过广度优先搜索来确定是否存在有效的传送点,使得从起点到终点的路径边权异或为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 a a a XOR a = 0 a=0 a=0 for all a). In other words, the only possible value of x x x equals the XOR of the edges on the unique path from a a a to b b b. We can find it through a a a BFS from a a a, 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 → c a→c ac, then teleport to d d d, and go from d → b d→b db, for some nodes c c c and d d d. Also, we cannot pass b b b on the path from a → c a→c ac.

Again, note that the value of x x x is fixed on each of the paths from a → c a→c ac and d → b d→b db, since there is a a a unique path between them. Let x 1 x_1 x1 be the XOR of the first path and x 2 x_2 x2 be the XOR of the second. Then we need x 1 x_1 x1 XOR x 2 = 0 x2=0 x2=0 ⟹ ⟹ x 1 = x 2 x_1=x_2 x1=x2. So we need to find if there are two nodes c c c, d d d 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 b b b, and check if any two values are the same.

Make sure not to include nodes past b b b while we look for c c c on our BFS from a a a.

The time complexity is O ( n l o g n ) O(nlogn) O(nlogn).

#include<bits/stdc++.h>
using namespace std;

typedef pair<int,int> PII;

int main()
{
    int T=1;cin>>T;
    while(T--)
    {
        int n,a,b;cin>>n>>a>>b;
        vector<vector<PII>> 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<vector<int>> f(2,vector<int>(n+1));
        function<void(int,int,int,int)> 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<int> 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"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}
### 关于 Codeforces Round 835 (Div. 4) 目前并未找到针对 **Codeforces Round 835 (Div. 4)** 的具体题目解析或官方比赛详情的相关引用[^1]。然而,可以基于以往的比赛模式推测该场比赛的内容结构以及可能涉及的算法知识点。 #### 比赛概述 CodeforcesDiv. 4 类型比赛通常面向新手选手设计,难度较低,适合初学者练习基础编程技能和简单算法逻辑。这类比赛一般包含以下特点: - 题目数量较多(通常是 7 到 10 道)。 - 前几道题考察基本输入输出、条件判断、循环等基础知识。 - 中间部分题目可能会涉及到简单的数学推导、字符串处理或者模拟操作。 - 后续较难的题目则会引入贪心策略、动态规划入门概念或者其他经典算法模型。 以下是根据经验总结的一些常见考点及其对应解决方法: --- #### 可能的题目类型及解决方案 ##### A. 简单计算 / 输入输出优化 此类问题往往只需要按照描述完成特定运算即可。例如求平均值、最大最小值等问题。 ```cpp #include <bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while(t--){ long long a, b; cin >> a >> b; cout << abs(a-b) << "\n"; } } ``` ##### B. 条件分支与枚举 通过列举所有可能性并逐一验证来得出答案。比如寻找满足某些性质的一组整数。 ```cpp for(int i=1;i<=sqrt(n);i++) { if(condition){ answer++; } } cout<<answer<<"\n"; ``` ##### C/D/E/F... 更复杂的逻辑推理 随着字母顺序增加,复杂度逐步提升。这些阶段的问题可能需要更深入的理解力才能解答出来。例如动态规划中的状态转移方程定义;图论里最短路径算法的应用等等。 由于缺乏具体的题目信息,在此仅提供通用框架作为参考模板供学习者模仿实践之用。 --- ### 提醒事项 对于任何一场竞赛而言,充分准备至关重要。建议参赛前复习相关资料,并尝试独立思考解决问题的方法而不是单纯依赖现成代码片段抄袭拼凑而成的作品提交评测系统检测运行效率是否达标最后记得测试边界情况确保程序鲁棒性强不易崩溃异常退出造成不必要的失分遗憾结局发生哦!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wa_Automata

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值