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 ...
随机推荐
- 因为对 Docker 不熟悉建了 N 多个 Nginx
因为对 Docker 不熟悉建了 N 多个 Nginx 一直不停的 docker run nginx 结果出现无数个 nginx. 然后最原来的 nginx 启动不了了. 使用 docker ps - ...
- qt 自定义窗口绘制正弦曲线
circlewidget.h #ifndef CIRCLAWIDGET_H #define CIRCLAWIDGET_H #include <QFrame> #include<QVe ...
- oracle函数 months_between(d1,d2)
[功能]:返回日期d1到日期d2之间的月数. [参数]:d1,d2 日期型 [返回]:数字 如果d1>d2,则返回正数 如果d1<d2,则返回负数 [示例] select sysdate, ...
- hdu 2892 area (圆与多边形交面积)
Problem - 2892 这道题的做法是以圆心为原点,对多边形进行三角剖分.题目描述中,多边形的可能是顺时针或者是逆时针给出,不过在我的做法里,是用有向面积来计算的,和常见的多边形面积的求法类似, ...
- hdu 3662 3D Convex Hull
Problem - 3662 题意很简单,构造三维凸包,求凸包有多少个面. 代码如下: #include <cstdio> #include <iostream> #inclu ...
- hdu 1286 找新朋友 (容斥原理 || 欧拉函数)
Problem - 1286 用容斥原理做的代码: #include <cstdio> #include <iostream> #include <algorithm&g ...
- PHP 试题(1)
1.__FILE__表示什么意思?(5分)文件的完整路径和文件名.如果用在包含文件中,则返回包含文件名.自 PHP 4.0.2 起,__FILE__ 总是包含一个绝对路径,而在此之前的版本有时会包含一 ...
- C#面向对象--命名空间与类库
1.命名空间 在源代码文件开头使用using语句引用 命名空间,就可以直接使用其中的类而不再需要指明其所属的命名空间. .NET Framework使用命名空间来管理所有的类. 类的修饰符: pu ...
- Pytest - 使用介绍
1. 概述 pytest是一个非常成熟的全功能的Python测试框架,主要特点有以下几点: 1.简单灵活,容易上手,文档丰富: 2.支持参数化,可以细粒度地控制要测试的测试用例: 3.能够支持简单的单 ...
- codedecision P1113 同颜色询问 题解 线段树动态开点
题目描述:https://www.cnblogs.com/problems/p/11789930.html 题目链接:http://codedecision.com/problem/1113 这道题目 ...