<Sicily>Pair
一、题目描述
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”相连的所有的边
因为已经配对完所有的点,所以能成功配对
五、代码
#include<iostream>
using namespace std;
int main()
{
int times;
cin >> times;
while(times--)
{
int pointCount; //城市的个数
int delPointNum = 0; //已经能配对好的城市个数
cin >> pointCount;
int edgeArray[pointCount][2]; //保存相连的两个城市
int pointLigature[pointCount]; //连接每个城市的度
for(int i = 0; i < pointCount; i++)
{
pointLigature[i] = 0;
}
for(int i = 0; i < pointCount - 1; i++)
{
int startPoint, endPoint;
cin >> startPoint >> endPoint;
edgeArray[i][0] = startPoint;
edgeArray[i][1] = endPoint;
pointLigature[startPoint - 1]++;
pointLigature[endPoint - 1]++;
}
if(pointCount % 2 == 1) {cout << "No" << endl;continue;} //如果城市的个数是奇数个直接判断不能配对
int maxDelTime = pointCount;
while(maxDelTime--) //如果存在着度为1的顶点(城市),每次循环都会配对(删掉)一对
{
int delPoint = 0; //找出度为1的顶点
for(; delPoint< pointCount; delPoint++)
{
if(pointLigature[delPoint] == 1)
{
delPointNum += 2;
break;
}
}
for(int i = 0; i < pointCount - 1; i++) //找出与度为1的顶点相连的顶点
{
if(edgeArray[i][0] == delPoint + 1) {delPoint = edgeArray[i][1]; break;}
if(edgeArray[i][1] == delPoint + 1) {delPoint = edgeArray[i][0]; break;}
}
for(int i = 0; i < pointCount - 1; i++) //以与度为1的顶点相连的顶点为中心,删掉与它相连的路径
{
if(edgeArray[i][0] == delPoint || edgeArray[i][1] == delPoint)
{
int point;
point = edgeArray[i][0] - 1;
pointLigature[point]--;
point = edgeArray[i][1] - 1;
pointLigature[point]--;
edgeArray[i][0] = 0;
edgeArray[i][1] = 0;
}
}
}
if(delPointNum >= pointCount) cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}
<Sicily>Pair的更多相关文章
- c++ pair 使用
1. 包含头文件: #include <utility> 2. pair 的操作: pair<T1,T2> p; pair<T1,T2> p(v1,v2); pai ...
- 论Pair的重要性
这些天我在用React和D3做图表,从已经实现的图表里复制了一些坐标轴的代码,发现坐标轴上的n个点里,只有第一个点下面能渲染出文字提示,其余点下面都无法渲染出文字. 和组里的FL一起百思不得其解好几天 ...
- 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 ...
- pair的使用
#include<iostream> #include<cmath> #include<cstdio> #include<algorithm> #inc ...
- 【C++】pair
STL的pair,有两个值,可以是不同的类型. template <class T1, class T2> struct pair; 注意,pair在头文件utility中,不要inclu ...
- hackerrank Similar Pair
传送门 Problem Statement You are given a tree where each node is labeled from 1 to n. How many similar ...
- uva12546. LCM Pair Sum
uva12546. LCM Pair Sum One of your friends desperately needs your help. He is working with a secret ...
- C++标准库 -- pair
头文件:<utility> 可访问属性: first 第一个值 second 第二个值 可访问方法: swap(pair) 和另外一个pair交换值 其他相关方法: make_pair(v ...
- 2016 大连网赛---Weak Pair(dfs+树状数组)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5877 Problem Description You are given a rooted ...
- C++学习之Pair
C++学习之Pair Pair类型概述 pair是一种模板类型,其中包含两个数据值,两个数据的类型可以不同,基本的定义如下: pair<int, string> a; 表示a中有两个类型, ...
随机推荐
- Oracle 12c agent install for windows
在Oracle EM12c 中部署agent的方法分两种,一种是通过EM12c的控制台通过ssh直接把agent"推送"安装到被管理端.这样的方法在linux平台的OMS和被管理端 ...
- ubuntu中taglist和ctags安装,简单明了
1.使用命令安装ctags: sudo apt-get install ctags 2.安装taglist 下载: http://vim.sourceforge.net/scripts/downloa ...
- 剪切具有CornerRadius的RectangleGeometry(可能在Ripple中用到)
剪切具有CornerRadius的RectangleGeometry(可能在Ripple中用到) 1.新建Converter public class BorderClipConverter : IM ...
- 极客时间 mysql实战45讲下载读 08讲事务到底是隔离的还是不隔离的 笔记
笔记体会: 1.innodb支持RC和RR隔离级别实现是用的一致性视图(consistent read view) 2.事务在启动时会拍一个快照,这个快照是基于整个库的.基于整个库的意思就是说一个事务 ...
- windows共享如何重新登录,或用另外的用户登录
使用net use * /del 可以结束已有的所有连接,或net use \\192.168.1.10 /del可以结束指定连接.比如想重新登录共享的话,就用这个命令结束原来的连接,就可以重新登录 ...
- crontab中使用sudo命令的注意
在使用crontab执行非root用户定时任务时,有时候shell脚本里需要用到sudo以获得root权限: 如: VIP_CARD=eth0 VIP_ADDR=192.168.4.119 NETMA ...
- SpringBoot学习笔记(4)----SpringBoot中freemarker、thymeleaf的使用
1. freemarker引擎的使用 如果你使用的是idea或者eclipse中安装了sts插件,那么在新建项目时就可以直接指定试图模板 如图: 勾选freeMarker,此时springboot项目 ...
- Redis散杂记
Redis是一款很火的KV模式的内存数据库,与众不同的特点: 1.数据存储在内存 内存的读取速度仅次于CPU的寄存器.各等级缓存,“英雄”自动敏捷属性,特点就是快.高效.因此不需要类似存储磁盘的数据库 ...
- 30 个实例详解 ,让运维彻底搞清TOP 命令!
Linux中的top命令显示系统上正在运行的进程.它是系统管理员最重要的工具之一.被广泛用于监视服务器的负载.在本篇中,我们会探索top命令的细节.top命令是一个交互命令.在运行top的时候还可以运 ...
- [置顶]
Docker学习总结(7)——云端基于Docker的微服务与持续交付实践
本文根据[2016 全球运维大会•深圳站]现场演讲嘉宾分享内容整理而成 讲师简介 易立 毕业于北京大学,获得学士学位和硕士学位:目前负责阿里云容器技术相关的产品的研发工作. 加入阿里之前,曾在IBM中 ...