HDU 1829 A Bug's Life (query set + BFS (breadth-first search ))

Source: Internet
Author: User

A Bug's Life
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 6595 Accepted Submission (s): 2154

 

Problem Description
Background
Extends sor Hopper is researching the sexual behavior of a rare species of bugs. he assumes that they feature two different genders and that they only interact with bugs of the opposite gender. in his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs.

Problem
Given a list of bugs interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.

 

Input
The first line of the input contains the number of scenarios. each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. in the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. bugs are numbered consecutively starting from one.


Output
The output for every scenario is a line containing "Scenario # I:", where I is the number of the scenario starting at 1, followed by one line saying either "No suspicous bugs found! "If the experiment is consistent with his assumption about the bugs 'sexual behavior, or" Suspicious bugs found! "If your sor Hopper's assumption is definitely wrong.


Sample Input
2
3 3
1 2
2 3
1 3
4 2
1 2
3 4

Sample Output
Scenario #1:
Suspicious bugs found!

Scenario #2:
No suspicious bugs found!

 

 

Question: Determine whether each group of data is gay

 


Method 1: parallel query + BFS (breadth-first search)

 


 

Import java. io. *; import java. util. *; public class Main {int M = 2001, t, n, m; int sex [] = new int [M]; int patten [] = new int [M]; int map [] [] = new int [M] [M]; Queue <Integer> que = new struct list <Integer> (); public static void main (String [] args) {new Main (). work ();} void work () {vertex SC = new vertex (new BufferedInputStream (System. in); t = SC. nextInt (); for (int I = 1; I <= t; I ++) {n = SC. nextInt (); m = SC. nextInt (); init (); for (int j = 0; j <m; j ++) {int a = SC. nextInt (); int B = SC. nextInt (); map [a] [B] = map [B] [a] = 1; union (a, B);} System. out. println ("Scenario #" + I + ":"); if (BFS () {System. out. println ("Suspicious bugs found! ");} Else {System. out. println (" No suspicious bugs found! ");} System. out. println () ;}/// boolean BFS () {// this parameter is not included in the set and is added to the queue for (int I = 1; I <= n; I ++) {if (I = patten [I]) {sex [I] = 1; que. add (I) ;}} while (! Que. isEmpty () {int t = que. poll (); for (int I = 1; I <= n; I ++) {if (map [t] [I]! = 0) {if (sex [I] = 0) {if (sex [t] = 1) sex [I] = 2; // if the gender is not determined as the opposite sex else if (sex [t] = 2) sex [I] = 1; que. add (I); // add to queue} else {if (sex [t] = sex [I]) return true; // if the sex is the same, is gay .}}}} Return false;} // check the set and void union (int a, int B) {int pa = find (a); int pb = find (B); if (pa! = Pb) {patten [pa] = pb ;}// query the int find (int x) {if (patten [x] = x) return x; patten [x] = find (patten [x]); // return patten [x];} // initialize void init () {que. clear (); Arrays. fill (sex, 0); for (int I = 0; I <= n; I ++) {patten [I] = I; Arrays. fill (map [I], 0 );}}}
Import java. io. *; import java. util. *; public class Main {int t, n, m, max = 2010; int patten [] = new int [max]; int sex [] = new int [max]; boolean boo; public static void main (String [] args) {new Main (). work ();} void work () {vertex SC = new vertex (new BufferedInputStream (System. in); t = SC. nextInt (); for (int I = 1; I <= t; I ++) {n = SC. nextInt (); m = SC. nextInt (); init (); for (int j = 0; j <m; j ++) {int a = SC. nextInt (); int B = SC. nextInt (); if (find (a) = find (B) {// find their ancestor. if the gender is the same, it is gay boo = true ;} else {if (sex [a] = 0) sex [a] = B; // if the sex of the worm is not determined, set the sex to be different from that of another worm, deploy else union (sex [a], B) in a collection; // if the gender of the worm is determined, locate its ancestor, in the same set, remove if (sex [B] = 0) sex [B] = a; // same as else union (sex [B], );}} system. out. println ("Scenario #" + I + ":"); if (boo = true) System. out. println ("Suspicious bugs found! "); ElseSystem. out. println (" No suspicious bugs found! "); System. out. println () ;}// check the merged void union (int a, int B) {int pa = find (a); int pb = find (B ); if (pa = pb) return; patten [pa] = pb;} // query the int find (int x) {if (patten [x] = x) return x; patten [x] = find (patten [x]); // return patten [x];} // initialize void init () {boo = false; Arrays. fill (sex, 0); for (int k = 1; k <= n; k ++) {patten [k] = k ;}}}
Import java. io. *; import java. util. *; public class Main {int t, n, m, max = 2001; int patten [] = new int [max]; int opp [] = new int [max]; boolean boo; public static void main (String [] args) {new Main (). work ();} void work () {vertex SC = new vertex (new BufferedInputStream (System. in); t = SC. nextInt (); for (int I = 0; I <t; I ++) {n = SC. nextInt (); m = SC. nextInt (); init (); for (int j = 0; j <m; j ++) {int a = SC. nextInt (); int B = SC. nextInt (); if (o Pp [a] =-1 & amp; opp [B] =-1) {// if neither of the AB statements sets their uniqueness, divide the data into a collection to go to opp [a] = B; opp [B] = a;} else if (opp [a] =-1) {// If a does not appear, set it to the opposite sex. Divide them into a collection, update their ancestors, and divide them into a set in the central section of the opp [a] = B; union (a, opp [B]); // update their ancestors and divide them into a collection area} else if (opp [B] =-1) {// same as opp [B] = a; union (B, opp [a]);} else {if (find (a) = find (B) boo = true; // find the same ancestor gender, then it is the gay else {union (a, opp [B]); // update their ancestor and divide it into a union (B, opp [a]) in the central part of the Set; // update their ancestors and divide them into a centralized area} System. out. println ("Scenario #" + (I + 1) + ":"); if (boo = true) System. out. println ("Suspicious bugs found! "); ElseSystem. out. println (" No suspicious bugs found! "); System. out. println () ;}/// check the set and void union (int a, int B) {int pa = find (a); int pb = find (B ); if (pa = pb) return; patten [pa] = pb;} // query the set to find int find (int x) {if (patten [x] = x) return x; patten [x] = find (patten [x]); // return patten [x];} // initialize void init () {boo = false; for (int I = 1; I <= n; I ++) {patten [I] = I; opp [I] =-1 ;}}}

 

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.