图论的思维题,太秀了,网上答案也不多,我就也来bb吧

总之47个样例姑且是过了,不知道还有没有反例;

会求树的重心和中心了,挺好

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<vector>
  6. #include<queue>
  7. using namespace std;
  8. #define maxn 100110
  9. vector<int>G[maxn];
  10. vector<int>an;
  11. void insert(int be, int en) {
  12. G[be].push_back(en);
  13. G[en].push_back(be);
  14. }
  15.  
  16. int root;
  17. int n;
  18. int in[maxn];
  19. int de[maxn];
  20. queue<int>que;
  21. int find_root() {
  22. while (!que.empty()) {
  23. int x = que.front();
  24. que.pop();
  25. for (int i = 0; i < G[x].size(); i++){
  26. int p = G[x][i];
  27. in[p]--;
  28. if (in[p] == 1) {
  29. que.push(p);
  30. root = p;
  31. }
  32. }
  33. }
  34. return 0;
  35. }
  36.  
  37. int cnt[maxn];
  38. int flag = 0;
  39. int jude(int x, int fa, int dep) {//这个点搜下去,是否符合条件
  40. if (cnt[dep] == 0) {
  41. cnt[dep] = de[x];
  42. }
  43. else {
  44. if (cnt[dep] != de[x]) {
  45. flag = 1;
  46. return 0;
  47. }
  48. }
  49. for (int i = 0; i < G[x].size(); i++) {
  50. int p = G[x][i];
  51. if (p == fa) continue;
  52. jude(p, x, dep + 1);
  53. }
  54. return 0;
  55. }
  56.  
  57. int find_point(int x,int fa,int dep) {
  58. if (cnt[dep] == 0 && de[x] == 1) {
  59. cnt[dep] = 1;
  60. an.push_back(x);
  61. }
  62. for (int i = 0; i < G[x].size(); i++) {
  63. int p = G[x][i];
  64. if (p == fa) continue;
  65. if (de[p] == 2 || de[p] == 1) {
  66. find_point(p, x, dep + 1);
  67. }
  68.  
  69. }
  70. return 0;
  71. }
  72. int main() {
  73. int be, en;
  74. scanf("%d", &n);
  75. for (int i = 1; i < n; i++) {
  76. scanf("%d %d", &be, &en);
  77. insert(be, en);
  78. de[be]++;
  79. de[en]++;
  80. in[be]++;
  81. in[en]++;
  82. }
  83. if (n == 1 || n == 2) {
  84. printf("1\n");
  85. return 0;
  86. }
  87. for (int i = 1; i <= n; i++) {
  88. if (in[i] == 1) {
  89. in[i] = 0;
  90. que.push(i);
  91. }
  92. }
  93. /*if (n == 1 || n == 2) {
  94. printf("1\n");
  95. return 0;
  96. }*/
  97. find_root();
  98. memset(cnt, 0, sizeof(cnt));
  99. find_point(root, root, 1);
  100.  
  101. memset(cnt, 0, sizeof(cnt));
  102. flag = 0;
  103. jude(root, root, 1);
  104.  
  105. if (!flag) {
  106. printf("%d\n", root);
  107. return 0;
  108. }
  109.  
  110. for (int i = 0; i < an.size(); i++) {
  111. memset(cnt, 0, sizeof(cnt));
  112. flag = 0;
  113. jude(an[i], an[i], 1);
  114. if (!flag) {
  115. printf("%d\n", an[i]);
  116. return 0;
  117. }
  118. }
  119. printf("-1\n");
  120. return 0;
  121. }

  

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. 因为对 Docker 不熟悉建了 N 多个 Nginx

    因为对 Docker 不熟悉建了 N 多个 Nginx 一直不停的 docker run nginx 结果出现无数个 nginx. 然后最原来的 nginx 启动不了了. 使用 docker ps - ...

  2. qt 自定义窗口绘制正弦曲线

    circlewidget.h #ifndef CIRCLAWIDGET_H #define CIRCLAWIDGET_H #include <QFrame> #include<QVe ...

  3. oracle函数 months_between(d1,d2)

    [功能]:返回日期d1到日期d2之间的月数. [参数]:d1,d2 日期型 [返回]:数字 如果d1>d2,则返回正数 如果d1<d2,则返回负数 [示例] select sysdate, ...

  4. hdu 2892 area (圆与多边形交面积)

    Problem - 2892 这道题的做法是以圆心为原点,对多边形进行三角剖分.题目描述中,多边形的可能是顺时针或者是逆时针给出,不过在我的做法里,是用有向面积来计算的,和常见的多边形面积的求法类似, ...

  5. hdu 3662 3D Convex Hull

    Problem - 3662 题意很简单,构造三维凸包,求凸包有多少个面. 代码如下: #include <cstdio> #include <iostream> #inclu ...

  6. hdu 1286 找新朋友 (容斥原理 || 欧拉函数)

    Problem - 1286 用容斥原理做的代码: #include <cstdio> #include <iostream> #include <algorithm&g ...

  7. PHP 试题(1)

    1.__FILE__表示什么意思?(5分)文件的完整路径和文件名.如果用在包含文件中,则返回包含文件名.自 PHP 4.0.2 起,__FILE__ 总是包含一个绝对路径,而在此之前的版本有时会包含一 ...

  8. C#面向对象--命名空间与类库

    1.命名空间 在源代码文件开头使用using语句引用 命名空间,就可以直接使用其中的类而不再需要指明其所属的命名空间. .NET Framework使用命名空间来管理所有的类. 类的修饰符:   pu ...

  9. Pytest - 使用介绍

    1. 概述 pytest是一个非常成熟的全功能的Python测试框架,主要特点有以下几点: 1.简单灵活,容易上手,文档丰富: 2.支持参数化,可以细粒度地控制要测试的测试用例: 3.能够支持简单的单 ...

  10. codedecision P1113 同颜色询问 题解 线段树动态开点

    题目描述:https://www.cnblogs.com/problems/p/11789930.html 题目链接:http://codedecision.com/problem/1113 这道题目 ...