CF766D Mahmoud and a Dictionary

Posted ukcxrtjr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF766D Mahmoud and a Dictionary相关的知识,希望对你有一定的参考价值。

题面: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
");
        }
        else{
            printf("NO
");
        }
    }
    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
");
        }
        else{
            printf("%d
",(Rank[g[x]]-Rank[g[y]]+2)%2+1);
        }
    }
    return 0;
}

```

以上是关于CF766D Mahmoud and a Dictionary的主要内容,如果未能解决你的问题,请参考以下文章

CF 862C Mahmoud and Ehab and the xor(异或)

CF.862D.Mahmoud and Ehab and the binary string(交互 二分)

[CF959E] Mahmoud and Ehab and the xor-MST - 贪心,最小生成树

Mahmoud and a Dictionary

B. Mahmoud and a Triangle1000 / 数学

Codeforces 862B - Mahmoud and Ehab and the bipartiteness