使用map容器写并查集

来源:互联网 发布:软件外包公司分录 编辑:程序博客网 时间:2024/05/08 23:32


题目链接 : http://hihocoder.com/contest/hiho14/problem/1


#include<iostream>#include<cstdio>#include<queue>#include<stack>#include<vector>#include<string>#include<set>#include<map>#include<algorithm>#include<cstring>#include<cstdlib>using namespace std;const int maxn=1e5+10;map<string, int> mm;int p[maxn];int find_fri(int x){    if( x==p[x] ) return x;    return p[x]=find_fri(p[x]);}int main(){    for(int i=0; i<maxn; i++) p[i]=i;    int n;    cin>>n;    int op, st=0;    string a, b;    for(int i=0; i<n; i++)    {        cin>>op>>a>>b;        mm.insert(pair<string, int>(a, st++));        mm.insert(pair<string, int>(b, st++));        int x=find_fri(mm[a]);        int y=find_fri(mm[b]);        if( !op )        {            if( x!=y ) p[x]=y;        }else        {            if( x==y ) printf("yes\n");            else printf("no\n");        }    }    return 0;}


0 0
原创粉丝点击