一.NYOJ42 能否一笔画
描述
zyc从小就比较喜欢玩一些小游戏,其中就包括画一笔画,他想请你帮他写一个程序,判断一个图是否能够用一笔画下来。
规定,所有的边都只能画一次,不能重复画。
输入
第一行只有一个正整数N(N<=10)表示测试数据的组数。 每组测试数据的第一行有两个正整数P,Q(P<=1000,Q<=2000),分别表示这个画中有多少个顶点和多少条连线。(点的编号从1到P) 随后的Q行,每行有两个正整数A,B(0 < A,B < P),表示编号为A和B的两点之间有连线。
输出
如果存在符合条件的连线,则输出"Yes", 如果不存在符合条件的连线,输出"No"。
样例输入
2
4 3
1 2
1 3
1 4
4 5
1 2
2 3
1 3
1 4
3 4
样例输出
No
Yes
代码:(并查集)
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,father[1005],du[1005];
void init()
{
for(int i=0;i<=n;i++)
father[i]=i; //父节点初始化
}
int find(int x)
{
if(father[x]==x)return x;
else
{
father[x]= find (father[x]); //加括号!!!
return father[x]; //找祖先节点 压缩(不记录路径)
}
}
int unite(int x,int y)
{
int c=find(x);
int d=find(y);
if(c!=d)
father[d]=c; //y的祖先节点的父节点是x的祖先节点
return 0;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(du,0,sizeof(du));
scanf("%d%d",&n,&m);
init();
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
du[x]++;
du[y]++;
unite(x,y);
}
int root=find(1),cnt=0,d=0;
for(int i=1;i<=n;i++)
{
if(find(i)!=root)///联通块个数-1
cnt++;
if(du[i]%2)///奇点个数
d++;
}
if(!cnt&&(d==0||d==2))
printf("Yes\n");
else
printf("No\n");
}
}
二.HDU3018 最少笔画数
Ant Country consist of N towns.There are M roads connecting the towns.
Ant Tony,together with his friends,wants to go through every part of the country.
They intend to visit every road , and every road must be visited for exact one time.However,it may be a mission impossible for only one group of people.So they are trying to divide all the people into several groups,and each may start at different town.Now tony wants to know what is the least groups of ants that needs to form to achieve their goal.
Input
Input contains multiple cases.Test cases are separated by several blank lines. Each test case starts with two integer N(1<=N<=100000),M(0<=M<=200000),indicating that there are N towns and M roads in Ant Country.Followed by M lines,each line contains two integers a,b,(1<=a,b<=N) indicating that there is a road connecting town a and town b.No two roads will be the same,and there is no road connecting the same town.
Output
For each test case ,output the least groups that needs to form to achieve their goal.
Sample Input
3 3
1 2
2 3
1 3
Sample Output
1
2
Hint
New ~~~ Notice: if there are no road connecting one town ,tony may forget about the town. In sample 1,tony and his friends just form one group,they can start at either town 1,2,or 3. In sample 2,tony and his friends must form two group.
分析:
注意此题求的是最少笔画数,先求出所有的联通块,每个连通块的最少笔画数相加
每个连通块的笔数:奇点的个数+1 / 2.0,如果奇点的个数为0,则笔数为1.
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<cmath>
using namespace std;
int n,m,father[100005],du[100005];
void init()
{
for(int i=0;i<=n;i++)
{father[i]=i;du[i]=0;} //父节点初始化
}
int find(int x)
{
if(father[x]==x)return x;
else
{
father[x]= find (father[x]); //加括号!!!
return father[x]; //找祖先节点 压缩(不记录路径)
}
}
int unite(int x,int y)
{
int c=find(x);
int d=find(y);
if(c!=d)
father[d]=c; //y的祖先节点的父节点是x的祖先节点
return 0;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
du[x]++;
du[y]++;
unite(x,y);
}
map<int,int>part;
for(int i=1;i<=n;i++)
{
int root=find(i);
if(!part.count(root)&&du[root]!=0)///根节点属于新的联通块,不是单独的点就放入part中
{
part[root]=0;
}
if(du[i]%2)///是奇点,那么联通块对应值++
part[root]++;
}
int ans=0;
map<int,int>::iterator it;
for(it = part.begin(); it !=part.end(); it++)
{
int p=it->second;
if(p==0)ans++;
else ans+=(p+1)/2;
}
printf("%d\n",ans);
}
}