hdu 5.1.7 virtual friend

hdu終於正常了……之前的題,都有些記不清了(什麼記性啊喂!)

Virtual Friends

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 54 Accepted Submission(s): 27
 
Problem Description
These days, you can do all sorts of things online. For example, you can use various websites to make virtual friends. For some people, growing their social network (their friends, their friends' friends, their friends' friends' friends, and so on), has become an addictive hobby. Just as some people collect stamps, other people collect virtual friends. 

Your task is to observe the interactions on such a website and keep track of the size of each person's network. 

Assume that every friendship is mutual. If Fred is Barney's friend, then Barney is also Fred's friend.
 
Input
Input file contains multiple test cases. 
The first line of each case indicates the number of test friendship nest.
each friendship nest begins with a line containing an integer F, the number of friendships formed in this frindship nest, which is no more than 100 000. Each of the following F lines contains the names of two people who have just become friends, separated by a space. A name is a string of 1 to 20 letters (uppercase or lowercase).
 
Output
Whenever a friendship is formed, print a line containing one integer, the number of people in the social network of the two people who have just become friends.
 
Sample Input
1
3
Fred Barney
Barney Betty
Betty Wilma
 
Sample Output
2
3
4
 

樸素的並查集,唯一的亮點是要用map建立字符串到數字的映射。map<string,int>p p.find(a) p.end() p.clear,就這些函數。用多了還挺順手的。

Input file contains multiple test cases. ……害我wa了一次。

#include <iostream>
#include <map>
#include <string>
using namespace std;

int root[100002];
int cnt[100002];//how many friend does (the root) have
int num;
map<string ,int> p;//map, cast string to int

void clear()
{
     int i;
     for(i=0;i<100002;i++)
     {
         root[i]=i;
         cnt[i]=1;
     }
     p.clear();
     num=0;
}

int find(int x)
{
    if(root[x]!=x)
    {
        root[x]=find(root[x]);//update root[]
    }
    return root[x];
}

void merge(int a,int b)
{
     int fa=find(a);
     int fb=find(b);
     if(fa!=fb)
     {
         root[fb]=fa;
         cnt[fa]+=cnt[fb];
     }
     cout<<cnt[fa]<<endl;
}
     
     

int main()
{
    int cas;
    while(cin>>cas)//Input file contains multiple test cases.
    {
    while(cas--)
    {
        int n;
        
        
        clear();
        cin>>n;
        while(n--)
        {
            string a,b;
            cin>>a>>b;
            if(p.find(a)==p.end())//no a in map
            {
                p[a]=++num;//cast a to ++num
            }
            if(p.find(b)==p.end())//no a in map
            {
                p[b]=++num;//
            }
            merge(p[a],p[b]);
        }
    }
    }
    system("pause");
    return 0;
}
            
            
            

二專這周結課,可以週末和米娜(當然主要是隊長大人哈哈)一起做題了~可惜本週的兩個還是參加不了……殘念……
恩,發現上面這種做法現在超時了,感謝 littlesheep_shaun~~

不過,網上找找不超時的方法只有hash表,這,看不懂...

唉...ctrl-c&v

#include<iostream>
#include<cstring>
#include<cstdio>
#define MAXN 200003
using namespace std;
typedef char Word[21];
Word que[MAXN];

const int HashSize = 1000003;
int head[HashSize], next[MAXN], rear;
int father[MAXN], num[MAXN]; 

inline void init_lookup_table(){
    rear=1; 
    memset(head, 0, sizeof(head));
}

inline int hash(Word &s){
    int seed = 131, v=0;
    for(int i=0; i<strlen(s); ++i){
        v = (v*seed + s[i]);
    }
    return (v & 0x7FFFFFFF)%HashSize;
}

bool try_to_insert(int s){
    int h = hash(que[s]);
    int u = head[h];
    while(u){
        if(strcmp(que[u], que[s])==0) return false;
        u = next[u];
    }
    next[s] = head[h];
    head[h] = s;
    num[s] = 1; // 加入成功後, 該位置的人數變爲1
    return true;
}

int search(Word &s){
    int h = hash(s);
    int u = head[h];
    while(u){
        if( strcmp(que[u], s)==0 ) return u;
        u = next[u];
    }
}


void init_unionset(){
    for(int i=0; i<MAXN; i++){
        father[i]=i; num[i] = 0;
    }
}

int find(int x){  
    int i, j=x;
    while(j!=father[j]) j = father[j]; // 找樹根
    while(x != j){ // 路徑壓縮
        i = father[x];
        father[x] = j;
        x = i;
    }
    return j;
}  

void Union(int x,int y){  
    x = find(x);  
    y = find(y);  
    if(x!=y){
        father[y] = x;
        num[x] += num[y];
    }
}


int main(){

    int T, n;
    Word name1, name2;
    while(scanf("%d", &T)!=EOF){

		while(T--){

		    init_lookup_table();
		    init_unionset();

		    scanf("%d",&n);

		    if(n==0) {
		        printf("0\n");
		        continue;
		    }
		    for(int i=0; i<n; ++i){
		        scanf("%s %s",name1, name2);
		        
		        strcpy(que[rear], name1);
		        if(try_to_insert(rear)) ++rear;

		        strcpy(que[rear], name2);
		        if(try_to_insert(rear)) ++rear;

		        int a = search(name1),  b = search(name2);

		        Union(a, b);

		        printf("%d\n",num[find(a)]); 
		    }
		}
    }
    return 0;
}

咕嚕咕嚕滾走

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章