[Los] P2256 Valley

(^_^)

topic:

topic

Ideas:

This is a problem and check catchment, suitable for beginners to do! ! !

If not disjoint-set point in me , that is dalao the blog!

The title difficulty: the name is a string to string manipulation

A number for each name, such as \ (1,2,3,4,5,6, ... \) , with \ (\ texttt {STL the map} \) survive, then ordinary disjoint-set

This question is C ++ Welfare Rights ~

Pascal array is unknown can subscript string, then an array can get

Code:

Refuse ctrl+ C

c ++ Code

#include <iostream>

using namespace std;

map <string,int> ma;
int f[20010],tot,n,m;

int find(int k){     //并查集find()
    if(f[k]==k)return k;
    return f[k]=find(f[k]);
} 

int main()
{
    //*****初始化***** 
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)f[i]=i;
    //*****存名字***** 
    for(int i=1;i<=n;i++)
    {
        string s;
        cin>>s;
        tot++;
        ma[s]=tot;
    }
    //*****并***** 
    for(int i=1;i<=m;i++)
    {
        string s,st;
        cin>>s>>st;
        int a=ma[s],b=ma[st];
        f[find(a)]=find(b);
    } 
    //*****查*****
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        string s,st;
        cin>>s>>st;
        int a=ma[s],b=ma[st];
        if(find(a)==find(b))cout<<"Yes.\n";
        else cout<<"No.\n";
    } 
    return 0;
}

Guess you like

Origin www.cnblogs.com/GJY-JURUO/p/2019_07_23_01_11.html