首页 > 其他 > 详细

hihocoder 1066 并查集

时间:2019-10-14 21:43:26      阅读:77      评论:0      收藏:0      [点我收藏+]
/**/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>

typedef long long LL;
typedef unsigned long long ULL;
using namespace std;

bool Sqrt(LL n) { return (LL)sqrt(n) * sqrt(n) == n; }
const double PI = acos(-1.0), ESP = 1e-10;
const LL INF = 99999999999999;
const int inf = 999999999, N = 1e5 + 5;
int n, op, fa[N], m, x, y;
char s[N];
map<string, int> mp;
int Find(int x)
{
    int y = x;
    for(; x != fa[x]; x = fa[x]);
    return fa[y] = x;
}

int Read()
{
    scanf("%s", s);
    if(mp.count(s)) return mp[s];
    mp[s] = ++m;
    fa[m] = m;
    return m;
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    scanf("%d", &n);
    while(n--) {
        scanf("%d", &op);
        x = Find(Read());
        y = Find(Read());
        if(op) x == y ? puts("yes") : puts("no");
        else fa[x] = y;
    }

    return 0;
}
/*
    input:
    output:
    modeling:
    methods:
    complexity:
    summary:
*/

hihocoder 1066 并查集

原文:https://www.cnblogs.com/000what/p/11674242.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!