POJ3162 문제 보고서

10784 단어 동적 기획
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.
제목: 나무 한 그루를 드리겠습니다. 직접 연결된 두 노드의 가장자리에 권한이 있습니다. 하나, 둘, 셋, 넷..., n개의 노드의 가장 긴 거리를 구하세요.d[1],d[2],…,d[n]; 그리고 최대 r-l를 찾아서 이 [l,r] 구간의 최대 값을 최소 값으로 // // Created by Running Photon on 2015-09-08 // Copyright (c) 2015 Running Photon. All rights reserved. // // //#pragma comment(linker, "/STACK:102400000,102400000") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #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
"
, 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; }

좋은 웹페이지 즐겨찾기