forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
[pull] master from wisdompeak:master #325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
..._Search/3585.Find-Weighted-Median-Node-in-Tree/3585.Find-Weighted-Median-Node-in-Tree.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using ll = long long; | ||
const int MAXN = 100000; | ||
const int LOGN = 17; | ||
|
||
class Solution { | ||
public: | ||
vector<pair<int,int>> adj[MAXN]; | ||
int up[MAXN][LOGN+1]; | ||
int depth[MAXN]; | ||
ll distRoot[MAXN]; | ||
|
||
void dfs(int cur, int parent) | ||
{ | ||
up[cur][0] = parent; | ||
for(auto &[v,w]: adj[cur]) | ||
{ | ||
if(v == parent) continue; | ||
depth[v] = depth[cur] + 1; | ||
distRoot[v] = distRoot[cur] + w; | ||
dfs(v, cur); | ||
} | ||
} | ||
|
||
int lca(int a, int b) | ||
{ | ||
if(depth[a] < depth[b]) swap(a,b); | ||
int diff = depth[a] - depth[b]; | ||
for(int k = 0; k <= LOGN; k++){ | ||
if(diff & (1<<k)) a = up[a][k]; | ||
} | ||
if(a == b) return a; | ||
for(int k = LOGN; k >= 0; k--){ | ||
if(up[a][k] != up[b][k]){ | ||
a = up[a][k]; | ||
b = up[b][k]; | ||
} | ||
} | ||
return up[a][0]; | ||
} | ||
|
||
int stepUp(int u, int k) { | ||
for (int i=16; i>=0; i--) { | ||
if ((k>>i)&1) { | ||
u = up[u][i]; | ||
} | ||
} | ||
return u; | ||
} | ||
|
||
ll dist(int a, int b) | ||
{ | ||
int c = lca(a,b); | ||
return distRoot[a] + distRoot[b] - 2*distRoot[c]; | ||
} | ||
|
||
vector<int> findMedian(int n, vector<vector<int>>& edges, vector<vector<int>>& queries) { | ||
for (int i = 0; i < n-1; i++) | ||
{ | ||
int u = edges[i][0], v = edges[i][1], w = edges[i][2]; | ||
adj[u].push_back({v,w}); | ||
adj[v].push_back({u,w}); | ||
} | ||
|
||
depth[0] = 0; | ||
distRoot[0] = 0; | ||
dfs(0, 0); | ||
|
||
for(int k = 1; k <= LOGN; k++) { | ||
for(int v = 0; v < n; v++) { | ||
up[v][k] = up[up[v][k-1]][k-1]; | ||
} | ||
} | ||
|
||
vector<int>rets; | ||
for (auto& q: queries) | ||
{ | ||
int u = q[0], v = q[1]; | ||
int c = lca(u,v); | ||
ll total = dist(u,c)+dist(c,v); | ||
|
||
int step1 = depth[u]-depth[c]; | ||
int step2 = depth[v]-depth[c]; | ||
|
||
int low = 0, high = step1+step2; | ||
int k; | ||
while (low < high) { | ||
int mid = low + (high-low)/2; | ||
ll d; | ||
if (mid <= step1) { | ||
k = stepUp(u, mid); | ||
d = distRoot[u] - distRoot[k]; | ||
} else { | ||
k = stepUp(v, step2 - (mid-step1)); | ||
d = total - (distRoot[v] - distRoot[k]); | ||
} | ||
if (d >= total*0.5) | ||
high = mid; | ||
else | ||
low = mid+1; | ||
} | ||
int step = low; | ||
if (step<=step1) | ||
rets.push_back(stepUp(u, step)); | ||
else | ||
rets.push_back(stepUp(v, step2-(step-step1))); | ||
} | ||
|
||
return rets; | ||
} | ||
}; |
7 changes: 7 additions & 0 deletions
Binary_Search/3585.Find-Weighted-Median-Node-in-Tree/Readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
### 3585.Find-Weighted-Median-Node-in-Tree | ||
|
||
对于任何一个query,我们只需要找到u到v路径(途中经过LCA的点记作c),假设路径的总步长是d,路径的总权重和是total。我们只需要在[0,d]之间进行二分搜索一个合适的步数k:即从u走k步,恰好走过的路径长度超过total的一半。 | ||
|
||
注意,我们在二分搜索对k进行判定的时候,需要分类讨论k是否在u到c的路径上,还是c到v的路径上。即看是否`dist(u,c) >= total * 0.5`. 如果k是在u到c的路径上,那么经过的路径长度就是dist(u,k)。如果k是在c到v的路径上,那么经过的路径长度就是dist(u,c)+dist(c,k)。 | ||
|
||
根据binary lifting的算法,树里任意两个节点之间的距离都可以用log(n)的时间求解。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.