• 可以得到一个结论, 可行的点要么是直径端点, 要么是直径中点, 要么是直径中点引出的链中最短的端点
#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的更多相关文章

  1. Codeforces 1182D Complete Mirror [树哈希]

    Codeforces 中考考完之后第一个AC,纪念一下qwq 思路 简单理解一下题之后就可以发现其实就是要求一个点,使得把它提为根之后整棵树显得非常对称. 很容易想到树哈希来判结构是否相同,而且由于只 ...

  2. CF1182 D Complete Mirror——思路

    题目:http://codeforces.com/contest/1182/problem/D 很好的思路是从度数为1的点和直径来入手. 找一条直径.看看直径的两个端点是否合法. 如果都不合法,那么根 ...

  3. Codeforces 1182D Complete Mirror 树的重心乱搞 / 树的直径 / 拓扑排序

    题意:给你一颗树,问这颗树是否存在一个根,使得对于任意两点,如果它们到根的距离相同,那么它们的度必须相等. 思路1:树的重心乱搞 根据样例发现,树的重心可能是答案,所以我们可以先判断一下树的重心可不可 ...

  4. Codeforces Round #566 (Div. 2)

    Codeforces Round #566 (Div. 2) A Filling Shapes 给定一个 \(3\times n\) 的网格,问使用 这样的占三个格子图形填充满整个网格的方案数 如果 ...

  5. Codeforces Round #566 (Div. 2)题解

    时间\(9.05\)好评 A Filling Shapes 宽度为\(3\),不能横向填 考虑纵向填,长度为\(2\)为一块,填法有两种 如果长度为奇数则显然无解,否则\(2^{n/2}\) B Pl ...

  6. GreenPlum failover,primary和mirror切换实验 -- 重要

    GP failover,primary和mirror切换实验 http://blog.sina.com.cn/s/blog_9869114e0101k1nc.html 一.恢复失败的segment出现 ...

  7. swjtuoj2433 Magic Mirror

    描述 Magic Mirror is an artificial intelligence system developed by TAL AI LAB,It can determine human ...

  8. 搭建高可用的rabbitmq集群 + Mirror Queue + 使用C#驱动连接

    我们知道rabbitmq是一个专业的MQ产品,而且它也是一个严格遵守AMQP协议的玩意,但是要想骚,一定需要拿出高可用的东西出来,这不本篇就跟大家说 一下cluster的概念,rabbitmq是erl ...

  9. [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数

    Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...

随机推荐

  1. HTML连载20-并集选择器&兄弟选择器

    一.并集选择器 1.作用:给所有的选择器选中的标签设置属性. 2.格式: 选择器1,选择器2{ 属性:值: } 3.例如: .abc1,#abc2{ color:red; } .......省略代码. ...

  2. SAS——proc format的其他应用:invalue,picture,default,mult,prefix,noedit,_same_,_error_,other

    一. proc format; invalue $test (default=200) "1"=_same_ "2"="Black" &qu ...

  3. linux 链接mysql并覆盖数据

    --链接mysql 命令格式: mysql -h 主机地址 -u 用户名 -p --按回车输入密码 --在服务器里登录到数据库里 ,使用某个库,gtmc 代表数据库名称 use gtmc --覆盖数据 ...

  4. SQL学习笔记之 数据库基础(一)

    数据库基础 数据库系统的组成:由数据库,数据库管理软件,数据库管理员DBA,支持数据库系统的硬件和软件组成,其中数据库管理员是对数据库进行规划.设计.维护.和监视的专业管理人员,在数据库系统中起着非常 ...

  5. ASP.NET Core 发布到Linux需要注意的地方

    此文持续更新. 读取本地目录的问题, 在windows下,一般会用“\”,但在Linux中用的是“/” 待续..

  6. Postman 调试请求Asp.Net Core3.0 WebApi几种常见的Get/Post/Put/Delete请求

    这里就直接截图了,如下(很简单的操作): 1:Get几种请求 2:Post 3:Put 4:Delete  最后,虽然简单,代码还是给放一下(这里只是抛砖引玉的作用,自己可以根据自身的业务需要来做进一 ...

  7. .Net常见的IOC框架及AOP框架

    IOC框架 Unity:微软patterns&practicest团队开发的IOC依赖注入框架,支持AOP横切关注点. MEF(Managed Extensibility Framework) ...

  8. Linux链接文件——软连接和硬链接

    Linux链接文件——软连接和硬链接 摘要:本文主要介绍了Linux系统中的链接文件. 文件系统 在Linux系统中,将文件分为两个部分:用户数据和元数据. 元数据(inode) 元数据即文件的索引节 ...

  9. npm和cnpm命令后无响应

    问题: 1.把前端环境配制完毕之后,打开项目,输入cnpm install之后,光标一直在另起一行的位置闪,但是丝毫没有在安装的迹象. 2.打开cmd,在窗体中输入node -v 可以显示版本,但是输 ...

  10. vue cli 3.0快速创建项目

    本地安装vue-cli 前置条件 更新npm到最新版本 命令行运行: npm install -g npmnpm就自动为我们更新到最新版本 淘宝npm镜像使用方法 npm config set reg ...