-
Notifications
You must be signed in to change notification settings - Fork 0
/
Luogu_P_8971.cpp
106 lines (85 loc) · 1.62 KB
/
Luogu_P_8971.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// dfs + 剪枝
#include <iostream>
#include <vector>
#include <bitset>
using namespace std;
using ll = long long;
const ll p = 1e9 + 7;
const int N = 2e5 + 3;
struct Node
{
int n; // 编号
ll w; // 长度
};
vector<Node> g[N];
vector<int> rt;
bitset<N> vis;
ll l, r, _l, _r;
int pre[N];
void init(int n)
{
for (int i = 1; i <= n; ++ i)
{
pre[i] = i;
g[i].clear();
}
rt.clear();
vis.reset();
_l = l, _r = r;
}
int root(int x)
{
return pre[x] = (pre[x] == x ? x : root(pre[x]));
}
void merge(int x, int y)
{
pre[root(x)] = root(y);
}
ll dfs(int x, ll add = 0, bool sgn = true)
{
vis[x] = true;
for(auto &i : g[x])
{
if(vis[i.n]) continue;
dfs(i.n, i.w - add, !sgn);
}
_l = max(_l, sgn ? (l - add) : (add - r));
_r = min(_r, sgn ? (r - add) : (add - l));
return (_l <= _r) ? (_r - _l + 1) : 0;
}
void solve()
{
int n; cin >> n >> l >> r;
init(n);
for(int i = 1; i < n; ++ i)
{
int op; cin >> op;
if(op)
{
int u, v, w; cin >> u >> v >> w;
g[u].push_back({v, w});
g[v].push_back({u, w});
merge(u, v);
}
else
{
int u, v; cin >> u >> v;
}
}
for(int i = 1;i <= n; ++ i) if(root(i) == i) rt.push_back(i);
// 前期处理完成
ll res = 1;
for(auto &r0 : rt)
{
_l = l, _r = r;
res = res * dfs(r0) % p;
}
cout << res << '\n';
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int _; cin >> _;
while(_--) solve();
return 0;
}