Codeforces 1670 E. Hemose on the Tree
题意
给你个数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;
}
Codeforces 1670 E. Hemose on the Tree的更多相关文章
- codeforces 1065F Up and Down the Tree
题目链接:codeforces 1065F Up and Down the Tree 题意:给出一棵树的节点数\(n\)以及一次移动的最大距离\(k\),现在有一个标记在根节点1处,每一次可以进行一下 ...
- Codeforces 914H Ember and Storm's Tree Game 【DP】*
Codeforces 914H Ember and Storm's Tree Game 题目链接 ORZ佬 果然出了一套自闭题 这题让你算出第一个人有必胜策略的方案数 然后我们就发现必胜的条件就是树上 ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Educational Codeforces Round 6 E. New Year Tree dfs+线段树
题目链接:http://codeforces.com/contest/620/problem/E E. New Year Tree time limit per test 3 seconds memo ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树
题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...
- Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA链上最大值
E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...
- Codeforces 600E. Lomsat gelral(Dsu on tree学习)
题目链接:http://codeforces.com/problemset/problem/600/E n个点的有根树,以1为根,每个点有一种颜色.我们称一种颜色占领了一个子树当且仅当没有其他颜色在这 ...
- Codeforces Round #540 (Div. 3)--1118F1 - Tree Cutting (Easy Version)
https://codeforces.com/contest/1118/problem/F1 #include<bits/stdc++.h> using namespace std; in ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 模拟
D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...
随机推荐
- ubantu安装中文失败的改正方法
情况说明 一开始我也是像博主们那样安装,但是一直在这个界面安装失败,太悲伤了!! 报错内容 c Failed to fetch http://us.archive.ubuntu.com/ubuntu/ ...
- HEXO-admin安装和使用(汉化版)
hi,大家好,我是KINGWDY,众所周知我用的是hexo,写博文首先要在终端输入hexo n xxxxx,然后打开MWeb PRO开始写md,但是,这很麻烦,就在我一筹莫展之际,我看到了这篇博文-- ...
- QFile 对文件进行读写操作
QFile 对文件进行读写操作 1 QFile 进行读写操纵 2 QFile file(pah ) 文件路径 3 读 file.open(打开方式) file.readAll(). file.re ...
- Docker安装Openresty总结
1. 启动Docker systemctl start docker 2. 查询有没有openresty镜像 docker search openresty -s 30 -s 30 stars数大于3 ...
- 如何干涉MySQL优化器使用hash join
GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. GreatSQL是MySQL的国产分支版本,使用上与MySQL一致. 前言 实验 总结 前言 数据库的优化器相当于人类的大 ...
- 002-ImageNetClassificationDeep2017
ImageNet classification with deep convolutional neural networks #paper 1. paper-info 1.1 Metadata Au ...
- 关于指针初始化为NULL的一些问题
关于指针初始化问题,先看以下代码: #include <stdio.h>typedef struct{ char data[128]; int top;} Stack;voi ...
- 2020年12月-第02阶段-前端基础-CSS Day06
CSS Day06 定位(position) 理解 能说出为什么要用定位 能说出定位的4种分类 能说出四种定位的各自特点 能说出我们为什么常用子绝父相布局 应用 能写出淘宝轮播图布局 1. CSS 布 ...
- fabric compose文件解读(CA篇)
CA在fabric中的作用是:分配证书,实现身份认证,配普通的CA机构没什么区别(所以可以用其他CA机构颁发的证书,只要商量好就行) 我的一段CA的conpose文件 1 services: 2 ca ...
- 数论进阶 
数论进阶 扩展欧几里得算法 裴蜀定理(Bézout's identity) \(1\) :对于任意整数 \(a\),\(b\) ,存在一对整数 \(x\) ,\(y\) ,满足 \(ax+by=GCD ...