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--变量的作用域
(1)函数内部申明/定义的变量叫局部变量,作用域仅限于函数的内部: (2)函数外部申明/定义的变量叫全局变量,作用域在整个包都有效,如果其首字母为大写,则作用域是整个程序: (3)如果变量是在一个代码 ...
- HTML连载25-通配符选择器&选择器综合练习
一.通配符选择器 作用:给当前页面上所有的标签设置属性 (2)格式: *{属性:值:} (3)注意点:由于通配符选择器是给界面上所有的标签设置属性,因此在设置之前会遍历所有的标签,如果当前界面上的标签 ...
- CompletableFuture1
public class CompletableFutureTest { public static void main(String[] args) throws Exception { test5 ...
- Python requests库的使用(二)
1.请求异常处理 请求异常类型: 请求超时处理(timeout): 实现代码: import requestsfrom requests import exceptions #引入exc ...
- 设计模式之设计原则 C#
成为一名资深架构师首先要懂设计模式,在懂之前,要清楚设计原则,原来我就吃过这个亏,很久以前有人问我设计原则,我是一头茫然,不是只有设计模式吗?且不知设计原则就像是写书法一样,楷体就是方正,竖道有垂露等 ...
- ucoreOS_lab1 实验报告
由于我个人不太懂 AT&T 语法,在完成实验的过程中遇到了相当大的阻碍,甚至有点怀疑人生,我是否心太大了,妄想在短时间内学懂大清的课程.ucoreOS_lab1 这个实验前前后后做到了现在才勉 ...
- 谈谈<? extends T> 和<? super T>理解
项目中遇到<? extends T> 和<? super T> 这两者,来说说自己的理解.首先我们先了解什么是泛型 什么是泛型 泛型是在编译阶段一种防止错误对象输入的机制.编译 ...
- ansible服务部署
1.ansible.cfg配置文件 [defaults] #inventory= /home/op/ansible/testing #sudo_user=root remote_port=9122 r ...
- my.cnf配置文件实用优化
[client] 1.登陆过程自动化(这样做可以让你在命令行登陆的时候免去输入用户名和密码) host="mysql服务器地址" user="用户名" pass ...
- linux (08) nginx入门详解
一. nginx 入门 nginx 入门学习 web服务器软件 windows IIS服务器 linux nginx apache 收费lighthttp 公司的技术栈 收费版技术栈 apache ...