图论的思维题,太秀了,网上答案也不多,我就也来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的更多相关文章

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

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

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

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

  3. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  4. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  5. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  6. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  7. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  8. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  9. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

随机推荐

  1. JSTL的时间格式化

    <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> 开头第一句必须上 ...

  2. SharpDX初学者教程第3部分:初始化DirectX

    原文 http://www.johanfalk.eu/blog/sharpdx-beginners-tutorial-part-3-initializing-directx 在这部分中,我们将初始化D ...

  3. @atcoder - AGC040C@ Neither AB nor BA

    目录 @description@ @solution@ @accepted code@ @detail@ @description@ 给定偶数 N,求由 'A', 'B', 'C' 三种字符组成的字符 ...

  4. @NOIP2018 - D2T2@ 填数游戏

    目录 @题目描述@ @题解@ @代码@ @题目描述@ 小 D 特别喜欢玩游戏.这一天,他在玩一款填数游戏. 这个填数游戏的棋盘是一个 n×m 的矩形表格.玩家需要在表格的每个格子中填入一个数字(数字 ...

  5. CDN WAF功能开放公测 提升网络应用安全性能

    阿里云CDN WAF功能,是指CDN融合了云盾Web应用防火墙(Web Application Firewall,简称 WAF)能力,在CDN节点上提供安全防护的功能,该功能目前已经开放公测. WAF ...

  6. 模板—BSGS

    #include<iostream> #include<cstdio> #include<cmath> #include<map> #define LL ...

  7. JVM问题诊断

    https://gitbook.cn/books/5d2d822f175a450263e945f9/index.html

  8. Facebook 发布深度学习工具包 PyTorch Hub,让论文复现变得更容易

    近日,PyTorch 社区发布了一个深度学习工具包 PyTorchHub, 帮助机器学习工作者更快实现重要论文的复现工作.PyTorchHub 由一个预训练模型仓库组成,专门用于提高研究工作的复现性以 ...

  9. @COCI 2016/2017 Round 3@ Meksikanac

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 在平面直角坐标系中,给定一个左下角为 (0, 0),右上角为 ( ...

  10. <%@ include file=""%>与<jsp:include page=""/>两种方式的作用以及传值

      一:使用    1.include指令: 1 <%@include file="文件的绝对路径或者相对路径"%> file属性是必填的(绝对或相对路径),但它不支持 ...