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 ...
随机推荐
- windows server 2008配置多用户远程连接
打开开始菜单->管理工具->远程桌面服务->远程桌面会话主机配置 右键限制每个用户只能进行一个会话->常规->勾掉限制每个与用户只能进行一个会话 右键远程桌面授权模式-& ...
- 解决 ubuntu 开机卡死在输入密码界面 && 键盘鼠标失灵!!
近期不知安装了什么package,导致 ubuntu 开机后键盘鼠标一直没法用,刚开始以为是 ubuntu 桌面环境崩溃了,后来发现系统能显示连接到网络.时间也在运行,那应该就是键盘鼠标失灵了. 网上 ...
- pip 设置阿里云源
在~/.pip/pip.conf文件中添加或修改 mkdir ~/.pip [global] index-url = http://mirrors.aliyun.com/pypi/simple/ [i ...
- python I/O复用
select是阻塞式的方法
- Topshelf + QuartzNet 实现挂载在 WIndows Services 中的定时任务
直接贴代码了: 首先我们可以把所有的 Job 放到一个单独的 DLL 中,好处是可以共享这些业务 Job.比如我们新建一个 QuartzNetDemo.WinService.Jobs 的类库. 然后, ...
- Django学习笔记(15)——中间件
当Django处理一个Request的过程是首先通过中间件,然后再通过默认的URL方式进行的.我们可以在Middleware这个地方把所有Request拦截住,用我们自己的方式完成处理以后直接返回Re ...
- KVM学习笔记--静态迁移
.静态迁移过程如下 (1)确定虚拟机关闭状态 (2)准备迁移oeltest02虚拟机,查看该虚拟机配置的磁盘文件 (3)导入虚拟机配置文件 [root@node1~]# virsh dumpxml o ...
- SAP PI开发手册-ERP发布服务供外围系统调用(RFC类型)
1转自:https://www.cnblogs.com/fanjb/p/10677018.html 8年进入国网项目后陆陆续续做了一些接口,按实现方法去分有RFC和代理类sproxy类型,按服务提供方 ...
- 安装Yapi时,出现json-schema-faker模块找不到问题
今天换了一台机器按以前的方式安装Yapi工具时,竟然出现了错误. 一 安装yapi时,出现了下面的报错,一开始以为没安装json-schema-faker模块.后面通过找资料,发现是权限问题, 使用c ...
- spark2.4.2 源码编译
基于Maven的构建是Apache Spark的参考构建.使用Maven构建Spark需要Maven 3.5.4和Java 8.请注意,从Spark 2.2.0开始,对Java 7的支持已被删除. 包 ...