Los athletic meets in the valley P2256 a sprinter

Los athletic meets in the valley P2256 a 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 Format

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 Format

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

Sample input and output

Input # 1
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 # 1
No.
Yes.
Yes.

Solution

And template title disjoint-set almost

This time I also used by the merger rank !

as well as

STL Dafa is good!

Code

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<map>
 4 using namespace std;
 5 map<string,int>name;
 6 int n,m,k,f[5001],size[5001],ans,yet;
 7 int getf(int a)
 8 {
 9     if(f[a]==a) return a;
10     return f[a]=getf(f[a]);//路径压缩 
11 }
12 void merge(int a,int b)
13 {
14     int i=getf(a);
15     int j=getf(b);
16     if(size[i]>size[j]) swap(i,j);
17     f[j]=i;
18     size[i]+=size[j];
19 }
20 int main()
21 {
22     cin>>n>>m;
23     string str,str1,str2;
24     for(int i=1;i<=n;i++)
25     {
26         size[i]=1;
27         f[i]=i;
28     }
29     for(int i=1;i<=n;i++)
30     {
31         cin>>str;
32         name[str]=i;
33     }
34     for(int i=1;i<=m;i++){
35         cin>>str1>>str2;
36         merge(name[str1],name[str2]);
37     }
38     cin>>k;
39     for(int i=1;i<=k;i++)
40     {
41         cin>>str1>>str2;
42         if(getf(name[str1])==getf(name[str2])) cout<<"Yes.\n";
43         else cout<<"No.\n";
44     }
45     return 0;
46 }

 

Guess you like

Origin www.cnblogs.com/send-off-a-friend/p/11374147.html