国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

zoj Circle

2019-11-10 18:27:25
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

并查集問(wèn)題。給出n個(gè)點(diǎn)n對(duì)連接關(guān)系

我們需要判斷它是否能夠構(gòu)成一個(gè)類似圓的圖形(帶邊的封閉圖形)

這個(gè)時(shí)候我們需要用并查集

并查集記得加路徑優(yōu)化

我們需要知道封閉圖形每個(gè)點(diǎn)只能與兩個(gè)點(diǎn)相連。

而且這個(gè)封閉圖形由n個(gè)點(diǎn)組成

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 node y.

There is a blank line between cases.

Output

If the graph is just a circle, output "YES", otherwise output "NO".

Sample Input

3 31 22 31 34 41 22 33 11 4

Sample Output

YESNO上代碼

#include<stdio.h>#include<string.h>#include<stdlib.h>#include<algorithm>#include<iostream>#include<ctype.h>#include<stack>#include<queue>using namespace std;int f[1003],num[1003],ans[1003];int join(int x){    return (x==f[x])?x:join(f[x]);//并查集的查,外加路徑壓縮}void coin(int x,int y)//并{    int xx=join(x);    int yy=join(y);    if(xx!=yy)    {        ans[xx]+=ans[yy];//每次建立新的聯(lián)系祖先點(diǎn)都需要將分支點(diǎn)的所包含的子點(diǎn)數(shù)加起來(lái)

        f[yy]=xx;    }}

//我們需要記錄每個(gè)點(diǎn)出現(xiàn)的次數(shù)

//我們需要借助祖先點(diǎn)

int main(){    int n,m;    while(~scanf("%d%d",&n,&m))    {        int flag=0;        memset(num,0,sizeof(num));        for(int i=1; i<=n; i++)f[i]=i,ans[i]=1;        int a,b;        for(int i=0; i<m; i++)        {            scanf("%d%d",&a,&b);            if(a==b)            {                flag=1;            }            num[a]++;            num[b]++;            coin(a,b);        }        for(int i=1; i<=n; i++)        {            int z=join(i);            if(num[i]!=2||ans[z]!=n)            {                //PRintf("%d %d/n",i,ans[z]);                flag=1;                break;            }        }        if(flag)            printf("NO/n");        else printf("YES/n");    }}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 凤冈县| 昭平县| 贵港市| 天津市| 广宁县| 隆回县| 开平市| 临桂县| 上饶市| 历史| 合水县| 光山县| 高阳县| 茶陵县| 乐东| 敦化市| 探索| 辽阳县| 灌阳县| 宁明县| 黎平县| 栾城县| 聂拉木县| 包头市| 五原县| 师宗县| 平果县| 太仆寺旗| 辽中县| 静海县| 景泰县| 即墨市| 门头沟区| 浙江省| 平武县| 博客| 宜兰市| 泊头市| 商河县| 萝北县| 内丘县|