CF Drazil and his Happy Friends

Source: Internet
Author: User

Drazil and his Happy friendstime limit per test2 secondsmemory limit per test256 megabytesinputstandard Inputoutputstandar D Output

Drazil has many friends. Some of them is happy and Some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.

There is n boys and m girls among his friends. Let's number them from 0 to N -1 and 0 to m -1 separately. In i-th day, Drazil invites-th boy and-th girl to has dinner together (as Drazil is programmer, i starts from 0). If One of those people is happy, the other one would also become happy. Otherwise, those, people remain in their states. Once a person becomes happy (or if he/she is happy originally), he stays happy forever.

Drazil wants to know whether he can use the this plan to make all his friends become happy at some moment.

Input

The first line contains both integer n and m (1≤ n, m ≤100).

The second line contains integer b (0≤ bn), denoting the number of happy boys a Mong Friends of Drazil, and then follow b distinct integers x1, x2,. .., xb (0≤ xi < n), denoting the list of Indice S of happy boys.

The third line conatins integer g (0≤ gm), denoting the number of happy girls among Friends of Drazil, and then follow g distinct integers y1, y2, ..., y G (0≤ yJ < m), denoting the list of indices of happy Girls.

It is guaranteed, that there are at least one person, which is unhappy among he friends.

Output

If Drazil can make all the friends become happy by the this plan, print "Yes". Otherwise, print "No".

Sample Test (s) input
2 3
0
1 0
Output
Yes
Input
2 4
1 0
1 2
Output
No
Input
2 3
1 0
1 1
Output
Yes

Pushed for a half-day did not roll out the law, simply 10W of violence to a, later. After the ....
#include <iostream>#include<cmath>#include<cstring>#include<cstdio>#include<string>#include<algorithm>#include<cctype>#include<queue>#include<map>using    namespacestd;Const    intSIZE = the;intMainvoid){    intN,m,box; inth_n,h_m; intS_n[size] = {0}; intS_m[size] = {0}; CIN>> N >>m; CIN>>H_n;  for(inti =0; i < H_n;i + +) {cin>>box; S_n[box]=1; } CIN>>h_m;  for(inti =0; i < H_m;i + +) {cin>>box; S_m[box]=1; }     for(inti =0; I <100000; i + +)        if(s_n[i% n] | | s_m[i%m]) s_n[i% n] = s_m[i% m] =1; intFlag_1 =1; intFlag_2 =1;  for(inti =0; i < N;i + +)        if(!S_n[i]) {flag_1=0;  Break; }     for(inti =0; i < M;i + +)        if(!S_m[i]) {flag_2=0;  Break; }    if(Flag_1 &&flag_2) cout<<"Yes"<<Endl; Elsecout<<"No"<<Endl; return    0;}

CF Drazil and his Happy Friends

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.