【刷题】BZOJ 3365 [Usaco2004 Feb]Distance Statistics 路程统计

Description

在得知了自己农场的完整地图后(地图形式如前三题所述),约翰又有了新的问题.他提供

一个整数K(1≤K≤109),希望你输出有多少对农场之间的距离是不超过K的.

Input

第1到I+M行:与前三题相同;

第M+2行:一个整数K.

Output

农场之间的距离不超过K的对数.

Sample Input

7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
10

Sample Output

5

有五对道路之间的距离小于10
1-4,距离为3
4-7,距离为2
1-7,距离为5
3-5,距离为7
3-6,距离为9

Solution

点分治
【刷题】BZOJ 1468 Tree一模一样
题目里虽然是单向的,但是问的是距离,跟方向没关系,直接双向边建上

#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=40000+10,inf=0x3f3f3f3f;
int n,k,e,to[MAXN<<1],nex[MAXN<<1],beg[MAXN],w[MAXN<<1],deep[MAXN],cnt,Msonsize[MAXN],size[MAXN],d[MAXN],root,finish[MAXN],m;
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 int calc(int x,int st)
{
    d[x]=st;cnt=0;
    getdeep(x,0);
    std::sort(deep+1,deep+cnt+1);
    int l=1,r=cnt,ans=0;
    while(l<r)
    {
        if(deep[l]+deep[r]<=k)ans+=r-l,l++;
        else r--;
    }
    return ans;
}
inline int solve(int x)
{
    int res=calc(x,0);
    finish[x]=1;
    for(register int i=beg[x];i;i=nex[i])
        if(!finish[to[i]])
        {
            res-=calc(to[i],w[i]);
            root=0;
            getroot(to[i],x,size[to[i]]);
            res+=solve(root);
        }
    return res;
}
int main()
{
    read(n);read(m);
    for(register int i=1;i<=m;++i)
    {
        int u,v,w;
        char s;
        read(u);read(v);read(w);std::cin>>s;
        insert(u,v,w);insert(v,u,w);
    }
    read(k);
    Msonsize[0]=inf;root=0;
    getroot(1,0,n);
    write(solve(1),'\n');
    return 0;
}

原文地址:https://www.cnblogs.com/hongyj/p/8806559.html

时间: 2024-08-10 09:13:00

【刷题】BZOJ 3365 [Usaco2004 Feb]Distance Statistics 路程统计的相关文章

BZOJ 3365: [Usaco2004 Feb]Distance Statistics 路程统计

Description 一棵树,统计距离不大于 \(k\) 的点对个数. Sol 点分治. 发现自己快把点分治忘干净了... 找重心使所有儿子的最大值尽量小,然后每次处理全部子树,再减去每个子树的贡献,这样就得到子树间的贡献了,然后再搞子树就可以,这就是一个子问题了. Code /************************************************************** Problem: 3365 User: BeiYu Language: C++ Result

bzoj 3365 [Usaco2004 Feb]Distance Statistics 路程统计(点分治,单调)

[题意] 求树上长度不超过k的点对数目. [思路] 和 Tree 一样一样的. 就是最后统计的时候别忘把根加上. [代码] 1 #include<set> 2 #include<cmath> 3 #include<queue> 4 #include<vector> 5 #include<cstdio> 6 #include<cstring> 7 #include<iostream> 8 #include<algori

bzoj 3365: [Usaco2004 Feb]Distance Statistics 路程统计【容斥原理+点分治】

统计在一个root下的两个子树,每个子树都和前面的运算一下再加进去对于这种需要排序的运算很麻烦,所以考虑先不去同子树内点对的算出合法点对个数,然后减去每一棵子树内的合法点对(它们实际上是不合法的,相当于一个容斥) 算点对用排序+双指针即可 #include<iostream> #include<cstdio> #include<algorithm> using namespace std; const int N=1000005; int n,m,h[N],cnt,rt

BZOJ 3364: [Usaco2004 Feb]Distance Queries 距离咨询

Description 一棵树,询问两点间距离. Sol 倍增. 方向没用. 没有然后了. Code /************************************************************** Problem: 3364 User: BeiYu Language: C++ Result: Accepted Time:400 ms Memory:11556 kb **************************************************

BZOJ 3367: [Usaco2004 Feb]The Big Game 球赛( dp )

dp(i)表示前i个人最少坐多少辆车, dp(i) = min(dp(j) + 1, dp(i)) (0 <= j < i 且 (i, j]的人能坐在一辆车上) 时间复杂度O(n²) --------------------------------------------------------------------------- #include<bits/stdc++.h> using namespace std; const int maxn = 2509; int sum

BZOJ 3363: [Usaco2004 Feb]Cow Marathon 奶牛马拉松

Description 给你一个图,两个点至多有一条路径,求最长的一条路径. \(n \leqslant 4\times 10^4\) Sol DFS?DP? 这就是一棵树,方向什么的都没用... 然后记录一下到这个点的最大值和次大值更新答案即可. Code /************************************************************** Problem: 3363 User: BeiYu Language: C++ Result: Accepted

BZOJ 3362: [Usaco2004 Feb]Navigation Nightmare 导航噩梦

Description 给你每个点与相邻点的距离和方向,求两点间的曼哈顿距离. \(n \leqslant 4\times 10^4\) . Sol 加权并查集. 像向量合成一样合并就可以了,找 \(f[x]\) 的时候需要先记录现在的父节点,然后更新他新的父节点. Code /************************************************************** Problem: 3362 User: BeiYu Language: C++ Result:

BZOJ第一页刷题计划

BZOJ第一页刷题计划 已完成:1 / 100 BZOJ1000:A+B

BZOJ 刷题记录 PART 4

[BZOJ1143]CTSC的题目...先用floyed传递闭包,然后直接上匈牙利算法. [BZOJ1452]从未写过的二维树状数组.好像很简单.. struct two_bit { int f[305][305]; inline void add(int x,int z,int A) { for (;x<=n;x+=L(x)) for (int y=z;y<=m;y+=L(y)) f[x][y]+=A; } inline int ask(int x,int z) { int ans=0; f