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 ...
随机推荐
- spingboot项目在windows环境中运行时接收参数及日志中文乱码
1.logback.xml配置 appender中添加 <param name="Encoding" value="UTF-8" /> <co ...
- python基础---字符串常用方法汇总
s3 = '123's2 = ' 's1 = 'This Is \t Cash's='abcdefghijklmnopqrstuvwxyz's4 = "0000000this is stri ...
- spring data jpa使用别名--as
使用jpa进行两表联查时总会有字段名相同,所以需要用别名进行区分: 例子: department表同时包含子级id和父级id: 查询语句为: select d.id,d.name,d.descript ...
- 三级分销会员一次查询出来的SQL语句
SELECT p.id AS partyId, p.parent_id AS parentId, pul.username AS userName, p.city, p.birth_date AS b ...
- qt 中画线时如何设置笔的颜色和填充
在上一次介绍中已经实现了自定义控件,并把Widget 放入了主界面中,画了一个圆,具体可参考“QT 自定义窗口” 下面我们介绍一下如何设置画笔颜色和所画图形的填充颜色. 画笔颜色: void Circ ...
- PHP 网站大流量与高并发的解决方法
php 网站如何应对大流量与高并发呢? 首先,确认服务器硬件是否足够支持当前的流量. 普通的P4服务器一般最多能支持每天10万地理IP,如果访问量比这个还要大,则请配置一台更高性能的专用服务器. 否则 ...
- JS拉平数组
JS拉平数组 有时候会遇到一个数组里边有多个数组的情况(多维数组),然后你想把它拉平使其成为一个一维数组,其实最简单的一个方法就是ES6数组方法Array.prototype.flat.使用方法如下: ...
- lrj word
www.tup.tsinghua.edu.cn/upload/books/yz/055687-01.doc bing搜索UVa437 搜到这个word版本的电子书第9章
- Python--day65--模板语言之tags
Tags for循环 普通for循环 <ul> {% for user in user_list %} <li>{{ user.name }}</li> {% en ...
- 2018-8-10-win10-sdk-是否向下兼容
title author date CreateTime categories win10 sdk 是否向下兼容 lindexi 2018-08-10 19:16:53 +0800 2018-2-13 ...