首页 > 其他分享 >Codeforces 1670 E. Hemose on the Tree

Codeforces 1670 E. Hemose on the Tree

时间:2022-11-01 19:55:09浏览次数:46  
标签:int Tree st Hemose 权值 一定 300005 1670 节点

题意

给你个数p,n = 2^p;
有一棵树有n个节点,告诉你怎么连边;
每个点有个权值,每条边也有个权值,权值需要自行分配,[1,2,3..n...2n-1],总共2n-1个权值;
你需要选一个节点,使得他到所有其他边或者节点的简单路径的异或最大值最小。

思路

显然,给你个p,不直接给你n一定是有潜藏的东西的,分析一下,n = 2^p, 那么n 的结构一定是 1 000000 ,1后面都是0,那可以推测出最终的答案一定是小于等于n的

1. 初始节点可以随便选的,但是它的值一定设为n
2. 处于一个点的连接点与边来说,他们的关系一定是x,x+n,这样他们的异或值一定是n,可以保证答案在0-n之间改变,注意x与x+n的位置设置
3. 如果不这样构造,最高位是1的情况下,一定不优于这种构造

代码

#include<bits/stdc++.h>

using namespace std;
int n, p, st;
vector<pair<int, int>> g[300005];
int ans[300005], w[300005];

void dfs(int x, int fa, int pre) {
    for (auto k: g[x]) {
        if (k.first == fa)continue;
        st++;
        if ((st ^ pre) <= n) {
            w[k.second] = st;
            ans[k.first] = st + n;
        } else {
            w[k.second] = st + n;
            ans[k.first] = st;
        }
        dfs(k.first, x, pre ^ n);
    }
}

void run() {
    cin >> p;
    n = 1 << p;
    for (int i = 1; i <= n; i++)g[i].clear();
    for (int i = 1; i < n; i++) {
        int x, y;
        cin >> x >> y;
        g[x].push_back(make_pair(y, i));
        g[y].push_back(make_pair(x, i));
    }
    st = 0;
    ans[1] = n;
    dfs(1, 0, n);
    cout << 1 << '\n';
    for (int i = 1; i <= n; i++)cout << ans[i] << " \n"[i == n];
    for (int i = 1; i < n; i++)cout << w[i] << " \n"[i == n - 1];
}

int main() {

    int t;
    cin >> t;
    while (t--)
        run();
    return 0;
}

标签:int,Tree,st,Hemose,权值,一定,300005,1670,节点
From: https://www.cnblogs.com/4VDP/p/16848942.html

相关文章

  • Tree 的
    二叉树 就是计划生育政策下,每个人只能最多生两个儿子的意思。 并且分为左子树和右子树。 二叉树又有二叉查找树,也就是二叉排序树。即,左节点都比自己小,右节点都比......
  • TreeSet实现类使用
    【1】存入Integer类型数据(底层用的是内部比较器)packagecom.msb.test09;importjava.util.TreeSet;/***@author:liu*日期:16:09:57*描述:IntelliJIDEA......
  • 机器学习 之 决策树(Decision Tree)文本算法的精确率
    目录​​0、推荐​​​​1、背景​​​​2、效果图​​​​3、本次实验整体流程​​​​4、这里用词向量,而不是TF-IDF预处理后的向量​​​​5、源代码​​​​6、知识点普......
  • Tree Longest Path 2246
    2246. LongestPathWithDifferentAdjacentCharactersHardYouaregivena tree (i.e.aconnected,undirectedgraphthathasnocycles) rooted atnod......
  • UVA11297 Census(kd-tree)
    题意:给定一个\(n\timesn\)的网格,要求支持修改和询问某个矩阵的最大值和最小值。解法多样,可以用二维线段树,我用的是\(kd-tree\)。那么这题就很裸了,我在这里只提几点需......
  • UI界面+treeviwe+dataGridView
    usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Runtime.Interop......
  • SourceTree的安装跳过注册账户
    今天安装sourcetree一直卡在注册界面,后来使用以下方法跳过注册步骤,亲测可用。1、地址栏直接输入%LocalAppData%\Atlassian(或者可以选择下载个EveryThing,搜索到目标文件夹......
  • LeetCode 2458. Height of Binary Tree After Subtree Removal Queries
    原题链接在这里:https://leetcode.com/problems/height-of-binary-tree-after-subtree-removal-queries/题目:Youaregiventhe root ofa binarytree with n node......
  • 【XSY3549】Tree(线段树,换根)
    原题不想说(太懒了),就说一下总结到的两点想法?对于树上多次询问路径信息的问题,如果两条路径的信息无法快速合并(即不能pushup),但是在路径两端增加/删除单点后的信息变化可以......
  • 解决Vue项目: verbose stack Error: unable to resolve dependency tree
    解决Vue项目:verbosestackError:unabletoresolvedependencytree问题npminstall报错如上。解决......