얼굴 질문
문제 해결
세트 ([I]가 \ F) \는 하는 루트 노드 (I는 \) \은 최소한의 시간이 소요
세트 \ (S \) 의 \ (I는 \) 의 선조의 집합이 경우 일 수
\ [F [I] = 분
- S \에서 j 개의 \ (j) ^ P + (I] J [F)] 하는 \을 ( (I - J) ^ P- 형의 \) , 우리는 한
\ [((I + 1) - (j + 1)) ^ (P) + (I - J) ^ 피의 \의 GEQ ((I + 1) - J) ^ P + (I - (j + 1
)) ^ (P) \] 가 사변형 만족 부등 식임을 알 수
직접 의사 결정 (단조 될 수 있습니다나는 오른쪽에 있어야한다, 이것은 다른 사람의 보는 것입니다 쓰고 있어요)
암호
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#define itn int
#define reaD read
#define N 100005
using namespace std;
int n, p, w[N], cnt;
long long pw[N], ans;
template < typename T >
inline T read()
{
T x = 0, w = 1; char c = getchar();
while(c < '0' || c > '9') { if (c == '-') w = -1; c = getchar(); }
while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
return x * w;
}
namespace Graph
{
int head[N];
struct edge { int to, next; } e[N];
inline void adde(int u, int v) { e[++cnt] = (edge) { v, head[u] }; head[u] = cnt; }
};
using namespace :: Graph;
long long fpow(long long x, int y = p)
{
long long res = 1;
for( ; y; y >>= 1, x = 1ll * x * x)
if(y & 1) res = 1ll * res * x;
return res;
}
namespace DFS
{
long long f[N];
int top, stk[N], pos[N];
struct node { int l, r, id; } q[N];
void dfs(int u, int fa)
{
if(u == 1) stk[++top] = u, f[u] = 0, pos[u] = top;
else
{
int num;
long long tmp = f[0];
for(int i = pos[fa]; i <= top; i++)
{
long long res = f[stk[i]] + w[stk[i]] + fpow(u - stk[i], p);
if(res <= tmp) num = i, tmp = res;
}
f[u] = tmp;
pos[u] = num;
stk[++top] = u;
}
bool flag = 0;
for(int i = head[u]; i; i = e[i].next)
flag = 1, dfs(e[i].to, u);
if(!flag) ans = min(ans, f[u]);
top--;
}
};
using namespace :: DFS;
int main()
{
n = read <int> (); p = read <int> ();
for(int i = 1; i <= n; i++)
{
w[i] = read <int> (); int u = read <int> ();
if(u) adde(u, i);
}
memset(f, 0x3f, sizeof(f));
ans = f[0];
dfs(1, 0);
printf("%lld\n", ans);
return 0;
}