【BZOJ-2599】Race 点分治

2599: [IOI2011]Race

Time Limit: 70 Sec  Memory Limit: 128 MB
Submit:
2590  Solved: 769
[Submit][Status][Discuss]

Description

给一棵树,每条边有权.求一条简单路径,权值和等于K,且边的数量最小.N <= 200000, K <= 1000000

Input

第一行 两个整数 n, k
第二..n行 每行三个整数 表示一条无向边的两端和权值 (注意点的编号从0开始)

Output

一个整数 表示最小边数量 如果不存在这样的路径 输出-1

Sample Input

4 3
0 1 1
1 2 2
1 3
4

Sample Output

2

HINT

$N<=200000,K<=1000000$

Source

Solution

树的点分治裸题,但是做法有点有趣

开始考虑的是:

正常的点分,然后每次用当前根开始BFS子树,以获得子树中每个节点到根的边权和和路径长度,并利用之前的结果和当前结果更新答案

不过貌似写挂了50s左右炸掉- -

发现其实不需要,只需要开一个100W的数组来记录 当前到根的距离为$i$的路径长度最短值

这样就可以不断对子树进行更新和对答案进行更新了

时间复杂度依旧是$O(nlogn)$

Code

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
int read()
{
    int x=0,f=1; char ch=getchar();
    while (ch<‘0‘ || ch>‘9‘) {if (ch==‘-‘) f=-1; ch=getchar();}
    while (ch>=‘0‘ && ch<=‘9‘) {x=x*10+ch-‘0‘; ch=getchar();}
    return x*f;
}
#define maxn 200010
#define maxk 1000010
#define inf 0x3f3f3f3f
int n,K;
struct EdgeNode{int next,to,val;}edge[maxn<<1];
int head[maxn],cnt=1;
void add(int u,int v,int w) {cnt++; edge[cnt].next=head[u]; head[u]=cnt; edge[cnt].to=v; edge[cnt].val=w;}
void insert(int u,int v,int w) {add(u,v,w); add(v,u,w);}
int size[maxn],maxx[maxn],num[maxk],siz,root,ans;
bool visit[maxn];
void Getroot(int now,int last)
{
    size[now]=1; maxx[now]=0;
    for (int i=head[now]; i; i=edge[i].next)
        if (edge[i].to!=last && !visit[edge[i].to])
            {
                Getroot(edge[i].to,now);
                size[now]+=size[edge[i].to];
                maxx[now]=max(maxx[now],size[edge[i].to]);
            }
    maxx[now]=max(maxx[now],siz-size[now]);
    if (maxx[now]<maxx[root]) root=now;
}
struct Node{int d,t;}st[maxn];
int s,top;
void DFS(int now,int dis,int tt,int last)
{
    if (dis>K) return;
    ans=min(ans,tt+num[K-dis]);
    st[top++]=Node{dis,tt};
    for (int i=head[now]; i; i=edge[i].next)
        if (edge[i].to!=last && !visit[edge[i].to])
            DFS(edge[i].to,dis+edge[i].val,tt+1,now);
}
void Solve(int now)
{
    root=0; Getroot(now,0); now=root;
    s=0,top=0;
    for (int i=head[now]; i; i=edge[i].next)
        if (!visit[edge[i].to])
            {
                DFS(edge[i].to,edge[i].val,1,now);
                for (int j=s; j<=top-1; j++)
                    num[st[j].d]=min(num[st[j].d],st[j].t);
                s=top;
            }
    for (int i=0; i<=top-1; i++) num[st[i].d]=inf; num[0]=0;
    visit[now]=1;
    for (int i=head[now]; i; i=edge[i].next)
        if (!visit[edge[i].to])
            siz=size[edge[i].to],Solve(edge[i].to);
}
int main()
{
    n=read(); K=read();
    for (int u,v,w,i=1; i<=n-1; i++) u=read()+1,v=read()+1,w=read(),insert(u,v,w);
    memset(num,inf,sizeof(num)); num[0]=0;
    ans=inf;
    siz=maxx[0]=n; Solve(1);
    if (ans!=inf) {printf("%d\n",ans); return 0;}
    puts("-1");
    return 0;
}

AC第一道IOI   ovo!

时间: 2024-08-24 23:22:12

【BZOJ-2599】Race 点分治的相关文章

BZOJ 2599 Race(树分治)

题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2599 题意:给一棵树,每条边有权.求一条路径,权值和等于K,且边的数量最小. 题意:每次找到当前树的重心作为树根,查找通过当前树根的路径. 1 #include<algorithm> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<iostream>

BZOJ 2599 Race

点分写挂调一上午. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<set> #define maxv 200050 #define maxe 400050 #define maxk 1000050 #define inf 1000000007 using namespace std; int n,k,x,y,w,g[maxv],

BZOJ 2599: [IOI2011]Race( 点分治 )

数据范围是N:20w, K100w. 点分治, 我们只需考虑经过当前树根的方案. K最大只有100w, 直接开个数组CNT[x]表示与当前树根距离为x的最少边数, 然后就可以对根的子树依次dfs并更新CNT数组和答案. ------------------------------------------------------------------------------------------ #include<bits/stdc++.h> using namespace std; typ

bzoj 2599 [IOI2011]Race (点分治)

[题意] 问树中长为k的路径中包含边数最少的路径所包含的边数. [思路] 统计经过根的路径.假设当前枚举到根的第S个子树,若x属于S子树,则有: ans<-dep[x]+min{ dep[y] },y属于前S-1个子树,dis[x]<=K 所以只需要用一个数组t[len]记录前S-1棵子树中长度为len的最少边数即可.t只用开到K的最大值. 然后分治处理子树. [代码] 1 #include<set> 2 #include<cmath> 3 #include<qu

bzoj 2599 [IOI2011]Race 未调完_点分治

Code: // luogu-judger-enable-o2 // luogu-judger-enable-o2 #include <bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) #define maxn 1000000 #define inf 0x7f7f7f using namespace std; int hd[maxn],to[maxn],nex[maxn],val[maxn

bzoj 2599(点分治)

2599: [IOI2011]Race Time Limit: 70 Sec  Memory Limit: 128 MBSubmit: 3642  Solved: 1081[Submit][Status][Discuss] Description 给一棵树,每条边有权.求一条简单路径,权值和等于K,且边的数量最小.N <= 200000, K <= 1000000 Input 第一行 两个整数 n, k第二..n行 每行三个整数 表示一条无向边的两端和权值 (注意点的编号从0开始) Outpu

【刷题】BZOJ 2599 [IOI2011]Race

Description 给一棵树,每条边有权.求一条简单路径,权值和等于K,且边的数量最小.N <= 200000, K <= 1000000 Input 第一行 两个整数 n, k 第二..n行 每行三个整数 表示一条无向边的两端和权值 (注意点的编号从0开始) Output 一个整数 表示最小边数量 如果不存在这样的路径 输出-1 Sample Input 4 3 0 1 1 1 2 2 1 3 4 Sample Output 2 Solution 点分治 考虑如何计算答案,有一个节点,我

BZOJ 3262: 陌上花开 [CDQ分治 三维偏序]

Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要美丽,当且仅当Sa>=Sb,Ca>=Cb,Ma>=Mb.显然,两朵花可能有同样的属性.需要统计出评出每个等级的花的数量. Input 第一行为N,K (1 <= N <= 100,000, 1 <= K <= 200,000 ), 分别表示花的数量和最大属性值. 以下N行,每

bzoj 3672 购票 点分治+dp

3672: [Noi2014]购票 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 1177  Solved: 562[Submit][Status][Discuss] Description 今年夏天,NOI在SZ市迎来了她30周岁的生日.来自全国 n 个城市的OIer们都会从各地出发,到SZ市参加这次盛会. 全国的城市构成了一棵以SZ市为根的有根树,每个城市与它的父亲用道路连接.为了方便起见,我们将全国的 n 个城市用 1 到 n 的整数编号.