Codeforces Round #396 (Div. 2)D. Mahmoud and a Dictionary(带权并查集)

Posted AC_Arthur

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #396 (Div. 2)D. Mahmoud and a Dictionary(带权并查集)相关的知识,希望对你有一定的参考价值。

题目链接:点击打开链接

思路:

带权并查集水题。  带权并查集可以知道在一个集合里的两点间距离。那么这种同义反义关心恰好对应距离的奇偶。

附上一图:


这就是合并的过程。

细节参见代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <ctime>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef long double ld;
const double eps = 1e-6;
const double PI = acos(-1);
const int mod = 1000000000 + 7;
const int INF = 0x3f3f3f3f;
// & 0x7FFFFFFF
const int seed = 131;
const ll INF64 = ll(1e18);
const int maxn = 1e5+10;
int T,n,m,q,p[maxn],dist[maxn];
map<string, int> mp;
char s[33], s1[33];
int _find(int x) 
    if(p[x] == x) return x;
    int oldfa = p[x];
    p[x] = _find(p[x]);
    dist[x] = (dist[x] + dist[oldfa])%2;
    return p[x];


void init() 
    for(int i = 1; i <= n; i++) 
        p[i] = i;
        dist[i] = 0;
    


int main() 
    scanf("%d%d%d", &n, &m, &q);
    for(int i = 1; i <= n; i++) 
        scanf("%s", s);
        mp[s] = i;
    
    init();
    for(int i = 1; i <= m; i++) 
        int id; scanf("%d%s%s", &id, s, s1);
        int id1 = mp[s], id2 = mp[s1];
        int x = _find(id1), y = _find(id2);
        if(x != y) 
            printf("YES\\n");
            if(id == 1) p[x] = y, dist[x] = (dist[id2]-dist[id1]+2)%2;
            else p[x] = y, dist[x] = (dist[id2]-dist[id1]+1+2)%2;
        
        else 
            int cur = (dist[id1]-dist[id2]+2)%2;
            if(id == 1) 
                if(cur & 1) printf("NO\\n");
                else printf("YES\\n");
            
            else 
                if(cur & 1) printf("YES\\n");
                else printf("NO\\n");
            
        
    
    while(q--) 
        scanf("%s%s", s, s1);
        int id1 = mp[s], id2 = mp[s1];
        int x = _find(id1), y = _find(id2);
        if(x != y) printf("3\\n");
        else 
            int cur = (dist[id1] - dist[id2] + 2)%2;
            if(cur & 1) printf("2\\n");
            else printf("1\\n");
        
    
    return 0;











以上是关于Codeforces Round #396 (Div. 2)D. Mahmoud and a Dictionary(带权并查集)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #396 (Div. 2) C. Mahmoud and a Message DP

Codeforces Round #396 (Div. 2) C题Mahmoud and a Message(dp)解题报告

Codeforces Round #396 (Div. 2) D题Mahmoud and a Dictionary(并查集)解题报告

Codeforces Round #396 (Div. 2)C. Mahmoud and a Message(dp)

Codeforces Round #396(Div. 2) A. Mahmoud and Longest Uncommon Subsequence

Codeforces Round #396 (Div. 2)D. Mahmoud and a Dictionary(带权并查集)