第一次学点分治是去年的九月份。
到现在已经一年了。
今天希望通过这篇博客让大家都能好好吸收淀粉质。
首先我们要知道分治是什么
天下大势 分久必合 合久必分
什么是合?什么是分?
给你一棵树长这样
我们发现上面有重心两个字 重心真是一个好的性质 关于为什么选重心可以观看 漆子超的论文
这样我们对第一个重心处理出所有路径 然后发现 路径有图上六条
我们需要两两合并 就像A - D 和 A - B合并 就变成B -D
但是大家返现A - B -C 和 A - B -E合并就会有重边
在我们统计一个树上路径为k的时候这样显然会出问题
我们想一下是哪里出问题了?
就是子树里的两两匹配的时候显然会出错
所以我们对子树也要进行一次操作 只不过这次操作是减去他的影响
比如 你看你到B的时候 变成B , B - C ,B - E
但是他们合并好像是正常的?注意我们solve函数 我们会把上条边传进去在这个例子也就是A 到 B
的边
下面是几道例题
POJ1741
小于等于k的路径数 排序后双指针即可
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
//#pragma comment(linker, "/STACK:10240000,10240000")
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
typedef unsigned long long ull;
const ull hash1 = 201326611;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
//ull ha[MAX_N],pp[MAX_N];
inline int read()
{
int date = 0,m = 1; char ch = 0;
while(ch!='-'&&(ch<'0'|ch>'9'))ch = getchar();
if(ch=='-'){m = -1; ch = getchar();}
while(ch>='0' && ch<='9')
{
date = date*10+ch-'0';
ch = getchar();
}return date*m;
}
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
/*int root[MAX_N],cnt,sz;
namespace hjt
{
#define mid ((l+r)>>1)
struct node{int l,r,maxx;}T[MAX_N*40];
#undef mid
}*/
const int MAX_N = 20025;
int eid,p[MAX_N],k,Size[MAX_N],maxson[MAX_N],SIZE,ms,root,now[MAX_N],dep[MAX_N],ans;
bool vis[MAX_N];
struct node
{
int next,v,dis;
}e[MAX_N<<1];
void init()
{
memset(p,-1,sizeof(p));
eid = 0;
}
void add(int u,int v,int dis)
{
e[eid].v = v;
e[eid].dis = dis;
e[eid].next = p[u];
p[u] = eid++;
}
void getroot(int x,int fa)
{
Size[x] = 1;maxson[x] = 0;
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;
if(vis[v]||v==fa) continue;
getroot(v,x);
Size[x]+=Size[v];
if(Size[v]>maxson[x]) maxson[x] = Size[v];
}
if(SIZE-Size[x]>maxson[x]) maxson[x] = SIZE-Size[x];
if(ms>maxson[x]) ms = maxson[x],root = x;
}
void getdis(int x,int fa)
{
now[++now[0]] = dep[x];
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;
if(v==fa||vis[v]) continue;
dep[v]=dep[x]+e[i].dis;
getdis(v,x);
}
}
int solve(int x,int dis,int flag)
{
dep[x] = dis;now[0] = 0;getdis(x,-1);
sort(now+1,now+1+now[0]);
int L = 1,R = now[0],sum = 0;
while(L<R) if(now[L]+now[R]<=k) sum+=R-L,L++;else R--;
return sum;
}
void fenzhi(int x,int ssize)
{
vis[x] = true;
ans+=solve(x,0,0);
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;
if(vis[v]) continue;
ans-=solve(v,e[i].dis,1);
ms = inf,root = 0;
SIZE = Size[v]<Size[x]?Size[v]:(ssize-Size[x]);
getroot(v,0);
fenzhi(root,(Size[v]<Size[x]?Size[v]:(ssize-Size[x])));
}
}
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n,a,b,c;
while(scanf("%d%d",&n,&k)==2&&(n|k))
{
memset(vis,false,sizeof(bool)*(n+1));
ans = 0;
init();
for(int i = 1;i<n;++i)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);add(b,a,c);
}
root = 0;ms = inf;SIZE = n;
getroot(1,-1);
fenzhi(root,n);
printf("%d\n",ans);
}
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
P3806
问路径和为k的路径是否存在
发现询问k不大 用桶标记 O1输出
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
//#pragma comment(linker, "/STACK:10240000,10240000")
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
typedef unsigned long long ull;
const ull hash1 = 201326611;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
//ull ha[MAX_N],pp[MAX_N];
inline int read()
{
int date = 0,m = 1; char ch = 0;
while(ch!='-'&&(ch<'0'|ch>'9'))ch = getchar();
if(ch=='-'){m = -1; ch = getchar();}
while(ch>='0' && ch<='9')
{
date = date*10+ch-'0';
ch = getchar();
}return date*m;
}
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
/*int root[MAX_N],cnt,sz;
namespace hjt
{
#define mid ((l+r)>>1)
struct node{int l,r,maxx;}T[MAX_N*40];
#undef mid
}*/
const int MAX_N = 10025;
int p[MAX_N],eid,SIZE,root,ms,dep[MAX_N],Size[MAX_N],maxson[MAX_N],now[MAX_N];
int cnt[10000025];
bool vis[MAX_N];
struct edge
{
int v,next,dis;
}e[MAX_N<<1];
void init()
{
memset(p,-1,sizeof(p));
eid = 0;
}
void add(int u,int v ,int dis)
{
e[eid].v = v;
e[eid].next = p[u];
e[eid].dis = dis;
p[u] = eid++;
}
void getroot(int x,int fa)
{
Size[x] = 1;maxson[x] = 0;
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;
if(vis[v]||v==fa) continue;
getroot(v,x);
Size[x]+=Size[v];
if(Size[v]>maxson[x]) maxson[x] = Size[v];
}
if(SIZE-Size[x]>maxson[x]) maxson[x] = SIZE - Size[x];
if(ms>maxson[x]) ms = maxson[x],root = x;
}
void getdis(int x,int fa)
{
now[++now[0]] = dep[x];
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;
if(v==fa||vis[v]) continue;
dep[v] = dep[x]+e[i].dis;
getdis(v,x);
}
}
void solve(int x,int dis,int flag)
{
dep[x] = dis;now[0]=0;getdis(x,-1);
sort(now+1,now+1+now[0]);
int L = 1,R = now[0];
if(!flag) while(L<R) if(now[L]+now[R]<=10000000) cnt[now[L]+now[R]]++,L++;else R--;
if(flag) while(L<R) if(now[L]+now[R]<=10000000) cnt[now[L]+now[R]]--,L++;else R--;
}
void fenzhi(int x,int ssize)
{
vis[x] = true;
solve(x,0,0);
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;
if(vis[v]) continue;
solve(v,e[i].dis,1);
ms = inf;root = 0;
SIZE = Size[v]<Size[x]?Size[v]:(ssize-Size[x]);
getroot(v,0);
fenzhi(root,(Size[v]<Size[x]?Size[v]:(ssize-Size[x])));
}
}
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n,m,a,b,c,k;
scanf("%d%d",&n,&m);
init();
for(int i = 1;i<n;++i)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);add(b,a,c);
}
root = 0;ms = inf;SIZE = n;
getroot(1,-1);
fenzhi(root,n);
while(m--)
{
scanf("%d",&k);
if(cnt[k]) printf("AYE\n");
else printf("NAY\n");
}
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
P2634
路径和为3
用路径取模后为 0 为 1 为 2的桶统计答案即可
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
//#pragma comment(linker, "/STACK:10240000,10240000")
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
typedef unsigned long long ull;
const ull hash1 = 201326611;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
//ull ha[MAX_N],pp[MAX_N];
inline int read()
{
int date = 0,m = 1; char ch = 0;
while(ch!='-'&&(ch<'0'|ch>'9'))ch = getchar();
if(ch=='-'){m = -1; ch = getchar();}
while(ch>='0' && ch<='9')
{
date = date*10+ch-'0';
ch = getchar();
}return date*m;
}
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
/*int root[MAX_N],cnt,sz;
namespace hjt
{
#define mid ((l+r)>>1)
struct node{int l,r,maxx;}T[MAX_N*40];
#undef mid
}*/
const int MAX_N = 20025;
int p[MAX_N],eid,T[5],Size[MAX_N],maxson[MAX_N],SIZE,root,ms,dep[MAX_N],ans;
bool vis[MAX_N];
void init()
{
memset(p,-1,sizeof(p));
eid = 0;
}
struct edge
{
int v,next,dis;
}e[MAX_N<<1];
void add(int u,int v,int dis)
{
e[eid].v = v;
e[eid].dis = dis;
e[eid].next = p[u];
p[u] = eid++;
}
void getroot(int x,int fa)
{
Size[x] = 1;maxson[x] = 0;
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;
if(v==fa||vis[v]) continue;
getroot(v,x);
Size[x]+=Size[v];
if(Size[x]>maxson[x]) maxson[x] = Size[v];
}
if(SIZE-Size[x]>maxson[x]) maxson[x] = SIZE - Size[x];
if(ms>maxson[x]) ms = maxson[x],root = x;
}
void getdis(int x,int fa)
{
dep[x]%=3;
T[dep[x]]++;
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;
if(v==fa||vis[v]) continue;
dep[v] = (dep[x]+e[i].dis)%3;
getdis(v,x);
}
}
int solve(int x,int dis)
{
dep[x] = dis;T[0] = T[1] = T[2] = 0;getdis(x,-1);
return T[0]*T[0]+T[1]*T[2]*2;
}
void fenzhi(int x,int ssize)
{
vis[x] = true;
ans+=solve(x,0);
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;
if(vis[v]) continue;
ans-=solve(v,e[i].dis);
ms = inf;root = 0;
SIZE = Size[v]<Size[x]?Size[v]:(ssize - Size[x]);
getroot(v,0);
fenzhi(root,(Size[v]<Size[x]?Size[v]:(ssize-Size[x])));
}
}
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n,a,b,c;
scanf("%d",&n);
init();
for(int i = 1;i<n;++i)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);add(b,a,c);
}
ms = inf;root = 0;SIZE = n;
getroot(1,-1);
fenzhi(root,n);
int down = n*n;
int GCD = gcd(ans,down);
printf("%d/%d",ans/GCD,down/GCD);
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
P4149
只要加边的时候赋值
删边的时候赋无穷大(1条边肯定比组合起来更优
所以可以直接删
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
//#pragma comment(linker, "/STACK:10240000,10240000")
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
typedef unsigned long long ull;
const ull hash1 = 201326611;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
//ull ha[MAX_N],pp[MAX_N];
inline int read()
{
int date = 0,m = 1; char ch = 0;
while(ch!='-'&&(ch<'0'|ch>'9'))ch = getchar();
if(ch=='-'){m = -1; ch = getchar();}
while(ch>='0' && ch<='9')
{
date = date*10+ch-'0';
ch = getchar();
}return date*m;
}
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
/*int root[MAX_N],cnt,sz;
namespace hjt
{
#define mid ((l+r)>>1)
struct node{int l,r,maxx;}T[MAX_N*40];
#undef mid
}*/
const int MAX_N = 200025;
int p[MAX_N],eid,k,dep[MAX_N],Size[MAX_N],SIZE,ms,root,maxson[MAX_N],ans;
bool vis[MAX_N];
int cnt[1000025];
void init()
{
memset(p,-1,sizeof(p));
eid = 0;
}
struct edge
{
int v,next,dis;
}e[MAX_N<<1];
void add(int u,int v,int dis)
{
e[eid].v = v;
e[eid].next = p[u];
e[eid].dis = dis;
p[u] = eid++;
}
void getroot(int x,int fa)
{
Size[x] = 1;maxson[x] = 0;
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;
if(v==fa||vis[v]) continue;
getroot(v,x);
Size[x]+=Size[v];
if(Size[v]>maxson[x]) maxson[x] = Size[v];
}
if(SIZE-Size[x]>maxson[x]) maxson[x] = SIZE-Size[x];
if(ms>maxson[x]) ms = maxson[x],root = x;
}
void getans(int x,int fa,int dis,int now)
{
if(dis>k) return;
ans = min(ans,cnt[k-dis]+now);
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;
if(v==fa||vis[v]) continue;
getans(v,x,dis+e[i].dis,now+1);
}
}
void update(int x,int fa,int dis,int now)
{
if(dis>k) return;
cnt[dis] = min(cnt[dis],now);
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;
if(v==fa||vis[v]) continue;
update(v,x,dis+e[i].dis,now+1);
}
}
void Clear(int x,int fa,int dis)
{
if(dis>k) return;
cnt[dis] = 1e9;
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;
if(v==fa||vis[v]) continue;
Clear(v,x,dis+e[i].dis);
}
}
void fenzhi(int x,int ssize)
{
cnt[0] = 0;
vis[x] = true;
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;
if(vis[v]) continue;
getans(v,x,e[i].dis,1);
update(v,x,e[i].dis,1);
}
Clear(x,0,0);
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;if(vis[v]) continue;
ms = inf;root = 0;
SIZE = Size[v]<Size[x]?Size[v]:(ssize-Size[x]);
getroot(v,0);
fenzhi(root,(Size[v]<Size[x]?Size[v]:(ssize-Size[x])));
}
}
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n,a,b,c;
scanf("%d%d",&n,&k);
init();
ans = 1e9;
for(int i = 1;i<n;++i)
{
scanf("%d%d%d",&a,&b,&c);a++,b++;
add(a,b,c);add(b,a,c);
}
for(int i = 0;i<=k;++i) cnt[i] = 1e9;
ms = inf;root = 0;
SIZE = n;
getroot(1,-1);
fenzhi(root,n);
if(ans>=1e9) printf("-1\n");
else printf("%d\n",ans);
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
cf293E
发现长度不大 可以对长度建树状数组 每次在区间内查询满足的个数即可
//现在有一棵树,每条边的长度都为1,然后有一个权值,求存在多少个(u,v)点对,他们的路劲长度 <= l, 总权重 <= w.
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
//#pragma comment(linker, "/STACK:10240000,10240000")
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
typedef unsigned long long ull;
const ull hash1 = 201326611;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
//ull ha[MAX_N],pp[MAX_N];
inline int read()
{
int date = 0,m = 1; char ch = 0;
while(ch!='-'&&(ch<'0'|ch>'9'))ch = getchar();
if(ch=='-'){m = -1; ch = getchar();}
while(ch>='0' && ch<='9')
{
date = date*10+ch-'0';
ch = getchar();
}return date*m;
}
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
/*int root[MAX_N],cnt,sz;
namespace hjt
{
#define mid ((l+r)>>1)
struct node{int l,r,maxx;}T[MAX_N*40];
#undef mid
}*/
const int MAX_N = 100025;
int C[MAX_N<<1];
void ADD(int x,int v)
{
for(;x<MAX_N;x+=x&(-x))
C[x]+=v;
}
int getsum(int x)
{
int res = 0;
if(x<=0) return 0;
for(;x;x-=x&(-x))
res+=C[x];
return res;
}
int n,l,W,p[MAX_N],eid,Size[MAX_N],SIZE,ms,root,maxson[MAX_N],dep[MAX_N],weight[MAX_N];
ll ans;
bool vis[MAX_N];
void init()
{
memset(p,-1,sizeof(p));
eid = 0;
}
struct edge
{
int v,next,dis,w;
}e[MAX_N<<1];
struct node
{
int a,b;
bool operator < (const node other) const
{
return b < other.b;
}
}now[MAX_N];
void add(int u,int v,int dis,int w)
{
e[eid].v = v;
e[eid].next = p[u];
e[eid].dis = dis;
e[eid].w = w;
p[u] = eid++;
}
void getroot(int x,int fa)
{
Size[x] = 1;maxson[x] = 0;
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;
if(vis[v]||v==fa) continue;
getroot(v,x);
Size[x]+=Size[v];
if(Size[v]>maxson[x]) maxson[x] = Size[v];
}
if(SIZE-Size[x]>maxson[x]) maxson[x] = SIZE - Size[x];
if(ms>maxson[x]) ms = maxson[x],root = x;
}
void getdis(int x,int fa)
{
now[++now[0].a].a = dep[x];now[now[0].a].b = weight[x];
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;
if(v==fa||vis[v]) continue;
dep[v] = dep[x] + 1;
weight[v] = weight[x] + e[i].w;
getdis(v,x);
}
}
ll solve(int x,int dis,int w)
{
dep[x] = dis;weight[x] = w;now[0].a = 0;getdis(x,-1);
sort(now+1,now+1+now[0].a);
int L = 1,R = now[0].a;
ll sum = 0;
for(int i = 1;i<=now[0].a;++i) ADD(now[i].a+1,1);
while(L<R)
{
if(now[L].b+now[R].b<=W) ADD(now[L].a+1,-1),sum+=1ll*getsum(l-now[L].a+1),L++;
else ADD(now[R].a+1,-1),R--;
}
ADD(now[L].a+1,-1);
return sum;
}
void fenzhi(int x,int ssize)
{
vis[x] = true;
ans+=solve(x,0,0);
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;
if(vis[v]) continue;
ans-=solve(v,e[i].dis,e[i].w);
ms = inf;root = 0;
SIZE = Size[v]<Size[x]?Size[v]:(ssize-Size[x]);
getroot(v,0);
fenzhi(root,(Size[v]<Size[x]?Size[v]:(ssize-Size[x])));
}
}
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int a,b;
scanf("%d%d%d",&n,&l,&W);
init();
for(int i = 2;i<=n;++i)
{
scanf("%d%d",&a,&b);
add(a,i,1,b);add(i,a,1,b);
}
ms = inf;root = 0;SIZE = n;
getroot(1,-1);
fenzhi(root,SIZE);
printf("%lld\n",ans);
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
CF161D
统计路径和为k的个数
暴力统计即可 注意我们不想要子树当前内的和 所以用一个tmp和一个num数组
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
//#pragma comment(linker, "/STACK:10240000,10240000")
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
typedef unsigned long long ull;
const ull hash1 = 201326611;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
//ull ha[MAX_N],pp[MAX_N];
inline int read()
{
int date = 0,m = 1; char ch = 0;
while(ch!='-'&&(ch<'0'|ch>'9'))ch = getchar();
if(ch=='-'){m = -1; ch = getchar();}
while(ch>='0' && ch<='9')
{
date = date*10+ch-'0';
ch = getchar();
}return date*m;
}
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
/*int root[MAX_N],cnt,sz;
namespace hjt
{
#define mid ((l+r)>>1)
struct node{int l,r,maxx;}T[MAX_N*40];
#undef mid
}*/
const int MAX_N = 50025;
int n,k,p[MAX_N],eid,maxson[MAX_N],dep[MAX_N],root,ms,SIZE,Size[MAX_N],ans;
bool vis[MAX_N];
int num[505],tmp[505];
struct edge
{
int v,next,dis;
}e[MAX_N<<1];
void add(int u,int v,int dis)
{
e[eid].dis = dis;
e[eid].next = p[u];
e[eid].v = v;
p[u] = eid++;
}
void init()
{
memset(p,-1,sizeof(p));
eid = 0;
}
void getroot(int x,int fa)
{
Size[x] = 1;maxson[x] = 0;
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;
if(vis[v]||v==fa) continue;
getroot(v,x);
Size[x]+=Size[v];
if(Size[v]>maxson[x]) maxson[x] = Size[v];
}
if(SIZE-Size[x]>maxson[x]) maxson[x] = SIZE - Size[x];
if(ms>maxson[x]) ms = maxson[x],root = x;
}
void getdis(int x,int fa)
{
if(dep[x]>k) return;
ans+=num[k-dep[x]];++tmp[dep[x]];
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;
if(v==fa||vis[v]) continue;
dep[v] = dep[x] + e[i].dis;
getdis(v,x);
}
}
void fenzhi(int x,int ssize)
{
vis[x] = true;
for(int i = 1;i<=k;++i) num[i] = 0;num[0] = 1;
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;
if(vis[v]) continue;
for(int j = 0;j<=k;++j) tmp[j] = 0;
dep[v] = 1;
getdis(v,x);
for(int j = 0;j<=k;++j) num[j] += tmp[j];
}
for(int i = p[x];i+1;i=e[i].next)
{
int v = e[i].v;
if(vis[v]) continue;
ms = inf;root = 0;
SIZE = Size[v]<Size[x]?Size[v]:(ssize-Size[x]);
getroot(v,0);
fenzhi(root,(Size[v]<Size[x]?Size[v]:(ssize-Size[x])));
}
}
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int a,b;
scanf("%d%d",&n,&k);
init();
for(int i = 1;i<n;++i)
{
scanf("%d%d",&a,&b);
add(a,b,1);add(b,a,1);
}
ms = inf;root = 0;SIZE = n;
getroot(1,-1);
fenzhi(root,SIZE);
printf("%d\n",ans);
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}