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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章