题意:
给定一棵有 n n n 个结点的树,结点、边带权,一条路径的权值非零且为路径上点权和当且仅当该路径上不存在相邻边的边权相同,求所有路径的权值和。 ( n ≤ 3 × 1 0 5 ) (n \leq 3×10^5) (n≤3×105)
链接:
https://vjudge.net/problem/HDU-4303
解题思路:
第一反应写了点分,对于
c
a
l
cal
cal 函数,只需要直接
d
f
s
dfs
dfs 求出各子树合法的路径个数以及路径权值和,然后再计算即可。如计算
x
x
x 和
y
y
y 两子树合并的答案:
∑
i
∑
j
(
x
i
+
y
j
)
=
∑
i
(
n
u
m
y
∗
x
i
+
s
u
m
y
)
=
n
u
m
y
∗
s
u
m
x
+
s
u
m
y
∗
n
u
m
x
\sum\limits_{i}\sum\limits_{j}(x_i + y_j)=\sum\limits_{i}(num_y * x_i + sum_y)=num_y*sum_x+sum_y*num_x
i∑j∑(xi+yj)=i∑(numy∗xi+sumy)=numy∗sumx+sumy∗numx
树形
d
p
dp
dp 也可,子树信息可向上合并。
参考代码:
点分治:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define sz(a) ((int)a.size())
#define pb push_back
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define gmid (l + r >> 1)
const int maxn = 3e5 + 5;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
typedef pair<ll, ll> pll;
vector<pii> G[maxn];
int a[maxn], siz[maxn], vis[maxn], rub[maxn];
pll mp[maxn];
int n, tn, rmn, rt, top;
ll tmpx, tmpy, ans;
void getRt(int u, int f){
int mx = 0; siz[u] = 1;
for(auto &e : G[u]){
int v = e.second, w = e.first;
if(v == f || vis[v]) continue;
getRt(v, u);
siz[u] += siz[v];
mx = max(mx, siz[v]);
}
mx = max(mx, tn - siz[u]);
if(mx < rmn) rmn = mx, rt = u;
}
void dfs(int u, int f, ll y, int pre){
++tmpx, tmpy += y;
for(auto &e : G[u]){
int v = e.second, w = e.first;
if(v == f || vis[v] || w == pre) continue;
dfs(v, u, y + a[v], w);
}
}
void cal(int u){
ll sumx = 1, sumy = 0;
for(auto &e : G[u]){
int v = e.second, w = e.first;
if(vis[v]) continue;
tmpx = tmpy = 0;
dfs(v, u, a[v], w);
// cout << u << " " << tmpx << " " << tmpy << " " << sumx << " " << sumy << " " << ans << endl;
ans += tmpx * sumy + tmpy * sumx + tmpx * sumx * a[u];
ans -= tmpx * mp[w].second + tmpy * mp[w].first + tmpx * mp[w].first * a[u];
mp[w].first += tmpx, mp[w].second += tmpy;
sumx += tmpx, sumy += tmpy;
rub[++top] = w;
}
while(top) mp[rub[top--]] = {0, 0};
}
void dfz(int u){
vis[u] = 1;
cal(u);
for(auto &e : G[u]){
int v = e.second, w = e.first;
if(vis[v]) continue;
tn = siz[v], rmn = inf, getRt(v, u);
dfz(rt);
}
vis[u] = 0;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
while(cin >> n){
ans = 0;
for(int i = 1; i <= n; ++i){
G[i].clear();
}
for(int i = 1; i <= n; ++i) cin >> a[i];
for(int i = 1; i < n; ++i){
int u, v, w; cin >> u >> v >> w;
G[u].pb({w, v}), G[v].pb({w, u});
}
tn = n, rmn = inf, getRt(1, 0);
dfz(rt);
cout << ans << endl;
}
return 0;
}
树形
d
p
dp
dp:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define sz(a) ((int)a.size())
#define pb push_back
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define gmid (l + r >> 1)
const int maxn = 3e5 + 5;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
typedef pair<ll, ll> pll;
vector<pii> G[maxn];
map<int, pll> dp[maxn];
int a[maxn];
int n; ll ans;
void dfs(int u, int f){
dp[u].clear();
dp[u][0] = {1, a[u]};
pll sum = {1, a[u]};
for(auto &e : G[u]){
int v = e.second, w = e.first;
if(v == f) continue;
dfs(v, u);
for(auto &it : dp[v]){
if(it.first == w) continue;
pll tmp = (pll){it.second.first, it.second.second + it.second.first * a[u]};
ans += tmp.first * sum.second + tmp.second * sum.first - tmp.first * sum.first * a[u];
ans -= tmp.first * dp[u][w].second + tmp.second * dp[u][w].first - tmp.first * dp[u][w].first * a[u];
// cout << u << " " << v << " " << tmp.first << " " << tmp.second << " " << sum.first << " " << sum.second << " " << ans << endl;
}
for(auto &it : dp[v]){
if(it.first == w) continue;
pll tmp = (pll){it.second.first, it.second.second + it.second.first * a[u]};
dp[u][w].first += tmp.first, dp[u][w].second += tmp.second;
sum.first += tmp.first, sum.second += tmp.second;
}
}
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
while(cin >> n){
ans = 0;
for(int i = 1; i <= n; ++i){
G[i].clear();
}
for(int i = 1; i <= n; ++i) cin >> a[i];
for(int i = 1; i < n; ++i){
int u, v, w; cin >> u >> v >> w;
G[u].pb({w, v}), G[v].pb({w, u});
}
dfs(1, 0);
cout << ans << endl;
}
return 0;
}