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 ...
随机推荐
- kubernetes之Endpoint引入外部资源实践;
1. 什么是Endpoint? 我们创建Service的时候会自动给我们创建一个同名的Endpoint资源,每一个同名的 Servie都有一个Endpoints资源,因为Service自己并不直接匹配 ...
- React报错之You provided a `checked` prop to a form field
正文从这开始~ 总览 当我们在多选框上设置了checked 属性,却没有onChange 处理函数时,会产生"You provided a checked prop to a form fi ...
- Math_Music
查看代码 #REmoo的优化任务 #1.公式写在<formula_set>类中,统一管理 --- Finished 2022.8.15 12:39 #2.建立<sample_set& ...
- 第二十四篇:对于dom的理解
好家伙, HTML CSS JS structure style function 结构体 样式 功能 <> ...
- 第二章 Kubernetes快速入门
一.四组基本概念 Pod/Pod控制器: Name/Namespace: Label/Label选择器: Service/Ingress. 二.Pod/Pod控制器 2.1 Pod Pod是K8S里能 ...
- KingbaseES V8R3集群运维案例之---主库系统down failover切换过程分析
案例说明: KingbaseES V8R3集群failover时两个cluster都会触发,但只有一个cluster会调用脚本去执行真正的切换流程,另一个有对应的打印,但不会调用脚本,只是走相关的 ...
- KingbaseES 中实现mysql的from_days和to_days
mysql中两个函数的说明: TO_DAYS(date)给出一个日期date,返回一个天数. FROM_DAYS(N)给出一个天数N,返回一个DATE值. 两个函数比较计算的日期都是 0000-01- ...
- KingbaseES通过sys_waldump解析wal日志
前言 oracle中的redo日志我们无法直接读取,然而对于KingbaseES数据库,我们可以利用sys_waldump工具解析wal日志,查看wal日志记录的信息. 我们可以利用 sys_wald ...
- PLSQL 与 PLPGSQL
KingbaseES 为了更好地适应用户的oracle 应用,实现了对 plsql 的支持,用户可以根据需要使用 plsql 或 plpgsql. 以下简要介绍下二者的差异 一.格式差异 1.plpg ...
- mysql explain总结
Explain 包含字段 id select_type table type possible_keys key key_len ref rows extra 字段解释 1. id id 相同则执行顺 ...