ZOJ 3321 Circle
2015-04-05 09:16
302 查看
I - Circle
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld
& %llu
Submit Status Practice ZOJ
3321
Description
Your task is so easy. I will give you an undirected graph, and you just need to tell me whether the graph is just a circle. A cycle is three or more nodes V1, V2, V3,
... Vk, such that there are edges between V1 and V2, V2 and V3, ... Vk and V1,
with no other extra edges. The graph will not contain self-loop. Furthermore, there is at most one edge between two nodes.
Input
There are multiple cases (no more than 10).
The first line contains two integers n and m, which indicate the number of nodes and the number of edges (1 < n < 10, 1 <= m <
20).
Following are m lines, each contains two integers x and y (1 <= x, y <= n, x != y),
which means there is an edge between node x and nodey.
There is a blank line between cases.
Output
If the graph is just a circle, output "YES", otherwise output "NO".
Sample Input
Sample Output
给你一幅无向图,让你判断是不是圆。。
成圆条件有两个。
第一:每一个点的入度只能为2.
第二:所有点属于一个集合。
上代码;
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
#include <iostream>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <time.h>
#include <stdlib.h>
int p[26];
int v[26];
int find(int x)
{
if(p[x]!=x)
p[x]=find(p[x]);
return p[x];
}
int hebing(int x,int y)
{
return p[x]=y;
}
int main()
{
int n,m,i,j,a,b;
while(cin>>n>>m)
{
memset(v,0,sizeof(v));
for(i=1; i<=25; i++)
p[i]=i;
while(m--)
{
cin>>a>>b;
v[a]++; //入度
v[b]++;
a=find(a);
b=find(b);
if(a!=b)
hebing(a,b); //合并、
}
for(i=1; i<=n; i++)
{
if(p[1]!=p[i]|| v[i]!=2)
break;
}
// cout<<i<<endl;
if(i==n+1)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}
I - Circle
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld
& %llu
Submit Status Practice ZOJ
3321
Description
Your task is so easy. I will give you an undirected graph, and you just need to tell me whether the graph is just a circle. A cycle is three or more nodes V1, V2, V3,
... Vk, such that there are edges between V1 and V2, V2 and V3, ... Vk and V1,
with no other extra edges. The graph will not contain self-loop. Furthermore, there is at most one edge between two nodes.
Input
There are multiple cases (no more than 10).
The first line contains two integers n and m, which indicate the number of nodes and the number of edges (1 < n < 10, 1 <= m <
20).
Following are m lines, each contains two integers x and y (1 <= x, y <= n, x != y),
which means there is an edge between node x and nodey.
There is a blank line between cases.
Output
If the graph is just a circle, output "YES", otherwise output "NO".
Sample Input
3 3 1 2 2 3 1 3 4 4 1 2 2 3 3 1 1 4
Sample Output
YES NO
给你一幅无向图,让你判断是不是圆。。
成圆条件有两个。
第一:每一个点的入度只能为2.
第二:所有点属于一个集合。
上代码;
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
#include <iostream>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <time.h>
#include <stdlib.h>
int p[26];
int v[26];
int find(int x)
{
if(p[x]!=x)
p[x]=find(p[x]);
return p[x];
}
int hebing(int x,int y)
{
return p[x]=y;
}
int main()
{
int n,m,i,j,a,b;
while(cin>>n>>m)
{
memset(v,0,sizeof(v));
for(i=1; i<=25; i++)
p[i]=i;
while(m--)
{
cin>>a>>b;
v[a]++; //入度
v[b]++;
a=find(a);
b=find(b);
if(a!=b)
hebing(a,b); //合并、
}
for(i=1; i<=n; i++)
{
if(p[1]!=p[i]|| v[i]!=2)
break;
}
// cout<<i<<endl;
if(i==n+1)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}
相关文章推荐
- zoj 3321 Circle
- ZOJ 3321 Circle(并查集)
- ZOJ 3321 Circle(并查集啊)
- ZOJ 3321 Circle【模拟】
- ZOJ 3321 Circle【并查集】
- ZOJ 3321 Circle
- zoj 3321 Circle【并查集】
- zoj 3321 - Circle
- zoj 3321 Circle (4)
- zoj 3806 Incircle and Circumcircle(二分)
- zoj 1090 || poj 2242 The Circumference of the Circle
- zoj 3058 Circle and Ring
- zoj 3321
- ZOJ 3058 Circle and Ring(圆和圆环的相交面积)
- ZOJ 1090 - The Circumference of the Circle 解题报告
- ZOJ-1090-The Circumference of the Circle(计算几何,海伦公式,三角形外接圆半径公式,cout输出小数的一些问题)
- Incircle and Circumcircle - ZOJ 3806 几何
- POJ 2242|ZOJ 1090 The Circumference of the Circle
- ZOJ - 3806 Incircle and Circumcircle(计算几何+二分)
- ZOJ 3806 Incircle and Circumcircle(几何+二分)