"Codevs2597" gang

Source: Internet
Author: User

Description

A group of robbers appeared in Chicago in 1920. If two robbers meet, then they are either friends or enemies. And one thing is for sure, that is:

My friend's friend is my friend;

The enemy of my enemy is also my friend.

Two robbers are the same gang condition is when and only if they are friends. Now give you some information about the robbers and ask you how many bandit gangs there are.

Input

The first line of the input file gangs.in is an integer N (2<=n<=1000) that represents the number of robbers (from 1 to N). The second line, M (1<=m<=5000), indicates the number of information bars about the robber. The following m lines, each line may be f p Q or E P Q (1<=p q<=n), F means P and q are friends, E means P and q are enemies. The input data guarantees that the information is not contradictory.

Output

The output file gangs.out only one row, indicating the maximum number of possible gangs.

Sample Input

6
4
E 1 4
F 3 5
F 4 6
E 1 2

Sample Output

3

HINT

2<=n<=1000

1<=m<=5000

1<=p q<=n

Exercises

And check set, encounter friends Merge, Encounter enemy to add an edge to the diagram, the last for a point, if X and y are connected, x and z, merge Y and Z (enemy is Friends)

#include <iostream>using namespace std;const int n=10010;const int m=50010;struct node{int next,to;} E[m];int head[n],fa[n];int n,m,x,y,cnt=0,ans;char ch;void ins (int u,int v) {e[++cnt].to=v; e[cnt].next=head[u]; Head[u] =cnt;} int root (int x) {if (fa[x]!=x) Fa[x]=root (fa[x]); return fa[x];} void Union (int x,int y) {fa[x]=y;} int main () {cin>>n>>m;for (int i=1;i<=n;i++) fa[i]=i;for (int i=1;i<=m;i++) {cin>>ch>>x >>y;if (ch== ' E ') {ins (x, y); ins (y,x);} Else{if (Root (x)!=root (y)) Union (Fa[x],fa[y]);}} for (int k=1;k<=n;k++) for (int i=head[k];i;i=e[i].next) for (int j=head[e[i].to];j;j=e[j].next) if (k!=e[j].to& &root (k)!=root (e[j].to)) Union (fa[k],fa[e[j].to]); for (int i=1;i<=n;i++) if (fa[i]==i) Ans++;cout<<ans;}

  

"Codevs2597" 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.