Email Merge——hihoCoder171

描述

You are given a list of usernames and their email addresses in the following format:

alice 2 alice@hihocoder.com alice@gmail.com
bob 1 bob@qq.com
alicebest 2 alice@gmail.com alice@qq.com
alice2016 1 alice@qq.com

Your task is to merge the usernames if they share common email address: 

alice alicebest alice2016
bob

输入

The first line contains an integer N, denoting the number of usernames. (1 < N ≤ 10000)

The following N lines contain N usernames and their emails in the previous mentioned format.

Each username may have 10 emails at most. 

输出

Output one merged group per line.

In each group output the usernames in the same order as the input.

Output the groups in the same order as their first usernames appear in the input. 

样例输入
4
alice 2 alice@hihocoder.com alice@gmail.com
bob 1 bob@qq.com
alicebest 2 alice@gmail.com alice@qq.com
alice2016 1 alice@qq.com 
样例输出
alice alicebest alice2016
bob 


————————————————————————分割线——————————————————————————————

思路:

我一开始的思路是每次读取名字之后,再读取后面的邮箱,若邮箱显示有人占有,那么将这个人与刚读取的人merge(用并查集的方法),再将该人的标志位设为1,表示这个人是重复。

若某个名字后面有多个邮箱被占用,那么找到这多个邮箱的所有者的名字,并将这些人全部merge,标志位同样设为1。

最后输出时,按顺序找到标志位为0的人,并输出其重名的人名即可。

vector<int> group[10000];
int father[10010];
bool flag[10010] = {false};

void Union(int a, int b){
    int ta = a, tb = b;
    while(flag[a]) a = father[a];
    while(flag[b]) b = father[b];
    if(a == b) return;
    if(a > b) {swap(a, b);swap(ta, tb);}
    group[a].push_back(tb);
    for(auto i = group[b].begin(); i!=group[b].end(); i++){
        group[a].push_back(*i);
    }
    flag[tb] = true;
    
}

int main(){
    int n;
    cin>>n;
    string name[10010];
    map<string, int> mail;
    
    for(int i = 0; i<n; i++){
        int m;
        cin>>name[i]>>m;
        for(int j = 0; j<m; j++){
            string temp; cin>>temp;
            if(mail.count(temp) == 0){
                mail[temp] = i;
            }
            else{
                if(!flag[i]){
                    flag[i] = true;
                    int tt = mail[temp];
                    while(flag[tt]){
                        tt = father[tt];
                    }
                    father[i] = tt;
                    group[tt].push_back(i);
                }
                else{
                    Union(father[i], mail[temp]);
                }
                
            }
            
        }
        
    }
    for(int i = 0; i<n; i++){
        if(!flag[i]){
            cout<<name[i];
            sort(group[i].begin(), group[i].end());
            for(auto j = group[i].begin(); j != group[i].end(); j++){
                cout<<" "<<name[*j];
            }
            cout<<endl;
        }
    }

    return 0;
}

代码很难看,并且显示TLE(只有30分),可能是按照依次找到邮箱的所有者并且merge这一块有问题,于是找了下网上的方法。

将每个邮箱的所有者都标明,然后将这些用户名全都merge即可。实现代码如下:
string name[10010];
map<string, vector<int>> mail;
int father[10010];

int findf(int a){
    while(father[a] != a) a = father[a];
    return a;
}

void Union(int a, int b){
    int fa = findf(a); int fb = findf(b);
    if(fa != fb){
        if(fa < fb) father[fb] = fa;
        else father[fa] = fb;
    }   
}


int main(){
    int n;
    cin>>n;
    for(int i = 0; i<n; i++){
        cin>>name[i];
        father[i] = i;
        int m; cin>>m;
        for(int j = 0; j<m; j++){
            string temp; cin>>temp;
            mail[temp].push_back(i);
        }
    }
    for(auto i = mail.begin(); i != mail.end(); i++){
        int len = (int)i->second.size();
        for(int j = 1; j<len; j++){
            Union(i->second[0], i->second[j]);
        }
    }
    
    vector<int> group[10010];
    for(int i = 0; i<n; i++){
        if(findf(i) != i) group[findf(i)].push_back(i);
    }
    for(int i = 0; i<n; i++){
        if(findf(i) == i){
            cout<<name[i];
            for(int j = 0; j<group[i].size(); j++){
                cout<<" "<<name[group[i][j]];
            }
            cout<<endl;
        }
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值