BZOJ 4326 树链剖分+二分+差分+记忆化

去年NOIP的时候我还不会树链剖分!

还是被UOJ 的数据卡了一组。

差分的思想还是很神啊!

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <ctime>
#include <cstdlib>
using namespace std;
const int Maxn=;
const int Inf=0x3f3f3f3f;
int n,m,u,v,w,cnt,tot,MaxLen,MinLen,Minw,Maxw,Ans;
int head[Maxn],dep[Maxn],siz[Maxn],father[Maxn],W[Maxn],p[Maxn],q[Maxn],dis[Maxn],Len[Maxn],c[Maxn],top[Maxn],Memory[Maxn],Root;
struct Edge{int to,next,w;}edge[Maxn<<];
inline void Get_Int(int &x)
{
x=; char ch=getchar(); int f=;
while (ch<'' || ch>'') {if (ch=='-') f=-; ch=getchar();}
while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();} x*=f;
}
inline void Put_Int(int x)
{
char ch[]; int top=;
if (x==) ch[++top]='';
while (x) ch[++top]=x%+'',x/=;
while (top) putchar(ch[top--]); putchar('\n');
}
inline int Max(int x,int y) {return x>y?x:y;}
inline int Min(int x,int y) {return x>y?y:x;}
inline void Add(int u,int v,int w)
{edge[cnt].to=v;edge[cnt].next=head[u];edge[cnt].w=w;head[u]=cnt++;}
//==============================================
void Dfs1(int u,int fa)
{
siz[u]=;
for (int i=head[u];i!=-;i=edge[i].next)
if (edge[i].to!=fa)
{
dep[edge[i].to]=dep[u]+;
father[edge[i].to]=u;
dis[edge[i].to]=dis[u]+edge[i].w;
W[edge[i].to]=edge[i].w;
Dfs1(edge[i].to,u);
siz[u]+=siz[edge[i].to];
}
}
void Dfs2(int u,int chain,int fa)
{
top[u]=chain; int k=;
for (int i=head[u];i!=-;i=edge[i].next)
if (edge[i].to!=fa && (siz[edge[i].to]>siz[k] || k==)) k=edge[i].to;
if (k==) return;
Dfs2(k,chain,u);
for (int i=head[u];i!=-;i=edge[i].next)
if (edge[i].to!=fa && k!=edge[i].to) Dfs2(edge[i].to,edge[i].to,u);
}
inline int Lca(int u,int v)
{
while (true)
{
if (top[u]==top[v]) return dep[u]>dep[v]?v:u;
if (dep[top[u]]>dep[top[v]]) u=father[top[u]]; else v=father[top[v]];
}
}
int Get(int u,int f)
{
for (int i=head[u];i!=-;i=edge[i].next)
if (f!=edge[i].to)
c[u]+=Get(edge[i].to,u);
if (c[u]==tot) MinLen=Max(MinLen,W[u]);
return c[u];
}
inline bool Check(int t)
{
MaxLen=MinLen=tot=;
memset(c,,sizeof(c));
for (int i=;i<=m;i++)
if (Len[i]>t)
{
c[p[i]]++,c[q[i]]++;
c[Lca(p[i],q[i])]-=;
MaxLen=Max(MaxLen,Len[i]);
tot++;
}
if (Memory[tot])
{
if (Memory[tot]<=t) return true;
return false;
}
Get(Root,Root);
Memory[tot]=(MaxLen-MinLen);
if (Memory[tot]<=t) return true;
return false;
}
int main()
{
srand(time());
Get_Int(n),Get_Int(m);
memset(head,-,sizeof(head)); Minw=Inf;
for (int i=;i<n;i++)
{
Get_Int(u),Get_Int(v),Get_Int(w);
Add(u,v,w),Add(v,u,w);
Minw=Min(Minw,w);
}
for (int i=;i<=m;i++) Get_Int(p[i]),Get_Int(q[i]);
Root=rand()%n+;
father[Root]=Root; dep[Root]=; dis[Root]=;
Dfs1(Root,Root);
Dfs2(Root,Root,Root);
memset(Memory,,sizeof(Memory));
for (int i=;i<=m;i++) Len[i]=dis[p[i]]+dis[q[i]]-*dis[Lca(p[i],q[i])],Maxw=Max(Maxw,Len[i]);
int l=Minw,r=Maxw;
while (l<=r)
{
int mid=(l+r)>>;
if (Check(mid)) Ans=mid,r=mid-; else l=mid+; }
Put_Int(Ans);
return ;
}

C++

上一篇:cocos2dx day 3 - Chapter5 Scene


下一篇:RabbitMQ 通信过程