跳到正文

个人记录

111

cyunx约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年4月19日
更新
2026年4月19日
篇幅
约 2 分钟

版本

  1. v1当前
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+10;
int n,m,a[N],t,k,b[N],dep[N],maxn,sum,d;
vector<int>e[N];
void dfs(int x){
	for(auto now:e[x]){
		if(dep[now]==0){
			dep[now]=dep[x]+1;
			dfs(now);
			maxn=max(maxn,a[now]);
			sum++;
		}
	}
}
signed main(){
	cin>>t;
	while(t--){
		int ans=0;
		cin>>n>>d;
		int ma=0;
		for(int i=1;i<=n;i++){
			e[i].clear();
			dep[i]=0;
			cin>>a[i]>>b[i];
			if(i!=d){
				ma=max(ma,a[i]);
			}
		}
		int x,y;
		for(int i=1;i<n;i++){
			cin>>x>>y;
			e[x].push_back(y);
			e[y].push_back(x);
		}
		if(ma<a[d]||b[d]>2){
			cout<<0<<endl;
			continue;
		}
		dep[d]=1;
		for(auto now:e[d]){
			maxn=0;
			sum=0;
			dep[now]=2;
			dfs(now);
			maxn=max(maxn,a[now]);
			sum++;
			if(maxn>=a[d]){
				ans+=sum;
			}
		}
		cout<<ans<<endl;
	}
}
原文