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 ...
随机推荐
- (三十九)golang--反序列化
反序列化:是指将json字符串反序列化成原来的数据类型. import ( "encoding/json" "fmt" ) type monster struc ...
- HTML连载25-通配符选择器&选择器综合练习
一.通配符选择器 作用:给当前页面上所有的标签设置属性 (2)格式: *{属性:值:} (3)注意点:由于通配符选择器是给界面上所有的标签设置属性,因此在设置之前会遍历所有的标签,如果当前界面上的标签 ...
- 转 推荐 33 个 IDEA 最牛配置,写代码太爽了!
本文由 简悦 SimpRead 转码, 原文地址 https://mp.weixin.qq.com/s/neyvJouuG1Rmxn3BwfRXVg 作者:琦彦 blog.csdn.net/fly91 ...
- 云原生生态周报 Vol. 12 | K8s 1.16 API 重大变更
本文作者:源三.临石.张磊.莫源 业界要闻 1. K8s 1.16 将废弃一系列旧的 API 版本 影响面涉及 NetworkPolicy.PodSecurityPolicy.DaemonSet, D ...
- centos6系统资源限制
1.限制用户CPU使用资源 /etc/security/limits.conf #主配置文件 /etc/security/limits.d/ #子模块配置文件 新建一个普通用户,并查看默认资源限制 [ ...
- Linux下java验证码不显示:Could not initialize class sun.awt.X11FontManager
一.问题 javaweb项目,登录的时候有个图片验证码的功能.在Windows本地测试能够正常显示,部署到Linux上就不行了.报错如下: org.springframework.web.util.N ...
- p12证书
http://my.oschina.net/u/1245365/blog/196363
- 剑指:和为S的连续正数序列
题目描述 输入一个正数 s,打印出所有和为 s 的连续正数序列(至少含有两个数). 例如输入 15,由于 1+2+3+4+5=4+5+6=7+8=15,所以结果打印出 3 个连续序列 1-5.4-6 ...
- redo log 重做日志
--------------------------------------------------2015-02-10---------------------------------------- ...
- Linux Tools 之 iostat 工具总结
iostat是Linux中被用来监控系统的I/O设备活动情况的工具,是input/output statistics的缩写.它可以生成三种类型的报告: CPU利用率报告 设备利用率报告 网络文件系统报 ...