码迷,mamicode.com
首页 > 其他好文 > 详细

CF766D Mahmoud and a Dictionary

时间:2019-10-08 22:20:55      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:http   and   using   names   抽象   tps   并查集   lse   iostream   

题面:https://www.luogu.org/problem/CF766D

本题直接开个map把字符串抽象成点,然后带权并查集就能解决了.
Code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<map>
using namespace std;
const int N=100005;
int n,m,q,fa[N],Rank[N];
map<string,int> g;
int Find(int x){
    if(x!=fa[x]){
        int t=Find(fa[x]);
        Rank[x]=(Rank[x]+Rank[fa[x]])%2;
        fa[x]=t;
    }
    return fa[x];
}
int merge(int d,int x,int y){
    int fx=Find(x),fy=Find(y);
    if(fx==fy){
        if((Rank[x]-Rank[y]+2)%2!=d){//x->y
            return 0;
        }
        return 1;
    }
    fa[fx]=fy;
    Rank[fx]=(Rank[y]-Rank[x]+d+2)%2;//fx->x->y->fy=fx->fy
    return 1;
}
int main(){
    cin>>n>>m>>q;
    for(int i=1;i<=n;i++){
        fa[i]=i;
    }
    for(int i=1;i<=n;i++){
        string a;
        cin>>a;
        g[a]=i;
    }
    for(int i=1;i<=m;i++){
        int d;
        string x,y;
        cin>>d>>x>>y;
        if(merge(d-1,g[x],g[y])==1){
            printf("YES\n");
        }
        else{
            printf("NO\n");
        }
    }
    for(int i=1;i<=q;i++){
        string x,y;
        cin>>x>>y;
        int fx=Find(g[x]),fy=Find(g[y]);
        if(fx!=fy){
            printf("3\n");
        }
        else{
            printf("%d\n",(Rank[g[x]]-Rank[g[y]]+2)%2+1);
        }
    }
    return 0;
}

```

CF766D Mahmoud and a Dictionary

标签:http   and   using   names   抽象   tps   并查集   lse   iostream   

原文地址:https://www.cnblogs.com/ukcxrtjr/p/11637880.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!