图论的思维题,太秀了,网上答案也不多,我就也来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. 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 M. Frequent Subsets Problem【状态压缩】

    2017 ACM-ICPC 亚洲区(南宁赛区)网络赛  M. Frequent Subsets Problem 题意:给定N和α还有M个U={1,2,3,...N}的子集,求子集X个数,X满足:X是U ...

  2. UVa 623 大整数乘法

    UVa 623 计算N! n上限为1000自然不能直接算.所以可以开一个数组f[],f[]每一位存N!结果的6位.如果按进制来理解,就是10^6进制: 例如 11!=39916800=11*10!=1 ...

  3. nodeJs学习-07 express、body-parser;链式操作next

    express和body-parser   const express=require('express'); const bodyParser=require('body-parser'); var ...

  4. HDU 5673 Robot【卡特兰数】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5673 题意: 有一个机器人位于坐标原点上.每秒钟机器人都可以向右移到一个单位距离,或者在原地不动.如 ...

  5. Linux下安装MySQL-python

    因为安装过程不断出现错误,备份.系统:linux ubuntupython版本:Python 2.7.3mysql版本:mysql  Ver 14.14 Distrib 5.5.35, for deb ...

  6. SPOJ 8073 The area of the union of circles (圆并入门)

    Sphere Online Judge (SPOJ) - Problem CIRU [求圆并的若干种算法,圆并扩展算法]_AekdyCoin的空间_百度空间 参考AekdyCoin的圆并算法解释,根据 ...

  7. pytorch bert 源码解读

    https://daiwk.github.io/posts/nlp-bert.html 目录 概述 BERT 模型架构 Input Representation Pre-training Tasks ...

  8. ip地址库 与浏览器的关系

    https://zhidao.baidu.com/question/325152705.html 只要手机连接数据上网就会产生ip,只要进入了淘宝,就能查出用户访问记录的. 手机是运营商动态分配的.它 ...

  9. Navicat for MySQL 使用SSH方式链接远程数据库

    第一步:ssh部分: 端口号:22 用户名为:在xshell中用来登录服务器的账号密码 第二步: 端口:3306 账号密码:在MySQL中的登录账号密码

  10. scrapdy部署爬虫项目

    原文:https://blog.csdn.net/JLaiRen/article/details/82902321 scrapyd安装 打开命令行工具输入命令:pip install scrapyd ...