일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- 마일리지
- persistent segment tree
- Algorithm
- Codeforces
- Ebate USA
- acmicpc
- gallian
- Algebraic Geometry
- 이베이트
- 7469
- BOJ
- 구간쿼리
- k번째 수
- 알고리즘
- 아시아나
- persistent indexed tree
- ccw
- self balancing binary search tree
- algebra
- 백준
- Ebate Korea
- 이베이트코리아
- round 424
- 대수학
- finite group
- indexed tree
- 이베이트미국
- subgroup
- 대한항공
- round 420
- Today
- Total
[Round#425 Div2] D. Misha, Grisha and Underground 본문
http://codeforces.com/contest/832/problem/D
D. Misha, Grisha and Underground
Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations connected with n - 1 routes so that each route connects two stations, and it is possible to reach every station from any other.
The boys decided to have fun and came up with a plan. Namely, in some day in the morning Misha will ride the underground from station sto station f by the shortest path, and will draw with aerosol an ugly text "Misha was here" on every station he will pass through (including sand f). After that on the same day at evening Grisha will ride from station t to station f by the shortest path and will count stations with Misha's text. After that at night the underground workers will wash the texts out, because the underground should be clean.
The boys have already chosen three stations a, b and c for each of several following days, one of them should be station s on that day, another should be station f, and the remaining should be station t. They became interested how they should choose these stations s, f, t so that the number Grisha will count is as large as possible. They asked you for help.
The first line contains two integers n and q (2 ≤ n ≤ 105, 1 ≤ q ≤ 105) — the number of stations and the number of days.
The second line contains n - 1 integers p2, p3, ..., pn (1 ≤ pi ≤ n). The integer pi means that there is a route between stations pi and i. It is guaranteed that it's possible to reach every station from any other.
The next q lines contains three integers a, b and c each (1 ≤ a, b, c ≤ n) — the ids of stations chosen by boys for some day. Note that some of these ids could be same.
Print q lines. In the i-th of these lines print the maximum possible number Grisha can get counting when the stations s, t and f are chosen optimally from the three stations on the i-th day.
3 2
1 1
1 2 3
2 3 3
2
3
4 1
1 2 3
1 2 3
2
In the first example on the first day if s = 1, f = 2, t = 3, Misha would go on the route 1 2, and Grisha would go on the route 3 1 2. He would see the text at the stations 1 and 2. On the second day, if s = 3, f = 2, t = 3, both boys would go on the route 3 1 2. Grisha would see the text at 3 stations.
In the second examle if s = 1, f = 3, t = 2, Misha would go on the route 1 2 3, and Grisha would go on the route 2 3 and would see the text at both stations.
루트가 1이고 N개의 노드로 구성된 트리가 주어진다. 또한 Q개의 쿼리가 주어지는데 각 쿼리는 a,b,c 세개의 수를 입력받는다.
문제를 잘 해석해보면 결국 구하고자 하는 것은
a - b 와 b - c 사이의 경로중 겹치는 노드의 수
a - b 와 a - c 사이의 경로중 겹치는 노드의 수
a - c 와 b - c 사이의 경로중 겹치는 노드의 수
중 최대 노드의 수를 구하는 문제이다.
트리에서 세 점이 주어 졌을 때, a와b, b와c, a와c의 LCA를 구해보면 세개의 LCA중 두개는 반드시 겹친다는 사실을 알 수 있다.
일반성을 잃지않고, a와 c의 LCA와 b와 c의 LCA가 겹친다고 가정하고 이를 p라 하자. 또한 겹치지 않는 나머지 a와 b의 LCA를 q라 하자.
그러면 우리가 구하고자 하는 최종 값은 결국
1. a와 q사이의 노드의 수
2. b와 q사이의 노드의 수
3. p와 q사이의 노드의수 + c와 q사이의 노드의 수 중 큰 값임을 알 수 있다.
따라서 각 쿼리마다 LCA를 logN만에 구해낼 수 있으므로, 최종 시간복잡도는 O(N+QlogN)이다.
'Algorithm > CodeForces' 카테고리의 다른 글
[Round#455 Div2] C. Python Indentation (0) | 2018.01.02 |
---|---|
[Round#450 Div2] C. Remove Extra One (0) | 2017.12.29 |
[Round#424 Div2] E. Cards Sorting (0) | 2017.07.16 |
[Round#424 Div2] C.Jury Marks (0) | 2017.07.15 |
[Round#420 Div.2] E. Okabe and El Psy Kongroo (0) | 2017.06.26 |