10分求助
查看原帖
10分求助
521283
wangif424楼主2023/7/8 18:26
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read() {
	register int r=0,f=0;
	register char c=getchar();
	while(c>'9'||c<'0'){
		if(c=='-')f=1;
		c=getchar();
	}
	while(c>='0'&&c<='9')r=(r<<1)+(r<<3)+c-'0',c=getchar();
	return f?-r:r;
}
inline void put(int x) {
	if(x<0){
		putchar('-');
		x=-x;
	}
	if(x>9)put(x/10);
	putchar(x%10+'0');
	return;
}
const int mod=1e9+7;
int t;
int n,l,r,op;
int ans;

struct b {
	int last,to,n;
} v[400100];
struct d {
	int k,b;
} s[400100];
int first[200100],len;
void add(int x,int y,int n) {
	++len;
	v[len].last=first[x];
	v[len].to=y;
	v[len].n=n;
	first[x]=len;
	return;
}
bool book[200100];//当前点的所在树是否查找过
inline void init() {
	memset(first,0,sizeof(first));
	len=0;
	ans=1;
	memset(v,0,sizeof(v));
	memset(book,0,sizeof(book));
	return;
}
signed main() {
	t=read();
	while(t--) {
		init();
		n=read();
		l=read();
		r=read();
		for(int i=1; i<n; i++) {
			op=read();
			int u,v,w;
			u=read();
			v=read();
			if(op!=0) {
				w=read();
				add(min(u,v),max(u,v),w);
			}
		}
		for(int i=1; i<=n; i++) {
			//put(book[i]);putchar('\n');
			if(book[i])continue;
			int nl=l;
			int nr=r;
			book[i]=1;
			s[i].b=0;
			s[i].k=1;
			queue<int>q;
			q.push(i);
			//put(q.size());
			while(!q.empty()) {
				for(int j=first[q.front()]; j; j=v[j].last) {
//					printf("|%lld %lld|",v[j].to,book[v[j].to]);
					if(book[v[j].to])continue;
					book[v[j].to]=1;
					q.push(v[j].to);
					s[v[j].to].b=v[j].n-s[q.front()].b;
					s[v[j].to].k=-s[q.front()].k;
//					printf("/%lld/",s[v[j].to].k);
					if(s[v[i].to].k==1) {
						nl=max(nl,l-s[v[j].to].b);
						nr=min(nr,r-s[v[j].to].b);
//						printf("|%lld|",s[v[j].to].k);
					} else {
						nl=max(nl,s[v[j].to].b-r);
						nr=min(nr,s[v[j].to].b-l);
//						printf("|%lld|",s[v[j].to].k);
					}
//					if(nl>=nr)break;
					
				}
				q.pop();
			}
			if(nl>nr){
				ans=0;
				break;
			}
			ans*=(nr-nl+1)%mod;
			ans%=mod;
//			put(ans);putchar(' ');
		}
		put(ans);
		putchar('\n');
	}
	return 0;
}
/*
1
4 -1000000000 1000000000
1 1 2 1000000000
1 2 3 -1000000000
1 2 4 -1000000000
*/
2023/7/8 18:26
加载中...