NYOJ 42 stroke

Source: Internet
Author: User

Http://acm.nyist.net/JudgeOnline/problem.php? Pid = 42

Description

Zyc prefers to play games from an early age, including drawing a stroke. He wants to ask you to write a program for him to determine whether a graph can be used.

It is specified that all edges can be painted only once and cannot be repeated.

First, check the set to determine whether the link is connected, and then determine whether the stroke condition is met.

/** One stroke means that the line cannot be interrupted. You cannot draw duplicate lines. * The key to determining the singularity and number of even points. *: Only an even point can be taken, and any point can be taken as the starting point *: only two singularity s can be taken, but they must be taken as the starting point and ending point respectively. *: If there are more than two singularity values, one stroke is not allowed. * For some complex route problems, you can first convert them into simple ry, And then answer the questions based on the method of determining whether a stroke can be taken. * If a finite connected graph G has 2 k odd vertices, it can be written in k strokes and at least [2] In k strokes. */Import java. util. *; import java. io. *; public class Main {private int N, v, e; private int [] f; private int [] num; private int isCon; static consumer SC = new consumer (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) // first determine whether the graph is connected and then determine whether the System meets the One-stroke condition. out. println ("No"); else System. out. println ("Yes") ;}} public static void main (String [] args) {new Main (). start ();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.