ZOJ 3321 Circle



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 V1V2V3,
... Vk, such that there are edges between V1 and V2V2 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 <= xy <= nx != 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;
}
时间: 2024-08-10 14:56:15

ZOJ 3321 Circle的相关文章

ZOJ 3321 Circle(并查集啊)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3731 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 th

ZOJ 3321 Circle【并查集】

解题思路:给定n个点,m条边,判断是否构成一个环 注意到构成一个环,所有点的度数为2,即一个点只有两条边与之相连,再有就是判断合并之后这n个点是否在同一个连通块 Circle Time Limit: 1 Second      Memory Limit: 32768 KB Your task is so easy. I will give you an undirected graph, and you just need to tell me whether the graph is just

ZOJ Problem Set - 3321 并查集

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3321 Circle Time Limit: 1 Second      Memory Limit: 32768 KB 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.

ZOJ 1450 Minimal Circle 最小圆覆盖

套了个模板直接上,貌似没有随机化序列 QAQ //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <cstring> #include <cmath> #include <stack> #include <queue> #include <

zoj 1450 Minimal Circle 最小覆盖圆

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=450 You are to write a program to find a circle which covers a set of points and has the minimal area. There will be no more than 100 points in one problem. 题意描述:找到一个最小圆能够包含到所有的二维坐标点. 算法

zoj 2853 Evolution(矩阵快速幂)

Evolution is a long, long process with extreme complexity and involves many species. Dr. C. P. Lottery is currently investigating a simplified model of evolution: consider that we haveN (2 <= N <= 200) species in the whole process of evolution, inde

ZOJ 3671 Japanese Mahjong III

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3671 Japanese Mahjong III Time Limit: 2 Seconds      Memory Limit: 65536 KB Mahjong is a game of skill, strategy and calculation and involves a certain degree of chance. In this proble

ZOJ 1450

最小圆覆盖 #include <iostream> #include <algorithm> #include <cstdio> #include <cmath> using namespace std; const double eps=0.00000001; struct point { double x,y; }p[110]; struct circle{ point cent; double rad; }cir; int n; double Tria

ZOJ 1675 矩形与圆的面积交

Little Mammoth Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge It is well known that mammoths used to live in caves. This is a story of a little mammoth who lived in a cave with his mummy and daddy. The mammoth was little and ver