mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-12-16 12:13:03 +00:00
feat(test.cpp): 添加函数指针测试代码
feat(P2491.cpp): 实现树的直径与滑动窗口算法 添加了测试函数指针的代码,展示如何获取和调用函数指针。 实现了树的直径计算算法,使用BFS找到最长路径,并通过滑动窗口优化求解满足条件的最大距离。
This commit is contained in:
parent
59ad401d66
commit
6c7412f1d2
164
src/10/5/P2491.cpp
Normal file
164
src/10/5/P2491.cpp
Normal file
@ -0,0 +1,164 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <deque>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
using ll = long long;
|
||||||
|
const int MAXN = 300005;
|
||||||
|
const ll INF = 4e18;
|
||||||
|
|
||||||
|
struct Edge {
|
||||||
|
int to;
|
||||||
|
int weight;
|
||||||
|
};
|
||||||
|
|
||||||
|
vector<Edge> adj[MAXN];
|
||||||
|
int n;
|
||||||
|
ll s;
|
||||||
|
|
||||||
|
ll dist[MAXN];
|
||||||
|
int p[MAXN];
|
||||||
|
|
||||||
|
int bfs(int start, int N, int& farnds) {
|
||||||
|
fill(dist + 1, dist + N + 1, -1);
|
||||||
|
fill(p + 1, p + N + 1, 0);
|
||||||
|
deque<int> q;
|
||||||
|
|
||||||
|
dist[start] = 0;
|
||||||
|
q.push_back(start);
|
||||||
|
|
||||||
|
farnds = start;
|
||||||
|
while (!q.empty()) {
|
||||||
|
int u = q.front();
|
||||||
|
q.pop_front();
|
||||||
|
if (dist[u] > dist[farnds]) {
|
||||||
|
farnds = u;
|
||||||
|
}
|
||||||
|
for (const auto& edge : adj[u]) {
|
||||||
|
int v = edge.to;
|
||||||
|
int w = edge.weight;
|
||||||
|
if (dist[v] == -1) {
|
||||||
|
dist[v] = dist[u] + w;
|
||||||
|
p[v] = u;
|
||||||
|
q.push_back(v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return farnds;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
ll bdist[MAXN];
|
||||||
|
bool d[MAXN];
|
||||||
|
|
||||||
|
void dfs(int u, int p) {
|
||||||
|
bdist[u] = 0;
|
||||||
|
for (const auto& edge : adj[u]) {
|
||||||
|
int v = edge.to;
|
||||||
|
int w = edge.weight;
|
||||||
|
|
||||||
|
if (v == p || d[v]) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
dfs(v, u);
|
||||||
|
|
||||||
|
bdist[u] = max(bdist[u], bdist[v] + w);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void solve() {
|
||||||
|
ios_base::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
if (!(cin >> n >> s)) return;
|
||||||
|
for (int i = 0; i < n - 1; ++i) {
|
||||||
|
int u, v, w;
|
||||||
|
if (!(cin >> u >> v >> w)) return;
|
||||||
|
adj[u].push_back({v, w});
|
||||||
|
adj[v].push_back({u, w});
|
||||||
|
}
|
||||||
|
if (n == 1) {
|
||||||
|
cout << 0 << endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int A = 0, B = 0;
|
||||||
|
|
||||||
|
A = bfs(1, n, A);
|
||||||
|
B = bfs(A, n, B);
|
||||||
|
|
||||||
|
vector<int> dpath;
|
||||||
|
int curr = B;
|
||||||
|
|
||||||
|
while (curr != 0) {
|
||||||
|
dpath.push_back(curr);
|
||||||
|
d[curr] = true;
|
||||||
|
curr = p[curr];
|
||||||
|
}
|
||||||
|
reverse(dpath.begin(), dpath.end());
|
||||||
|
|
||||||
|
int k = dpath.size();
|
||||||
|
vector<ll> d(k);
|
||||||
|
vector<ll> e(k);
|
||||||
|
for (int i = 0; i < k; ++i) {
|
||||||
|
d[i] = dist[dpath[i]];
|
||||||
|
}
|
||||||
|
for (int i = 0; i < k; ++i) {
|
||||||
|
dfs(dpath[i], 0);
|
||||||
|
e[i] = bdist[dpath[i]];
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<ll> L(k + 1, -INF);
|
||||||
|
vector<ll> R(k + 1, -INF);
|
||||||
|
|
||||||
|
for (int i = 0; i < k; ++i) {
|
||||||
|
L[i + 1] = max(L[i], e[i] - d[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = k - 1; i >= 0; --i) {
|
||||||
|
R[i] = max(R[i + 1], e[i] + d[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
ll ansdist = INF;
|
||||||
|
deque<int> dq;
|
||||||
|
int p1 = 0;
|
||||||
|
|
||||||
|
for (int p2 = 0; p2 < k; ++p2) {
|
||||||
|
while (!dq.empty() && e[dq.back()] <= e[p2]) {
|
||||||
|
dq.pop_back();
|
||||||
|
}
|
||||||
|
dq.push_back(p2);
|
||||||
|
|
||||||
|
while (d[p2] - d[p1] > s) {
|
||||||
|
if (!dq.empty() && dq.front() == p1) {
|
||||||
|
dq.pop_front();
|
||||||
|
}
|
||||||
|
p1++;
|
||||||
|
}
|
||||||
|
|
||||||
|
ll maxmid = e[dq.front()];
|
||||||
|
|
||||||
|
ll maxleft = -INF;
|
||||||
|
if (p1 > 0) {
|
||||||
|
maxleft = L[p1] + d[p1];
|
||||||
|
}
|
||||||
|
|
||||||
|
ll maxright = -INF;
|
||||||
|
if (p2 < k - 1) {
|
||||||
|
maxright = R[p2 + 1] - d[p2];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ll curmax = max({maxmid, maxleft, maxright});
|
||||||
|
ansdist = min(ansdist, curmax);
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << ansdist << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
15
src/test.cpp
15
src/test.cpp
@ -1,3 +1,16 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
using uint64 = unsigned long long;
|
||||||
|
|
||||||
|
void (*ptr)(void) = (void(*)(void))0xDEADBEEF;
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
|
uint64 nptr =(uint64)(printf);
|
||||||
|
printf("func ptr= %lx\n",printf);
|
||||||
|
ptr=(void(*)(void))nptr;
|
||||||
|
ptr();
|
||||||
|
ptr();
|
||||||
|
ptr();
|
||||||
|
ptr();
|
||||||
|
ptr();
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user