HDU 1829 A Bug ' s life

Source: Internet
Author: User

Study and check the set,

If the root node of the 2 numbers is the same, it means they are in the same collection, detecting whether they are the opposite sex, if not the bug

If they are not the same, then determine if they are the opposite sex, if not, turn the gender of one of the sets upside down, merge them, or merge directly

#include <iostream> #define MAXN 2000+5using namespace Std;int n,m,flag;int f[maxn];int sign[maxn];void Ready () { for (int i=1;i<maxn;i++) f[i]=i,sign[i]=0;flag=1;} int dfs (int x) {if (f[x]!=x) F[x]=dfs (f[x]); return f[x];} void change (int x) {for (int i=1;i<=n;i++) {if (Dfs (x) ==dfs (i)) sign[i]=sign[i]^1;}} void build (int x,int y) {if (Dfs (x)!=dfs (y)) {if (Sign[x]==sign[y]) change (y); F[dfs (x)]=dfs (y);} Else{if (Sign[x]==sign[y]) flag=0;} int main () {Cin.sync_with_stdio (false); int T,casee=1,x,y;cin>>t;while (t--) {Ready ();cin>>n>>m; while (m--) {cin>>x>>y;build (x, y);} cout<< "Scenario #" <<casee++<< ":" <<endl;if (flag) cout<< "No suspicious bugs found!"; else cout<< "suspicious bugs found!"; Cout<<endl<<endl;} return 0;}


HDU 1829 A Bug ' s life

Related Article

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.