NYOJ 42 一筆畫問題

來源:互聯網
上載者:User

http://acm.nyist.net/JudgeOnline/problem.php?pid=42

描述

zyc從小就比較喜歡玩一些小遊戲,其中就包括畫一筆畫,他想請你幫他寫一個程式,判斷一個圖是否能夠用一筆畫下來。

規定,所有的邊都只能畫一次,不能重複畫。

先用並查集判斷是否連通,然後判斷是否符合一筆畫的條件 

/*  * 一筆畫 即線路不能中斷,不能畫重複線路  * 能否一筆畫成,關鍵在於判別奇點、偶點的個數。   * ::只有偶點,可以一筆畫,並且可以以任意一點作為起點  * ::只有兩個奇點,可以一筆畫,但必須以這兩個奇點分別作為起點和終點。  * ::奇點超過兩個,則不能一筆畫。  * 對於一些比較複雜的路線問題,可以先轉化為簡單的幾何圖形,然後根據判定是否能一筆畫的方法進行解答。  * 如果有限連通圖 G 有 2k 個奇頂點,那麼它可以用 k 筆畫成,並且至少要用 k 筆畫成[2]。  */import java.util.*;import java.io.*;public class Main {private int N,v,e;private int[] f;private int[] num;private int isCon;static Scanner sc = new Scanner(new BufferedInputStream(System.in));void init(){for(int i=1;i<=v;i++){f[i]=i;num[i]=1;}}int findSet(int i){if(f[i]!=i)f[i]=findSet(f[i]);return f[i];}void unionSet(int x,int y){isCon++;if(num[x]>num[y]){f[y]=x;num[x]+=num[y];}else{f[x]=y;num[y]+=num[x];}}void start() {N=sc.nextInt();while(N--!=0){v=sc.nextInt();e=sc.nextInt();isCon=0;int[] map=new int[v+1];f=new int[v+1];num=new int[v+1];init();for(int i=0;i<e;i++){int m=sc.nextInt(),n=sc.nextInt();map[m]++;map[n]++;int x=findSet(m),y=findSet(n);if(x!=y)unionSet(x,y);}int degree=0;for(int i=1;i<=v;i++){if((map[i]&1)==1)degree++;}if(isCon!=v-1||degree!=0&°ree!=2)//先判斷是否是連通圖,然後判斷是否符合一筆畫的條件System.out.println("No");else System.out.println("Yes");}}public static void main(String[] args) {new Main().start();}}   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.