Continuation and collection of learning notes--gang Gang

Source: Internet
Author: User

A word does not first affixed to the topic description in a city live n person, any two people who know is not a friend is the enemy, and satisfies: 1, my friend's friend is my friend, 2, my enemy's enemy is my friend; all the people who are friends form a gang. Tell you about this n person's m piece of information, that is, some two people are friends, or some two people are enemies, ask you to write a program to calculate how many of the city may have a number of groups? Input 1th behavior N and m,n less than 1000,m less than 5000, the following M-line, each behavior p x y,p value is 0 or 1,p is 0 o'clock, indicating that X and Y are friends, p is 1 o'clock, that x and Y are enemies. Output An integer that indicates that n individuals may have a maximum of several gangs. Sample Input6
4
E 1 4
F 3 5
F 4 6
E 1 2
Sample Output3
HINT

{1},{2,4,6},{3,5}

The first idea, like the ABC beast, is to maintain the relationship between each point and ancestor. And it seems more simple. So we have the following procedure.

#include <iostream>#include<cstdio>using namespacestd;intfa[1010],deep[1010],ans[1010][2],n,m;intGETF (intk) {    if(fa[k]!=k) {        intt=Fa[k]; FA[K]=GETF (Fa[k]); DEEP[K]=deep[k]+Deep[t]; DEEP[K]=deep[k]%2; }    returnfa[k];}intMain () {CIN>>n>>m;  for(intI=1; i<=n;++i) {Fa[i]=i; Deep[i]=0; }     for(intI=1; i<=m;++i) {        Charc[2]; intx, y; scanf ("%s%d%d",&c,&x,&y); if((c[0]=='1') && (GETF (x)! =getf (y))) {            intfx=GETF (x); intfy=getf (y); DEEP[FX]= (deep[y]-deep[x]+3)%2; FA[FX]=fy; }        if((c[0]=='0') && (GETF (x)! =getf (y))) {            intfx=GETF (x); intfy=getf (y); DEEP[FX]= (deep[y]-deep[x]+2)%2; FA[FX]=fy; }    }         for(intI=1; i<=n;++i) {Fa[i]=GETF (i); //cout<<fa[i]<< "" <<deep[i]<<endl;ans[fa[i]][deep[i]]++; }    intAdd=0;  for(intI=1; i<=n;++i) for(intj=0; j<=1; ++j)if(ans[i][j]>0) {Add++; } cout<<add; return 0;}

And then 、、、、、、 blew 0.

Where's the problem? Suddenly found that the problem is not: My enemy's friend is my enemy. mdzz! It's not logical, though Noi never tells the logic.

Start again and think of the points of the way. Connected by a friend, Fa[x]=y, fa[x]=y+n,fa[x+n]=y by an enemy. The problem is solved perfectly. Downstairs program:

#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd; intn,m,ans,a[1010]; intfa[2020]; intFind (intx) {if(!fa[x]| | fa[x]==x)returnfa[x]=x; returnfa[x]=Find (fa[x]); }  voidUnion (intXinty) {x=find (x); y=Find (y); if(x==y)return ; FA[X]=y; }  intMain () {intI,x,y; Charp[Ten]; CIN>>n>>m;  for(i=1; i<=m;i++) {scanf ("%s%d%d",p,&x,&y); if(p[0]=='F') Union (x, y); ElseUnion (x, y+n), Union (x+n,y); }       for(i=1; i<=n;i++) A[i]=Find (i); Sort (a+1, a+n+1);  for(i=1; i<=n;i++)          if(i==1|| a[i]!=a[i-1])              ++ans; cout<<ans<<Endl; return 0; }  

To be continue ...

Continuation and collection of learning notes--gang Gang

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.