CodeForces 1182D
图论的思维题,太秀了,网上答案也不多,我就也来bb吧

总之47个样例姑且是过了,不知道还有没有反例;
会求树的重心和中心了,挺好
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define maxn 100110
vector<int>G[maxn];
vector<int>an;
void insert(int be, int en) {
G[be].push_back(en);
G[en].push_back(be);
} int root;
int n;
int in[maxn];
int de[maxn];
queue<int>que;
int find_root() {
while (!que.empty()) {
int x = que.front();
que.pop();
for (int i = 0; i < G[x].size(); i++){
int p = G[x][i];
in[p]--;
if (in[p] == 1) {
que.push(p);
root = p;
}
}
}
return 0;
} int cnt[maxn];
int flag = 0;
int jude(int x, int fa, int dep) {//这个点搜下去,是否符合条件
if (cnt[dep] == 0) {
cnt[dep] = de[x];
}
else {
if (cnt[dep] != de[x]) {
flag = 1;
return 0;
}
}
for (int i = 0; i < G[x].size(); i++) {
int p = G[x][i];
if (p == fa) continue;
jude(p, x, dep + 1);
}
return 0;
} int find_point(int x,int fa,int dep) {
if (cnt[dep] == 0 && de[x] == 1) {
cnt[dep] = 1;
an.push_back(x);
}
for (int i = 0; i < G[x].size(); i++) {
int p = G[x][i];
if (p == fa) continue;
if (de[p] == 2 || de[p] == 1) {
find_point(p, x, dep + 1);
} }
return 0;
}
int main() {
int be, en;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d %d", &be, &en);
insert(be, en);
de[be]++;
de[en]++;
in[be]++;
in[en]++;
}
if (n == 1 || n == 2) {
printf("1\n");
return 0;
}
for (int i = 1; i <= n; i++) {
if (in[i] == 1) {
in[i] = 0;
que.push(i);
}
}
/*if (n == 1 || n == 2) {
printf("1\n");
return 0;
}*/
find_root();
memset(cnt, 0, sizeof(cnt));
find_point(root, root, 1); memset(cnt, 0, sizeof(cnt));
flag = 0;
jude(root, root, 1); if (!flag) {
printf("%d\n", root);
return 0;
} for (int i = 0; i < an.size(); i++) {
memset(cnt, 0, sizeof(cnt));
flag = 0;
jude(an[i], an[i], 1);
if (!flag) {
printf("%d\n", an[i]);
return 0;
}
}
printf("-1\n");
return 0;
}
CodeForces 1182D的更多相关文章
- Codeforces 1182D Complete Mirror [树哈希]
Codeforces 中考考完之后第一个AC,纪念一下qwq 思路 简单理解一下题之后就可以发现其实就是要求一个点,使得把它提为根之后整棵树显得非常对称. 很容易想到树哈希来判结构是否相同,而且由于只 ...
- Codeforces 1182D Complete Mirror 树的重心乱搞 / 树的直径 / 拓扑排序
题意:给你一颗树,问这颗树是否存在一个根,使得对于任意两点,如果它们到根的距离相同,那么它们的度必须相等. 思路1:树的重心乱搞 根据样例发现,树的重心可能是答案,所以我们可以先判断一下树的重心可不可 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
随机推荐
- ArcGIS下如何提取研究区域
举个例子,如果我用“按位置选择工具”从shitrt图层中提取普查小区在count6中的部分,并将结果输出为shapefile文件cnty6trt,可以这么做: 就OK了
- Gym - 101617D_Jumping Haybales(BFS)
Sample Input 4 2 .### #... .#.. #.#. 3 1 .#. .#. .#. Sample Output 4 -1 题意:给一个n*n的图,每次最多能跳k个格子,只能向南( ...
- python基础之逻辑题(1)
python基础之逻辑题(1) 1.用一行代码实现数值交换? 2.求结果--fromkeys? 3.1,2,3,4,5能组成多少个互不相同且无重复的三位数? 4.有两个字符串列表a和b,每个字符串是逗 ...
- selenium webdriver学习(二)————对浏览器的简单操作(转载JARVI)
selenium webdriver学习(二)————对浏览器的简单操作 博客分类: Selenium-webdriver selenium webdriver对浏览器的简单操作 打开一个测试浏览 ...
- IntelliJ IDEA和Eclipse设置JVM运行参数
打开 IDEA 安装目录,看到有一个 bin 目录,其中有两个 vmoptions 文件,需针对不同的JDK进行配置: 32 位:idea.exe.vmoptions64 位:idea64.exe.v ...
- call_user_func_array() 内置函数
func_get_args()方法是获取当前文件所有的函数参数 fun_num_args()这个方法是获取方法实参个数 call_user_func_array()这个方法是内置函数,可以直接调用函数 ...
- 搭建服务器上的GIT并实现自动同步到站点目录(www)
https://blog.csdn.net/baidu_30000217/article/details/51327289 前言:当我们想要实现几个小伙伴合作开发同一个项目,或者建立一个资源分享平台的 ...
- [转]Spring历史版本变迁和如今的生态帝国
前两篇: 为什么要有Spring? 为什么要有Spring AOP? 前两篇从Web开发史的角度介绍了我们在开发的时候遇到的一个个坑,然后一步步衍生出Spring Ioc和Spring AOP的概念雏 ...
- java StringBuffer 与 StringBuilder
String是不可变类,一旦String对象被创建,包含在对象中的字符序列是不可变的,直到对象被销毁: StringBuffer 与 StringBuilder对象则是可变的! 举例说明这两个的好处: ...
- python基础十四之匿名函数
匿名函数 处理简单问题的简化函数,关键字lambda. # 格式:函数名 = lambda 参数:返回值 anonymity = lambda s: s ** 0.5 print(anonymity( ...