cf1182D Complete Mirror
- 可以得到一个结论, 可行的点要么是直径端点, 要么是直径中点, 要么是直径中点引出的链中最短的端点
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#define mmp make_pair
#define ll long long
#define M 100010
using namespace std;
int read() {
int nm = 0, f = 1;
char c = getchar();
for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
return nm * f;
}
vector<int> to[M];
int n;
int sta[M], tp, a, b, mid, maxx;
int du[M], deep[M], af[M];
vector<int> note[M];
void dfss(int now, int f) {
deep[now] = deep[f] + 1;
note[deep[now]].push_back(du[now]);
for(int i = 0; i < to[now].size(); i++) {
int vj = to[now][i];
if(vj == f) continue;
dfss(vj, now);
}
}
void dfs(int now, int f) {
sta[++tp] = now;
if(tp > maxx) {
maxx = tp;
b = now, mid = sta[(tp + 1) >> 1];
}
for(int i = 0; i < to[now].size(); i++) {
int vj = to[now][i];
if(vj == f) continue;
dfs(vj, now);
}
tp--;
}
void check(int x) {
for(int i = 1; i <= n; i++) vector<int>().swap(note[i]);
dfss(x, 0);
for(int i = 1; i <= n; i++) {
for(int j = 1; j < note[i].size(); j++) {
if(note[i][j] != note[i][j - 1]) return;
}
}
cout << x << "\n";
exit(0);
}
void work(int now, int f) {
deep[now] = deep[f] + 1;
if(du[now] > 2) return;
if(du[now] == 1) {
if(maxx > deep[now]) {
maxx = deep[now], a = now;
}
return;
}
for(int i = 0; i < to[now].size(); i++) {
int vj = to[now][i];
if(vj == f) continue;
work(vj, now);
}
}
int main() {
n = read();
for(int i = 1; i < n; i++) {
int vi = read(), vj = read();
to[vi].push_back(vj);
to[vj].push_back(vi);
du[vi]++;
du[vj]++;
}
a = 1;
dfs(1, 0);
maxx = 0;
a = b;
dfs(a, 0);
check(a);
check(b);
check(mid);
maxx = 0x3e3e3e3e;
for(int i = 0; i < to[mid].size(); i++) {
int vj = to[mid][i];
work(vj, mid);
}
check(a);
cout << "-1\n";
return 0;
}
cf1182D Complete Mirror的更多相关文章
- Codeforces 1182D Complete Mirror [树哈希]
Codeforces 中考考完之后第一个AC,纪念一下qwq 思路 简单理解一下题之后就可以发现其实就是要求一个点,使得把它提为根之后整棵树显得非常对称. 很容易想到树哈希来判结构是否相同,而且由于只 ...
- CF1182 D Complete Mirror——思路
题目:http://codeforces.com/contest/1182/problem/D 很好的思路是从度数为1的点和直径来入手. 找一条直径.看看直径的两个端点是否合法. 如果都不合法,那么根 ...
- Codeforces 1182D Complete Mirror 树的重心乱搞 / 树的直径 / 拓扑排序
题意:给你一颗树,问这颗树是否存在一个根,使得对于任意两点,如果它们到根的距离相同,那么它们的度必须相等. 思路1:树的重心乱搞 根据样例发现,树的重心可能是答案,所以我们可以先判断一下树的重心可不可 ...
- Codeforces Round #566 (Div. 2)
Codeforces Round #566 (Div. 2) A Filling Shapes 给定一个 \(3\times n\) 的网格,问使用 这样的占三个格子图形填充满整个网格的方案数 如果 ...
- Codeforces Round #566 (Div. 2)题解
时间\(9.05\)好评 A Filling Shapes 宽度为\(3\),不能横向填 考虑纵向填,长度为\(2\)为一块,填法有两种 如果长度为奇数则显然无解,否则\(2^{n/2}\) B Pl ...
- GreenPlum failover,primary和mirror切换实验 -- 重要
GP failover,primary和mirror切换实验 http://blog.sina.com.cn/s/blog_9869114e0101k1nc.html 一.恢复失败的segment出现 ...
- swjtuoj2433 Magic Mirror
描述 Magic Mirror is an artificial intelligence system developed by TAL AI LAB,It can determine human ...
- 搭建高可用的rabbitmq集群 + Mirror Queue + 使用C#驱动连接
我们知道rabbitmq是一个专业的MQ产品,而且它也是一个严格遵守AMQP协议的玩意,但是要想骚,一定需要拿出高可用的东西出来,这不本篇就跟大家说 一下cluster的概念,rabbitmq是erl ...
- [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
随机推荐
- 数据仓库006 - MySQL 5.6.x - Linux最佳生产环境离线部署
一.离线安装包 文件准备 这里以mysql-5.6.23-linux-glibc2.5-x86_64.tar.gz为例,记一次MySQL 5.6.x 的生产环境离线部署过程.使用SecureCRT连接 ...
- Java Metrics工具介绍
目录 简介 快速入门 Maven配置 MetricRegistry Gauge Meter Counter Histgram Timer Reporter 更多用法 参考资料 简介 Metric是一个 ...
- (二十四)golang--错误处理
在默认情况下,遇到错误时,程序会崩溃: 在发生错误时,我们可以捕获错误,使程序可以继续运行,并抛出错误提示: 错误处理: (1)Go语言追求简洁优雅,所以不支持传统的try catch finally ...
- mybatis批处理数据
批处理数据主要有三种方式: 1.传统jdbc处理 2.mybatis批处理插入 3.使用executortype处理 jdbc 处理 1.通过 for循环插入 main方法如下所示: Co ...
- linux内核参数sysctl.conf,TCP握手ack,洪水攻击syn,超时关闭wait
题记:优化Linux内核sysctl.conf参数来提高服务器并发处理能力 PS:在服务器硬件资源额定有限的情况下,最大的压榨服务器的性能,提高服务器的并发处理能力,是很多运维技术人员思考的问题.要提 ...
- Zabbix触发器和监控项设置时间范围
目录 一.实际业务场景 业务问题 解决办法 二.Zabbix触发器和监控项与用户预警设置时间范围配置流程 一.触发器设置时间范围 二.监控项设置时间范围 三.用户报警设置启用时间 一.实际业务场景 业 ...
- RFC函数的初步使用-同步
1.由于没有外围系统,采用不同SAP不同client之间进行测试. 首先在A-client搭建需要被调用的RFC函数.在A-client里运行SE37创建函数 在属性页签选择“远程启用的模块” 设定i ...
- Appium 自动化测试配置wda的两种方式。
tips:WebDriverAgent是Appium1.6.3以后版本新添加的模块,为了让appium与iPhone(基于xcuitest)设备进行通信而添加的.但是,这个模块在是一个独立的项目,在使 ...
- Error: Opening Robot Framework log failed on mac jenkins
For resolve your problem you must : Connect on your jenkins url (http://[IP]:8080/) Click on Manage ...
- FCC-学习笔记 Missing letters
FCC-学习笔记 Missing letters 1>最近在学习和练习FCC的题目.这个真的比较的好,推荐给大家. 2>中文版的地址:https://www.freecodecamp.c ...