[並查集]一中校運會之百米跑

思路

有什麼好說的呢?我就多用了一個map將名字轉換成了數字,其他無異。並查集走起。

#include <cstdio>
#include <string>
#include <map>
#include <algorithm>
#include <iostream>
using namespace std;
int fa[20002];
map<string,int> ma;
int k;
int n,m;
string s,s1;
int find(int pos)
{
    if(fa[pos] == pos)return pos;
    fa[pos] = find(fa[pos]);//優化
    return fa[pos];
}
void unione(int a,int b)
{
    int x = find(a);
    int y = find(b);
    if(x != y);
        fa[y] = x;
}
int main()
{
    register int i;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++)
    {
        cin>>s;
        ma[s] = i;
        fa[i] = i;
    }
    for(i=1;i<=m;i++)
    {
        cin>>s>>s1;
        unione(ma[s],ma[s1]);
    }
    scanf("%d",&k);
    for(i=1;i<=k;i++)
    {
        cin>>s1>>s;
        if(find(ma[s1]) == find(ma[s]))printf("Yes.\n");
        else printf("No.\n");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章