GDUT_排位赛题解报告_第1场_E题 Milk Visits

题目:

Farmer John is planning to build N (1≤N≤105) farms that will be connected by N−1 roads, forming a tree (i.e., all farms are reachable from each-other, and there are no cycles). Each farm contains a cow, whose breed is either Guernsey or Holstein.

Farmer John’s M friends (1≤M≤105) often come to visit him. During a visit with friend i, Farmer John will walk with his friend along the unique path of roads from farm Ai to farm Bi (it may be the case that Ai=Bi). Additionally, they can try some milk from any cow along the path they walk. Since most of Farmer John’s friends are also farmers, they have very strong preferences regarding milk. Some of his friends will only drink Guernsey milk, while the remainder will only drink Holstein milk. Any of Farmer John’s friends will only be happy if they can drink their preferred type of milk during their visit.

Please determine whether each friend will be happy after visiting.

Input
The first line contains the two integers N and M.

The second line contains a string of length N. The ith character of the string is ‘G’ if the cow in the ith farm is a Guernsey, or ‘H’ if the cow in the ith farm is a Holstein.

The next N−1 lines each contain two distinct integers X and Y (1≤X,Y≤N), indicating that there is a road between farms X and Y.

The next M lines contain integers Ai, Bi, and a character Ci. Ai and Bi represent the endpoints of the path walked during friend i’s visit, while Ci is either G or H if the ith friend prefers Guernsey milk or Holstein milk.

Output
Print a binary string of length M. The ith character of the string should be ‘1’ if the ith friend will be happy, or ‘0’ otherwise.

Example
inputCopy
5 5
HHGHG
1 2
2 3
2 4
1 5
1 4 H
1 4 G
1 3 G
1 3 H
5 5 H
outputCopy
10110
Note
Here, the path from farm 1 and farm 4 involves farms 1, 2, and 4. All of these contain Holsteins, so the first friend will be satisfied while the second one will not.

题意:
这个题目就是给一个树,各序号点有两种类型,一种是G结点,一种是H结点。给出起点和终点还有目标类型,问:这条路径是否存在目标类型的结点。

这课树会出现一个情况就是,两个点之间的直接路径就是最短路径,所以我们可以把所有 可达且同类型的点给连起来,这就是一个连通块了,然后起点和终点在不同的连通块那么意思就是说图中可以经过G,H两种类型的结点,如果是相同连通块那么只能经过本身连通块的类型点。

完整代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <queue>
#include <stack>
#include <map>
//鬼畜头文件
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9+7;
typedef unsigned long long ULL;
typedef long long LL;
//鬼畜define
int n,m;
char all[100010];
int fa[100010];
bool ans[100010];
int father(int k)
{
	if(fa[k]==k)return k;
	else return fa[k]=father(fa[k]);
}
void merge(int a,int b)
{
	fa[father(a)]=father(b);
}
int main()
{
	scanf("%d %d",&n,&m);
	scanf("%s",all);
	for(int time=0;time<n;time++)fa[time]=time;
	for(int time=0;time<n-1;time++)
	{
		int from,to;
		scanf("%d %d",&from,&to);
		from--;
		to--;
		if(all[from]==all[to])merge(from,to);
	}
	for(int time=0;time<m;time++)
	{
		int from,to;
		char ch;
		scanf("%d %d %c",&from,&to,&ch);
		from--;
		to--;
		if(father(from)==father(to)&&all[from]!=ch)ans[time]=0;
		else ans[time]=1;
	}
	for(int time=0;time<m;time++)printf("%d",ans[time]);
	printf("\n");
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章