POJ3162 解题报告

Walking Race
Description
flymouse’s sister wc is very capable at sports and her favorite event is walking race. Chasing after the championship in an important competition, she comes to a training center to attend a training course. The center has N check-points numbered 1 through N. Some pairs of check-points are directly connected by two-way paths. The check-points and the paths form exactly a tree-like structure. The course lasts N days. On the i-th day, wc picks check-point i as the starting point and chooses another check-point as the finishing point and walks along the only simple path between the two points for the day’s training. Her choice of finishing point will make it that the resulting path will be the longest among those of all possible choices.

After every day’s training, flymouse will do a physical examination from which data will obtained and analyzed to help wc’s future training be better instructed. In order to make the results reliable, flymouse is not using data all from N days for analysis. flymouse’s model for analysis requires data from a series of consecutive days during which the difference between the longest and the shortest distances wc walks cannot exceed a bound M. The longer the series is, the more accurate the results are. flymouse wants to know the number of days in such a longest series. Can you do the job for him?

Input
The input contains a single test case. The test case starts with a line containing the integers N (N ≤ 106) and M (M < 109). Then follow N − 1 lines, each containing two integers fi and di (i = 1, 2, …, N − 1), meaning the check-points i + 1 and fi are connected by a path of length di.

Output
Output one line with only the desired number of days in the longest series.

Sample Input
3 2
1 1
1 3
Sample Output
3
Hint
Explanation for the sample:

There are three check-points. Two paths of lengths 1 and 3 connect check-points 2 and 3 to check-point 1. The three paths along with wc walks are 1-3, 2-1-3 and 3-1-2. And their lengths are 3, 4 and 4. Therefore data from all three days can be used for analysis.

题意:
给你一棵树,直接相连的两个节点的边有权值,求出第1,2,3,4,…,n个节点的最长距离。d[1],d[2],…,d[n];
然后你要找到最大的r - l,使得这个[l, r]区间的最大值减去最小值 < k;
第一个很好求,dfs两次求出直径端点,最后在对最后求出的端点做一次dfs,每次dfs的时候同时更新d[u]。
第二个,要么用线段树优化,要么用单调队列优化,分别维护区间的最大和最小值,然后滑动窗口。
具体见代码:

//
//  Created by Running Photon on 2015-09-08
//  Copyright (c) 2015 Running Photon. All rights reserved.
//
//
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <sstream>
#include <set>
#include <vector>
#include <stack>
#define ALL(x) x.begin(), x.end()
#define INS(x) inserter(x, x,begin())
#define ll long long
#define CLR(x) memset(x, 0, sizeof x)
#define MAXN 9999
#define MAXSIZE 10
#define DLEN 4
using namespace std;
const int inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int maxn = 2e6 + 10;
const int maxv = 1e6 + 10;
const double eps = 1e-9;


inline int read() {
    char c = getchar();
    int f = 1;
    while(!isdigit(c)) {
    if(c == '-') f = -1;
    c = getchar();
    }
    int x = 0;
    while(isdigit(c)) {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}
int n, k;
int head[maxv], pnt[maxn], cnt, nxt[maxn], cost[maxn];
int dis[maxv], root1, root2, d[maxv];
void add_edge(int u, int v, int val) {
    pnt[cnt] = v;
    cost[cnt] = val;
    nxt[cnt] = head[u];
    head[u] = cnt++;
}
void findRoot(int u, int fa, int &root) {
    for(int i = head[u]; ~i; i = nxt[i]) {
        int v = pnt[i];
        if(v == fa) continue;
        dis[v] = dis[u] + cost[i];
        findRoot(v, u, root);
    }
    if(dis[u] > dis[root]) {
        root = u;
    }
    d[u] = max(d[u], dis[u]);//dfs的同时更新d
}
void work() {
    root1 = 1, root2 = 1;
    findRoot(1, 1, root1);//找到根1
    dis[root1] = 0;
    findRoot(root1, root1, root2);找到根2
    dis[root2] = 0;
    findRoot(root2, root2, root1);//最后再对根2进行dfs
}
void sol() {
    int ans = 0;
    deque <int> dequemax, dequemin;
    int l = 1, r = 1;
    for(; r <= n; r++) {
        while(dequemax.size() && d[r] >= d[dequemax.back()]) {
            dequemax.pop_back();
        }
        dequemax.push_back(r);
        while(dequemin.size() && d[r] <= d[dequemin.back()]) {
            dequemin.pop_back();
        }
        dequemin.push_back(r);
        if(d[dequemax[0]] - d[dequemin[0]] > k) {
            while(d[dequemax[0]] - d[dequemin[0]] > k) {
                l = min(dequemax[0], dequemin[0]) + 1;
                while(dequemax[0] < l) dequemax.pop_front();
                while(dequemin[0] < l) dequemin.pop_front();
            }
            ans = max(ans, r - l + 1);
        }
        ans = max(ans, r - l + 1);
    }
    printf("%d\n", ans);
}
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
//  freopen("out.txt","w",stdout);
#endif
    while(scanf("%d%d", &n, &k) != EOF) {
        cnt = 0;
        CLR(d);
        memset(head, -1, sizeof head);
        for(int i = 2; i <= n; i++) {
            int u = read(), val = read();
            add_edge(i, u, val);
            add_edge(u, i, val);
        }
        work();
        sol();
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ1753题目为"Flip Game",题目给出了一个4x4的棋盘,每个格子有黑色或白色,每次翻转一个格子会同时翻转它上下左右四个格子的颜色,目标是把整个棋盘都变为同一种颜色,求把棋盘变成同种颜色的最小步数。 解题思路: 一般关于棋盘变色的题目,可以考虑使用搜索来解决。对于POJ1753题目,可以使用广度优先搜索(BFS)来解决。 首先,对于每个格子,定义一个状态,0表示当前格子是白色,1表示当前格子是黑色。 然后,我们可以把棋盘抽象成一个长度为16的二进制数,将所有格子的状态按照从左往右,从上往下的顺序排列,就可以用一个16位的二进制数表示整个棋盘的状态。例如,一个棋盘状态为: 0101 1010 0101 1010 则按照从左往右,从上往下的顺序把所有格子的状态连接起来,即可得到该棋盘的状态为"0101101001011010"。 接着,我们可以使用队列来实现广度优先搜索。首先将初始状态加入队列中,然后对于队列中的每一个状态,我们都尝试将棋盘上的每个格子翻转一次,生成一个新状态,将新状态加入队列中。对于每一个新状态,我们也需要记录它是从哪个状态翻转得到的,以便在得到最终状态时能够输出路径。 在搜索过程中,我们需要维护每个状态离初始状态的步数,即将该状态转换为最终状态需要的最小步数。如果我们找到了最终状态,就可以输出答案,即最小步数。 代码实现:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值