ZOJ 2833-Friendship (query set + optimization)

Source: Internet
Author: User

ZOJ 2833-Friendship (query set + optimization)

Friendship Time Limit: 3 Seconds Memory Limit: 32768 KB
A friend is like a flower,
A rose to be exact,
Or maybe like a brand new gate
That never comes unlatched.

A friend is like an owl,
Both beautiful and wise.
Or perhaps a friend is like a ghost,
Whose spirit never dies.

A friend is like a heart that goes
Strong until the end.
Where wocould we be in this world
If we didn't have a friend?

-By Emma Guest

Now you 've got up, it's time to make friends. The friends you make in university are the friends you make for life. You will be proud if you have friends.

Input

There are multiple test cases for this problem.

Each test case starts with a line containing two integers N, M (1 <= N <= 100 '000, 1 <= M <= 200 '000 ), representing that there are totally N persons (indexed from 1 to N) and M operations, then M lines with the form "M a B" (without quotation) or "Q a" (without quotation) follow. the operation "M a B" means that person a and B make friends with each other, though they may be already friends, while "Q a" means a query operation.

Friendship is transitivity, which means if a and B, B and c are friends then a and c are also friends. in the initial, you have no friends users t yourself, when you are freshman, you know nobody, right? So in such case you have only one friend.

Output

For each test case, output "Case #:" first where "#" is the number of the case which starts from 1, then for each query operation "Q ", output a single line with the number of person a's friends.

Separate two consecutive test cases with a blank line,Do NOTOutput an extra blank line after the last one.

Sample Input

3 5
M 1 2
Q 1
Q 3
M 2 3
Q 2
5 10
M 3 2
Q 4
M 1 2
Q 4
M 3 2
Q 1
M 3 1
Q 5
M 4 2
Q 4

Sample Output

Case 1:
2
1
3

Case 2:
1
1
3
1
4


Naked and check the set. At the beginning, TLE checked the number of elements in the set to which an element belongs. to scan it again, O (n) is required) the worst case is O (m * n )... No, it's strange to use TLE. The number of elements in each set is directly recorded in the struct and initialized to 1. When merging, the number of elements in the merged set is added to another set, search for O (1)

#include 
 
  #include 
  
   #include 
   
    #include 
    
     #include 
     
      #include #include 
      
       #include 
       
        #include 
        
         #include 
         
          #include 
          #include 
           
            using namespace std;typedef struct node{int data,num;};node fa[100010];void Make_set(int n){for(int i=1;i<=n;i++){fa[i].data=i;fa[i].num=1;}}int Find(int x){if(x!=fa[x].data)fa[x].data=Find(fa[x].data);return fa[x].data;}void Union(int x,int y){int fx=Find(x);int fy=Find(y);if(fx==fy)return ;fa[fy].data=fx;fa[fx].num+=fa[fy].num;}int main(){int n,m,x,y,T=1;char op;while(scanf("%d%d",&n,&m)!=EOF){if(T!=1)printf("\n");getchar();printf("Case %d:\n",T++);Make_set(n);while(m--) { scanf("%c",&op); if(op=='M') {scanf("%d%d",&x,&y);getchar();Union(x,y); } else if(op=='Q') {scanf("%d",&x);getchar();int ans=fa[Find(x)].num;printf("%d\n",ans); }}}return 0;}
           
         
        
       
      
     
    
   
  
 


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.