In a sports meeting of sprinter

Topic background

The pace of the show in a lot of loving ** too late to show loving God Soviet university marching firm (?) Toward the starting point of the 100 meters run. Then God Su University found that students participating meter race is too much, even physical education teacher also busy. As a physical education teacher then discovered the Soviet Sports Committee of the University of God, they come to him for help. But God Soviet university needs to warm up, otherwise it will go half smoked (ribs), and he found you. . . If you help solve the problem of physical education teacher, the teacher will give you 5 points.

Title Description

Assuming a total of N (2 <= N <= 20000) th players. (Nima all students did so much of it)

The teacher will tell you the name of the N players.

Then I will tell you M (1 <= M <= 1000000) sentence that tells your Student A and Student B in the same group.

If a student A and B in the same group of students, students B and C students are in the same group, it shows students A and C students in the same group.

Then the teacher will ask you K (1 <= K <= 1000000) words, that student X and Y students are in the same group.

If the output "Yes.", Otherwise a "No."

Input and output formats

Input formats:

 

The first input line N and M.

Next N lines enter the name of each student.

Further down two M lines each input name, and to ensure that these two names have appeared above the N rows representing the two players in the same group.

Again enter K.

Now enter the K physical education teacher asked.

 

Output formats:

 

For each physical education teacher asked, output "Yes." Or "No.".

 

Sample input and output

Input Sample # 1:  Copy
10 6 
Jack 
Mike 
ASDA 
Michel 
brabrabra 
HeHe 
héhé 
papapa 
Hey 
Obama 
Jack Obama 
HeHe héhé 
brabrabra HeHe 
Obama ASDA 
papapa Obama 
Obama héhé 
3 
Mike Obama 
héhé Jack 
papapa brabrabra
Output Sample # 1:  Copy
No. 
Yes. 
Yes. 


Look over a year ago A, that is very simple and done it again, the results ??????

Note this question (asterisk)

1: Find a string:

If the number is, we do not find, only this can be combined, but if it is a string, it is necessary to find the observed range finding (2 <= N <= 20000) ( 2 < = N < = 2 0 0 0 0 ) then. . . We really can from 1 have been found 1 20001 2 0 0 0 1, if found, on his return index.

 

#include<cstdio>
#include<cstring>
using namespace std;
int n, m,k;
int f[30000];
int find(int x){
    if(f[x]==x){
        return x;
    }
    else return{
        f[x]=find(f[x]);
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    int i,j; 
    for(i = 1; i <= n; i ++)
    {
        int sum = 0;
        char s[50000];
        scanf("%s",s);
        for(int j = 0; j < strlen(s); j ++)
            sum += int(s[j]);
        f[sum] = sum;
    }
    for(i = 1; i <= m; i ++)
    {
        int sum1=0,sum2=0;
        char s1[5000] , s2[5000];
        scanf("%s%s",s1,s2);
        for(j = 0; j < strlen(s1); j ++)
            sum1 += int(s1[j]);
        for(j = 0; j < strlen(s2); j ++)
            sum2 += int(s2[j]);
        int k = find(sum1) , j = find(sum2);
        if(k!=j)
            f[k]=j;
    }
    scanf("%d",&k);
    for(i = 1; i <= k; i ++)
    {
        int sum1 = 0,sum2 = 0;
        char s1[5000] , s2[5000];
        scanf("%s%s",s1,s2);
        for(j = 0; j < strlen(s1); j ++)
            sum1 += int(s1[j]);
        for(j = 0; j < strlen(s2); j ++)
            sum2 += int(s2[j]);
        if(find(sum1) == find(sum2))printf("Yes.\n");
        else printf("No.\n");
    }
}

 

Guess you like

Origin www.cnblogs.com/hrj1/p/11129765.html