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 ...
随机推荐
- poj 1655 Balancing Act 求树的重心【树形dp】
poj 1655 Balancing Act 题意:求树的重心且编号数最小 一棵树的重心是指一个结点u,去掉它后剩下的子树结点数最少. (图片来源: PatrickZhou 感谢博主) 看上面的图就好 ...
- Creating a Pulsing Circle Animation
原文 https://www.kirupa.com/animations/creating_pulsing_circle_animation.htm Outside of transitions th ...
- ros自定义消息
ros自定义消息可以根据自身项目需求定义和封装想要的数据类型和数据结构.具体可以参考维基百科关于ros自定义消息部分 这里我只是记录自定义消息的要点部分: 1.首先要在工作空间下功能包中创建一个msg ...
- 阿里云POLARDB如何助力轻松筹打造5亿用户信赖的大病筹款平台?
轻松筹首创了“大病救助”模式,帮助了众多病患在第一时间解決了医疗资金等问题,为了从源头解决了医疗资金问题.而在轻松筹这样全球5.5亿用户信赖的大病筹款平台的背后,是日益增长的各种数据.面对这样数据量所 ...
- jmter正则表达式提取器
1.若返回的body内容为空,仅有 header值,则: \s代表为空 2.使用Debug来调试
- Java发送邮件的工具类
package com.csice.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNot ...
- 阿里云DataWorks正式推出Stream Studio:为用户提供大数据实时计算的数据中台
5月15日 阿里云DataWorks正式推出Stream Studio,正式为用户提供大数据的实时计算能力,同时标志着DataWorks成为离线.实时双计算领域的数据中台. 据介绍,Stream St ...
- AFNetworkingErrorDomain 错误
AFNetworking and POST Request I'm getting this response in error.userInfo while making a POST reques ...
- 传说中Python最难理解的点|看这完篇就够了(装饰器)
https://mp.weixin.qq.com/s/B6pEZLrayqzJfMtLqiAfpQ 1.什么是装饰器 网上有人是这么评价装饰器的,我觉得写的很有趣,比喻的很形象 每个人都有的内裤主要是 ...
- laravel 增加不存在数据库的字段
有时候您可能想要增加不存在数据库字段的属性数据.这时候只要定义一个获取器即可: public function getIsAdminAttribute() { return $this->att ...