然而这题为啥没有 DP 的tag...
  • 板块P3942 将军令
  • 楼主DiruiXiao
  • 当前回复1
  • 已保存回复1
  • 发布时间2022/10/6 17:19
  • 上次更新2023/10/27 08:27:58
查看原帖
然而这题为啥没有 DP 的tag...
675237
DiruiXiao楼主2022/10/6 17:19

RT

#include<cstdio>
#include<cstring>
#include<iostream>
#define getchar() p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++
#define putchar(x) (p3-obuf<1000000)?(*p3++=x):(fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=x)
using namespace std;
static char buf[1000000], *p1 = buf, *p2 = buf, obuf[1000000],*p3 = obuf;
template<typename item>
inline void read(register item &x) {
	x = 0; register int f = 1; register char c = getchar();
	while(c < '0' || c > '9') { if(c=='-')f = -1; c = getchar(); }
	while(c >= '0' && c <='9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
	x *= f;
}
template<typename T>
inline void print(register T x) {
	x < 0 ? (putchar('-'), x = -x) : x;
	register int c[25], len = 0;
	memset(c, 0, sizeof(c));
	if (x == 0) len = 1;
	while (x) ++len, c[len] = x % 10, x /= 10;
	for (int i = len; i >= 1; --i) {
		putchar(c[i] + '0');
	}
}

#define MAXN 100005
#define MAXD 23
#define INF (int)(1e9)

struct Edge { int v, nex; } edge[MAXN * 2];
int N, D, M, ecnt, head[MAXN], F[MAXN][MAXD], G[MAXN][MAXD];

inline void addEdge(int u, int v) {
	edge[++ecnt].v = v;
	edge[ecnt].nex = head[u];
	head[u] = ecnt;
}

inline void DFS(register int u, register int fa) {
	F[u][0] = G[u][0] = 1;
	for (register int i = 1; i <= D; ++i) F[u][i] = 1;
	F[u][D + 1] = INF;
	for (register int i = head[u]; i; i = edge[i].nex) {
		register int v = edge[i].v;
		if (v == fa) continue;
		DFS(v, u);
		for (register int j = D; j >= 0; --j) { 
			F[u][j] = min(F[u][j] + G[v][j], F[v][j + 1] + G[u][j + 1]);
			F[u][j] = min(F[u][j], F[u][j + 1]);
		}
		G[u][0] = F[u][0];
		for (register int j = 1; j <= D + 1; ++j) {
			G[u][j] += G[v][j - 1];
			G[u][j] = min(G[u][j], G[u][j - 1]);
		}
	}
}

int main() {
	freopen("rebody.in", "r", stdin);
	freopen("rebody.out", "w", stdout);
	read(N), read(D); int T; read(T);
	for (register int i = 1; i < N; ++i) {
		register int u, v;
		read(u), read(v);
		addEdge(v, u), addEdge(u, v);
	}
	DFS(1, 1);
	print(F[1][0]);
	fwrite(obuf, p3 - obuf, 1, stdout);
	return 0;
}
2022/10/6 17:19
加载中...