poj2114 寻找树上存在长度为k点对,树上的分治

时间:2023-01-05 12:38:34

寻找树上存在长度为k点对,树上的分治  代码和  这个  差不多 ,改一下判断的就好

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <string.h>
using namespace std;
const int maxn=;
int H[maxn],nx[maxn*],to[maxn*],numofE,dist[maxn*];
void add(int u,int v, int d)
{
numofE++;
dist[numofE]=d;
to[numofE]=v;
nx[numofE]=H[u];
H[u]=numofE;
}
void init(int N){
numofE=;
memset(H,,sizeof(H));
}
int ans;
int subnum[maxn],Q[maxn+],fa[maxn];
bool center[maxn];
int N,K;
int searchroot(int cur)
{
int rear=,root=cur;
Q[rear++]=cur;fa[cur]=;
for(int i=; i<rear; i++)
{
int x=Q[i];
for(int j=H[x]; j; j=nx[j])
if(to[j]!=fa[x]&&center[to[j]]==false)
Q[rear++]=to[j],fa[to[j]]=x;
}
int MIN=maxn;
for(int i=rear-; i>=; i--)
{
int x=Q[i];
subnum[x]=;
int MA=;
for(int j=H[x]; j ; j=nx[j])
if(to[j]!=fa[x]&&center[to[j]]==false)
MA=max(MA,subnum[to[j]]),subnum[ x ]+=subnum[ to[j] ];
MA=max(MA,rear-subnum[x]);
if(MIN>MA)MIN=MA,root=x;
}
return root;
}
int P[maxn];
int count_pair(int s, int t)
{
int ge=,l=t,r=t;
for(int i=s; i<t; i++)
{
while(r>s&&P[i]+P[r-]>K)r--;
while(l>s&&P[i]+P[l-]>=K)l--;
if(i>=l&&i<r)ge+=r-l-;
else ge+=r-l;
}
return ge/;
}
void updateedg(int s, int cur, int d)
{
int rear=;
Q[rear++]=cur;fa[cur]=;P[s]=d;
for(int i=; i<rear; i++)
{
int x=Q[i];
for(int j=H[x];j; j=nx[j])
{
int tto=to[j];
if(center[tto]||tto==fa[x])continue;
P[s+rear]=P[s+i]+dist[j],Q[rear++]=tto,fa[tto]=x;
}
}
sort(P+s,P+s+rear);
}
int dfs(int s, int cur, int d)
{
int root,tot=;
root=searchroot(cur);
center[root]=true;
for(int i=H[root]; i ;i=nx[i])
{
int tto=to[i];
if(center[tto])continue;
int n=dfs(s+tot,tto,dist[i]);
if(ans>){
center[root]=false;return ;
}
ans-=count_pair(s+tot,s+tot+n);
tot+=n;
}
P[s]=;
sort(P+s,P+s+tot);
ans+=count_pair(s,s+tot);
if(ans>){
center[root]=false;return ;
}
center[root]=false;
updateedg(s,cur,d);
return tot;
}
int main()
{
for(;;)
{
scanf("%d",&N); if(N==)break;
init(N);
for(int i=;i<=N; i++)
{ int d;
for(;;)
{
scanf("%d",&d); if(d==)break;
int c; scanf("%d",&c);
add(i,d,c);add(d,i,c);
}
}
for(;;)
{
scanf("%d",&K);if(K==)break;
ans=;
dfs(,,);
if(ans>)
puts("AYE");
else puts("NAY");
}
puts(".");
}
return ;
}
/*
6
2 1 3 1 4 1 0
0
5 1 6 1 0
0
0
0
2
*/