一、题目描述

The N cities of Estiah are connected by N-1 roads. The roads are built in a way that it’s always possible to travel between any two cities.

Now the king of Estiah wants to pair adjacent cities into defending units. Two cities are adjacent if they are connected directly by a road. Each defending unit consists of exactly two cities, and each city can never be paired into two different defending units.

What the king wants to know is if it’s possible to have all the cities paired into defending units. Can you help him ?

二、输入

The input consists of several test cases.

The first line of the input is an positive integer indicating the number of test cases following.

Each test case starts with an positive integer N (1<=N<=10000) , which is the number of cities. Cities are numbered from 1 to N.

The next N-1 lines each contains two positive integer A and B, indicating that there is a road connecting city A and city B.

三、输出

For each test case, output a line containing “Yes” if there is a way to pair all the cities, or “No” otherwise.

例如:

输入:

2

6

3 4

6 5

4 6

2 1

6 2

6

3 4

2 1

4 6

4 2

6 5

输出:

Yes

Yes

四、解题思路

题中愿意是:有N个点,n-1条边使n个点组成连通图。从题中能够看出,这是一个最小生成树。题中要求,没相连的两个城市可以组成一组,而且每个城市只能归于一组。

思路:

从图中找出其中一个度为1的点,删掉该点与和该点相连的另外一个点,已经与他们相连的边。不断重复该步骤,直到不存在度为1的点,如果已经全部配对完,表示能配对,否则不能配对。

步骤:

1、找出图中其中一个度为1的点,如下图点“1”

2、找到与点1相连的那个点,点“2”

3、删掉与点“2”相连的所有的边

4、找出图中其中一个度为1的点,如下图点“3”

5、找到与点3相连的那个点,点“4”

6、删掉与点“4”相连的所有的边

7、找出图中其中一个度为1的点,如下图点“5”

8、找到与点5相连的那个点,点“6”

9、删掉与点“6”相连的所有的边

因为已经配对完所有的点,所以能成功配对

五、代码

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int times;
  6. cin >> times;
  7. while(times--)
  8. {
  9. int pointCount; //城市的个数
  10. int delPointNum = 0; //已经能配对好的城市个数
  11. cin >> pointCount;
  12. int edgeArray[pointCount][2]; //保存相连的两个城市
  13. int pointLigature[pointCount]; //连接每个城市的度
  14. for(int i = 0; i < pointCount; i++)
  15. {
  16. pointLigature[i] = 0;
  17. }
  18. for(int i = 0; i < pointCount - 1; i++)
  19. {
  20. int startPoint, endPoint;
  21. cin >> startPoint >> endPoint;
  22. edgeArray[i][0] = startPoint;
  23. edgeArray[i][1] = endPoint;
  24. pointLigature[startPoint - 1]++;
  25. pointLigature[endPoint - 1]++;
  26. }
  27. if(pointCount % 2 == 1) {cout << "No" << endl;continue;} //如果城市的个数是奇数个直接判断不能配对
  28. int maxDelTime = pointCount;
  29. while(maxDelTime--) //如果存在着度为1的顶点(城市),每次循环都会配对(删掉)一对
  30. {
  31. int delPoint = 0; //找出度为1的顶点
  32. for(; delPoint< pointCount; delPoint++)
  33. {
  34. if(pointLigature[delPoint] == 1)
  35. {
  36. delPointNum += 2;
  37. break;
  38. }
  39. }
  40. for(int i = 0; i < pointCount - 1; i++) //找出与度为1的顶点相连的顶点
  41. {
  42. if(edgeArray[i][0] == delPoint + 1) {delPoint = edgeArray[i][1]; break;}
  43. if(edgeArray[i][1] == delPoint + 1) {delPoint = edgeArray[i][0]; break;}
  44. }
  45. for(int i = 0; i < pointCount - 1; i++) //以与度为1的顶点相连的顶点为中心,删掉与它相连的路径
  46. {
  47. if(edgeArray[i][0] == delPoint || edgeArray[i][1] == delPoint)
  48. {
  49. int point;
  50. point = edgeArray[i][0] - 1;
  51. pointLigature[point]--;
  52. point = edgeArray[i][1] - 1;
  53. pointLigature[point]--;
  54. edgeArray[i][0] = 0;
  55. edgeArray[i][1] = 0;
  56. }
  57. }
  58. }
  59. if(delPointNum >= pointCount) cout << "Yes" << endl;
  60. else cout << "No" << endl;
  61. }
  62. return 0;
  63. }

<Sicily>Pair的更多相关文章

  1. c++ pair 使用

    1. 包含头文件: #include <utility> 2. pair 的操作: pair<T1,T2> p; pair<T1,T2> p(v1,v2); pai ...

  2. 论Pair的重要性

    这些天我在用React和D3做图表,从已经实现的图表里复制了一些坐标轴的代码,发现坐标轴上的n个点里,只有第一个点下面能渲染出文字提示,其余点下面都无法渲染出文字. 和组里的FL一起百思不得其解好几天 ...

  3. 2016 ACM/ICPC Asia Regional Dalian Online 1010 Weak Pair dfs序+分块

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submissio ...

  4. pair的使用

    #include<iostream> #include<cmath> #include<cstdio> #include<algorithm> #inc ...

  5. 【C++】pair

    STL的pair,有两个值,可以是不同的类型. template <class T1, class T2> struct pair; 注意,pair在头文件utility中,不要inclu ...

  6. hackerrank Similar Pair

    传送门 Problem Statement You are given a tree where each node is labeled from 1 to n. How many similar ...

  7. uva12546. LCM Pair Sum

    uva12546. LCM Pair Sum One of your friends desperately needs your help. He is working with a secret ...

  8. C++标准库 -- pair

    头文件:<utility> 可访问属性: first 第一个值 second 第二个值 可访问方法: swap(pair) 和另外一个pair交换值 其他相关方法: make_pair(v ...

  9. 2016 大连网赛---Weak Pair(dfs+树状数组)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5877 Problem Description You are given a rooted ...

  10. C++学习之Pair

    C++学习之Pair Pair类型概述 pair是一种模板类型,其中包含两个数据值,两个数据的类型可以不同,基本的定义如下: pair<int, string> a; 表示a中有两个类型, ...

随机推荐

  1. CodeForces ---596B--Wilbur and Array(贪心模拟)

    Wilbur and Array Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Su ...

  2. ES6 | 关于class类 继承总结

    子类必须在constructor方法中调用super方法,否则新建实例时会报错.这是因为子类没有自己的this对象,而是继承父类的this对象,然后对其进行加工.如果不调用super方法,子类就得不到 ...

  3. 转js调优

    随着网络的发展,网速和机器速度的提高,越来越多的网站用到了丰富客户端技术.而现在Ajax则是最为流行的一种方式.javascript是一种解释型 语言,所以能无法达到和C/Java之类的水平,限制了它 ...

  4. PostgreSQL+PostGIS

    PostGIS简介 PostGIS是对象关系型数据库系统PostgreSQL的一个扩展,PostGIS提供如下空间信息服务功能:空间对象.空间索引.空间操作函数和空间操作符.同时,PostGIS遵循O ...

  5. No mapping found for HTTP request with URI [/spring_liu/hello.do] in DispatcherServlet with name 'SpringMVC'

    控制台一直报No mapping found for HTTP request with URI [/spring_liu/hello.do] in DispatcherServlet with na ...

  6. ES6学习笔记(二十一)编程风格

    本章探讨如何将 ES6 的新语法,运用到编码实践之中,与传统的 JavaScript 语法结合在一起,写出合理的.易于阅读和维护的代码. 1.块级作用域 (1)let 取代 var ES6 提出了两个 ...

  7. vue项目的webpack4.X配置

    这两天摆弄webpack,躺过很多坑,直到今天看了一位博主的文章才得以解决.他对配置中的各个部分做说明. 下面的配置99.9%抄自博主: https://www.cnblogs.com/nianyif ...

  8. java实现websocket 终极指南

    大概思路:  首先用户登陆  获取用户信息存储到httpsession中,然后客户端链接服务端websocket,首先HandshakeInterceptor这个拦截器会拦截请求 调用 beforeH ...

  9. Rancher介绍安装以及对docker的管理

    原文:Rancher介绍安装以及对docker的管理 一.简介 Rancher是一个开源的企业级全栈化容器部署及管理平台.Rancher为容器提供一揽子基础架构服务:CNI兼容的网络服务.存储服务.主 ...

  10. Java 二进制,八进制,十进制,十六进制转换

    A.十进制转换其他 十进制转成二进制  Integer.toBinaryString(int i) 十进制转成八进制  Integer.toOctalString(int i) 十进制转成十六进制:  ...