P4149
这道题目我为了方便保存距离与边数的关系,大量使用了 unordered_map,导致速度变慢,且没有关闭同步流,罪大恶极的丢失了 分。
CF1923E
这道题目我输入复制的上一道题代码,导致多输入了 和边权,全部炸掉了。
所以将输入输出改正且单独处理了 函数递归到重复的节点颜色直接退出的错误,将其更改为仅仅取消加入 数组的权限。
CF1575E
这道题塞时没写。
考虑使用点分治解决这道题目。
我们使用点分治向下递归时,设当前递归到的重心为 ,在已经统计完后的子树中,对每个点统计 到他们的路径的换乘次数,接着统计当前子树的换乘次数,此时枚举到的答案就是两个次数相加。
但是此时会有一种特殊情况,我们单独讨论:
当前枚举到的链配对到的已经计算过的链的第一条边和当前枚举的链的第一条边颜色不同时,换乘次数就需要额外增加 。我们存储一下每条链的第一条边的颜色,判断即可。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN = 1e6 + 1, MOD = 1e9 + 7;
int k, siz[MAXN], dep[MAXN], maxp, tot, tmp[MAXN], id, ans, rt;
bool vis[MAXN];
struct node {
int to, t;
};
struct NODE {
int c, t, s;
};
vector<node> c[MAXN];
vector<NODE> op;
int n, a[MAXN];
int cnt_tr[2][MAXN], sum_tr[2][MAXN];
int tmp_t[MAXN], tmp_s[MAXN];
int lowbit(int x) {
return x & -x;
}
void add_cnt(int c, int x, int v) {
x++;
while(x <= k + 1) {
cnt_tr[c][x] = (cnt_tr[c][x] + v + MOD) % MOD;
x += lowbit(x);
}
return;
}
int q_cnt(int c, int x) {
if(x < 0) {
return 0;
}
if(x > k) {
x = k;
}
x++;
int res = 0;
while(x > 0) {
res = (res + cnt_tr[c][x]) % MOD;
x -= lowbit(x);
}
return res;
}
void add_sum(int c, int x, int v) {
x++;
v = (v % MOD + MOD) % MOD;
while(x <= k + 1) {
sum_tr[c][x] = (sum_tr[c][x] + v + MOD) % MOD;
x += lowbit(x);
}
return;
}
int q_sum(int c, int x) {
if(x < 0) {
return 0;
}
if(x > k) {
x = k;
}
x++;
int res = 0;
while(x > 0) {
res = (res + sum_tr[c][x]) % MOD;
x -= lowbit(x);
}
return res;
}
void get_size(int x, int f) {
siz[x] = 1;
for(int i = 0; i < c[x].size(); i++) {
int y = c[x][i].to;
if(y == f || vis[y]) {
continue;
}
get_size(y, x);
siz[x] += siz[y];
}
return;
}
void get_rt(int x, int fa) {
int mx = 0;
for(int i = 0; i < c[x].size(); i++) {
int y = c[x][i].to;
if(y == fa || vis[y]) {
continue;
}
get_rt(y, x);
mx = max(mx, siz[y]);
}
mx = max(mx, tot - siz[x]);
if(mx < maxp) {
maxp = mx;
rt = x;
}
}
void get_dis(int x, int fa, int cur_t, int cur_s, int last_c) {
if(cur_t > k) {
return;
}
tmp_t[++id] = cur_t;
tmp_s[id] = cur_s % MOD;
for(int i = 0; i < c[x].size(); i++) {
int y = c[x][i].to;
int t = c[x][i].t;
if(y == fa || vis[y]) {
continue;
}
int new_t = (cur_t + (t != last_c));
int new_s = (cur_s + a[y]) % MOD;
get_dis(y, x, new_t, new_s, t);
}
}
void solve(int x) {
get_size(x, -1);
tot = siz[x];
maxp = 1e18;
get_rt(x, -1);
op.clear();
int cur = rt;
for(int i = 0; i < c[rt].size(); i++) {
int y = c[rt][i].to;
int tc = c[cur][i].t;
if(vis[y]) {
continue;
}
id = 0;
get_dis(y, cur, 0, (a[cur] + a[y]) % MOD, tc);
for(int j = 1; j <= id; j++) {
int t = tmp_t[j];
int s = tmp_s[j];
if(t <= k) {
ans = (ans + s) % MOD;
}
int same_max = k - t;
int diff_max = k - t - 1;
int cnt_tot = (q_cnt(tc, same_max) + q_cnt(tc ^ 1, diff_max)) % MOD;
int sum_tot = (q_sum(tc, same_max) + q_sum(tc ^ 1, diff_max)) % MOD;
int cur_s = (s - a[cur]) % MOD;
cur_s = (cur_s + MOD) % MOD;
int res = cur_s * cnt_tot % MOD;
res = (res + sum_tot) % MOD;
ans = (ans + res) % MOD;
}
for(int j = 1; j <= id; j++) {
int t = tmp_t[j];
int s = tmp_s[j];
if(t > k) {
continue;
}
add_cnt(tc, t, 1);
add_sum(tc, t, s);
op.push_back({tc, t, s});
}
}
for(int i = 0; i < op.size(); i++) {
int c = op[i].c;
int t = op[i].t;
int s = op[i].s;
add_cnt(c, t, -1);
add_sum(c, t, -s);
}
vis[rt] = true;
for(int i = 0; i < c[cur].size(); i++) {
if(vis[c[cur][i].to]) {
continue;
}
solve(c[cur][i].to);
}
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
// freopen("as01.in", "r", stdin);
// freopen("as01.out", "w", stdout);
cin >> n >> k;
int sum = 0;
for(int i = 1; i <= n; i++) {
cin >> a[i];
sum = (sum + a[i]) % MOD;
}
for(int i = 1, x, y, z; i < n; i++) {
cin >> x >> y >> z;
c[x].push_back({y, z});
c[y].push_back({x, z});
}
ans = sum;
solve(1);
cout << (ans % MOD + MOD) % MOD;
return 0;
}