L2-016. May all lovers in the world be long-lost brothers and sisters

L2-016. May all lovers in the world be long-lost brothers and sisters

time limit
200 ms
memory limit
65536 kB
code length limit
8000 B
Judgment procedure
Standard
author
Chen Yue

Ha ha. We all know that intermarriage is not allowed within five generations, that is, if the nearest common ancestor of two people is within five generations (i.e., the person, parents, grandparents, great-grandparents, and great-grandparents), they cannot intermarry. In this question, please help a pair of lovers decide whether they can get married?

Input format:

The first line of input gives a positive integer N (2 <= N <= 10 4 ), followed by N lines, each giving information about a person in the following format:

Personal ID Gender Father ID Mother ID

The ID is a 5-digit number, which is different for each person; the gender is M for male and F for female. If a person's father or mother is no longer available, the corresponding ID position is marked with -1.

Next, a positive integer K is given, followed by K lines, each line giving the ID of a pair of lovers, separated by spaces.

Note: The title guarantees that two people are of the same generation, each has only one gender, and that there is no incest or second-generation marriage in the blood relationship network.

Output format:

For each pair of lovers, judge whether their relationship can be intermarriage: if the two are of the same sex, output "Never Mind"; if it is the opposite sex and the relationship has five clothes, output "Yes"; if the opposite sex relationship has not been five clothes, output "Yes". Output "No".

Input sample:
24
00001 M 01111 -1
00002 F 02222 03333
00003 M 02222 03333
00004 F 04444 03333
00005 M 04444 05555
00006 F 04444 05555
00007 F 06666 07777
00008 M 06666 07777
00009 M 00001 00002
00010 M 00003 00006
00011 F 00005 00007
00012 F 00008 08888
00013 F 00009 00011
00014 M 00010 09999
00015 M 00010 09999
00016 M 10000 00012
00017 F -1 00012
00018 F 11000 00013
00019 F 11100 00018
00020 F 00015 11110
00021 M 11100 00020
00022 M 00016 -1
00023 M 10012 00017
00024 M 00022 10013
9
00021 00024
00019 00024
00011 00012
00022 00018
00001 00004
00013 00016
00017 00015
00019 00021
00010 00011
Sample output:
Never Mind
Yes
Never Mind
No
Yes
No
Yes
No
No
#include <stdio.h>
#include <string.h>
struct P {
	int fu, mu;
	char sex;
} M[100001];
int vis[10000], f;
void dfs_a(int x, int t) { ///Search for the first person's five servers
	if(x == -1) return ;
	if(t > 5) return ;
	vis[x] = 1;///Mark the ones related to him within five servers
	dfs_a(M[x].fu, t+1);
	dfs_a(M[x].mu, t+1);
}
void dfs_b(int x, int t) { ///Search for the second person's five suits
	if(x == -1 || f) return ;
	if(t > 5) return ;
	if(vis[x]) f = 1;
	dfs_b(M[x].fu, t+1);
	dfs_b(M[x].mu, t+1);
}
int main() {
	int n, a, c, d, m, x, y;
	char w, b;
	scanf("%d", &n);
	memset(M, -1, sizeof(M));///Initialization
	for(int i = 0; i < n; i++) {
		scanf("%d %c %d %d", &a, &b, &c, &d);
		M[a].sex = b;
		M[a].fu = c;
		M[a].mu = d;
		if(c!=-1) M[c].sex = 'M';
		if(d!=-1) M[d].sex = 'F';
	}
	scanf("%d", &m);
	while(m--) {
		scanf("%d %d", &x, &y);
		if(M[x].sex == M[y].sex) {
			printf("Never Mind\n");
			continue;
		}
		memset(vis, 0, sizeof(vis));
		dfs_a(x, 1);
		f = 0;
		dfs_b(y, 1);
		if(f) printf("No\n");
		else printf("Yes\n");
	}
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326039459&siteId=291194637