1. 程式人生 > >【刷題】洛谷 P3806【模板】點分治1

【刷題】洛谷 P3806【模板】點分治1

source getc deep data str oot root its ||

題目背景

感謝hzwer的點分治互測。

題目描述

給定一棵有n個點的樹

詢問樹上距離為k的點對是否存在。

輸入輸出格式

輸入格式:

n,m 接下來n-1條邊a,b,c描述a到b有一條長度為c的路徑

接下來m行每行詢問一個K

輸出格式:

對於每個K每行輸出一個答案,存在輸出“AYE”,否則輸出”NAY”(不包含引號)

輸入輸出樣例

輸入樣例#1:

2 1
1 2 2
2

輸出樣例#1:

AYE

說明

對於30%的數據n<=100

對於60%的數據n<=1000,m<=50

對於100%的數據n<=10000,m<=100,c<=1000,K<=10000000

題解

還是有點裸的點分治模板題
有趣的一點是:其實calc裏跑 \(O(n^2)\)

的暴力記錄哪些值可以達到,這可以過(數據水還是復雜度玄學?
如果一定要理論復雜度正確,那就不 \(n^2\) ,每次遍歷每一個詢問,兩次twopoints求小於等於 \(q[i]\) 的有多少對,小於等於 \(q[i]-1\) 的有多少對,相減就是等於 \(q[i]\) 的對數了
兩個程序我都寫了, calc裏 \(n^2\) 最慢的要200多ms,\(mlogn\)的最慢只要30ms,畢竟復雜度擺在那裏
兩份代碼都貼上來
這是暴力一點的

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double #define ull unsigned long long const int MAXN=10000+10,MAXK=10000000+10,inf=0x3f3f3f3f; int ans[MAXK],n,m,e,to[MAXN<<1],nex[MAXN<<1],beg[MAXN],w[MAXN<<1],root,f[MAXN],d[MAXN],deep[MAXN],cnt,size[MAXN],Msonsize[MAXN],finish[MAXN]; template<typename T> inline void read(T &x) { T data=0
,w=1; char ch=0; while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar(); if(ch=='-')w=-1,ch=getchar(); while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar(); x=data*w; } template<typename T> inline void write(T x,char ch='\0') { if(x<0)putchar('-'),x=-x; if(x>9)write(x/10); putchar(x%10+'0'); if(ch!='\0')putchar(ch); } template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);} template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);} template<typename T> inline T min(T x,T y){return x<y?x:y;} template<typename T> inline T max(T x,T y){return x>y?x:y;} inline void insert(int x,int y,int z) { to[++e]=y; nex[e]=beg[x]; beg[x]=e; w[e]=z; } inline void getroot(int x,int f,int ntotal) { Msonsize[x]=0;size[x]=1; for(register int i=beg[x];i;i=nex[i]) if(to[i]==f||finish[to[i]])continue; else { getroot(to[i],x,ntotal); size[x]+=size[to[i]]; chkmax(Msonsize[x],size[to[i]]); } chkmax(Msonsize[x],ntotal-size[x]); if(Msonsize[x]<Msonsize[root])root=x; } inline void getdeep(int x,int f) { deep[++cnt]=d[x]; for(register int i=beg[x];i;i=nex[i]) if(to[i]==f||finish[to[i]])continue; else d[to[i]]=d[x]+w[i],getdeep(to[i],x); } inline void calc(int x,int st,int v) { d[x]=st;cnt=0; getdeep(x,0); for(register int i=1;i<=cnt;++i) for(register int j=i+1;j<=cnt;++j) if(deep[i]+deep[j]<=1e7)ans[deep[i]+deep[j]]+=v; } inline void solve(int x) { calc(x,0,1); finish[x]=1; for(register int i=beg[x];i;i=nex[i]) if(!finish[to[i]]) { calc(to[i],w[i],-1); root=0; getroot(to[i],x,size[to[i]]); solve(to[i]); } } int main() { read(n);read(m); for(register int i=1;i<n;++i) { int u,v,w; read(u);read(v);read(w); insert(u,v,w);insert(v,u,w); } Msonsize[0]=inf;root=0; getroot(1,0,n); solve(1); for(register int i=1;i<=m;++i) { int x; read(x); if(ans[x])puts("AYE"); else puts("NAY"); } return 0; }

這是優秀的(ShichengXiao OrzOrzOrzOrz)

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=10000+10,MAXM=100+10,MAXK=10000000+10,inf=0x3f3f3f3f;
int n,m,e,to[MAXN<<1],nex[MAXN<<1],beg[MAXN],w[MAXN<<1],root,f[MAXN],d[MAXN],deep[MAXN],cnt,size[MAXN],Msonsize[MAXN],finish[MAXN],q[MAXM],ans[MAXM];
template<typename T> inline void read(T &x)
{
    T data=0,w=1;
    char ch=0;
    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-')w=-1,ch=getchar();
    while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
    x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)write(x/10);
    putchar(x%10+'0');
    if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void insert(int x,int y,int z)
{
    to[++e]=y;
    nex[e]=beg[x];
    beg[x]=e;
    w[e]=z;
}
inline void getroot(int x,int f,int ntotal)
{
    Msonsize[x]=0;size[x]=1;
    for(register int i=beg[x];i;i=nex[i])
        if(to[i]==f||finish[to[i]])continue;
        else
        {
            getroot(to[i],x,ntotal);
            size[x]+=size[to[i]];
            chkmax(Msonsize[x],size[to[i]]);
        }
    chkmax(Msonsize[x],ntotal-size[x]);
    if(Msonsize[x]<Msonsize[root])root=x;
}
inline void getdeep(int x,int f)
{
    deep[++cnt]=d[x];
    for(register int i=beg[x];i;i=nex[i])
        if(to[i]==f||finish[to[i]])continue;
        else d[to[i]]=d[x]+w[i],getdeep(to[i],x);
}
inline void calc(int x,int st,int v)
{
    d[x]=st;cnt=0;
    getdeep(x,0);
    std::sort(deep+1,deep+cnt+1);
    for(register int i=1;i<=m;++i)
    {
        int res1=0,res2=0,l,r;
        l=1,r=cnt;
        while(l<r)
        {
            if(deep[l]+deep[r]<=q[i])res1+=r-l,l++;
            else r--;
        }
        l=1,r=cnt;
        while(l<r)
        {
            if(deep[l]+deep[r]<=q[i]-1)res2+=r-l,l++;
            else r--;
        }
        ans[i]+=(res1-res2)*v;
    }
}
inline void solve(int x)
{
    calc(x,0,1);
    finish[x]=1;
    for(register int i=beg[x];i;i=nex[i])
        if(!finish[to[i]])
        {
            calc(to[i],w[i],-1);
            root=0;
            getroot(to[i],x,size[to[i]]);
            solve(to[i]);
        }
}
int main()
{
    read(n);read(m);
    for(register int i=1;i<n;++i)
    {
        int u,v,w;
        read(u);read(v);read(w);
        insert(u,v,w);insert(v,u,w);
    }
    for(register int i=1;i<=m;++i)read(q[i]);
    Msonsize[0]=inf;root=0;
    getroot(1,0,n);
    solve(1);
    for(register int i=1;i<=m;++i)
        if(ans[i])puts("AYE");
        else puts("NAY");
    return 0;
}

【刷題】洛谷 P3806【模板】點分治1