璟雯院

珺璟如晔,雯华若锦

用户工具

站点工具


icpc:problems:luogup2441
problems
名称角色属性树
题目编号P2441
题目链接luogu.com.cn/…
来源Luogu
算法分类树型结构
难易程度容易

角色属性树

想法

记录父节点,暴力向上枚举。由于数据全随机,故gcd存在大概率不为1的情况,故题目变水了。

代码实现

#include<cstdio>
#include<algorithm>
const int N = 200010;
int father[N];
int line[N];
int gcd(int a, int b) {
	while (b != 0) {
		int tmp = a;
		a = b;
		b = tmp % b;
	}
	return a;
}
int main() {
	int n, k, x, y, type;
	scanf("%d %d", &n, &k);
	for(int i=1; i<=n; i++)
		scanf("%d", &line[i]);
	for(int i=1; i<n; i++) {
		scanf("%d %d", &x, &y);
		father[y] = x;
	}
	for(int t=1; t<=k; t++) {
		scanf("%d", &type);
		if(type == 1) {
			scanf("%d", &x);
			int temp = line[x];
			while(father[x] != 0) {
				if(gcd(line[father[x]], temp) != 1) {
					printf("%d\n", father[x]);
					break;
				}
				x = father[x];
			}
			if(father[x] == 0) 
				printf("-1\n");
		}
		else {
			scanf("%d %d", &x, &y);
			line[x] = y;
		}
	}
	return 0;
}
/app/www/public/data/pages/icpc/problems/luogup2441.txt · 最后更改: 2024/01/19 11:51 由 温婕莺