https://www.zybuluo.com/ysner/note/1302132 Problem surface
Select as few points as possible on the size of \ (n\) tree, so that all the selected point distances are less than or equal to \ (k\).
-
\ (n\leq10^5,k\leq20\)
parsing Order \ (k\) The is a positive integer. The
Set \ (k+1\) is the closest, selected point , and the distance to be covered upward is \ (k\) . This type of tag is called \ (a\) .
Set \ (-k-1\) is the closest, selected Point , with a coverage of \ ( K\) . This type of tag Chen Wei
(\ (+1\) , \ ( -1\) is primarily to avoid \ (0\) has a double meaning)
to mark all leaf nodes.
When collecting markup, if \ (ABS (A) \geq abs (B) \) , \ (b\) Point can be \ (a\) One of the selected points overlay to, take \ (a\) The tag.
Otherwise take \ (b\) tag.
If \ (b=-k-1\) , the point must be selected (otherwise it will not be covered).
pay particular attention to root nodes.
If you mark a negative number at the root, the root needs to be marked separately (because there is no point to overwrite it more).
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath > #include <algorithm> #define LL Long long#define re register#define il inline#define fp (i,a,b) for (re int i=a;i&l t;=b;i++) #define FQ (I,A,B) for (re int i=a;i>=b;i--) using namespace Std;const int N=1e5+100;int N,k,t,f[n],g[n],h[n], Cnt;struct edge{int to,nxt;} E[n<<1];il void Add (re int u,re int v) {e[++cnt]= (Edge) {v,h[u]};h[u]=cnt;} Il int gi () {re int x=0,t=1; Re char Ch=getchar (); while (ch!= '-' && (ch< ' 0 ' | | Ch> ' 9 ')) Ch=getchar (); if (ch== '-') T=-1,ch=getchar (); while (ch>= ' 0 ' &&ch<= ' 9 ') X=x*10+ch-48,ch=getchar (); return x*t;} Il void dfs (re int u,re int fa) {re int mor=0,les=-1,son=0; for (re int i=h[u];i+1;i=e[i].nxt) {re int v=e[i].to; if (V==FA) Continue;++son; DFS (V,U); G[U]+=G[V]; --f[v];if (!f[v])--f[v]; if (f[v]>0) Mor=max (Mor,f[v]); else Les=min (Les,f[v]); } if (ABS (MOR) >=abs (les)) f[U]=mor; else if (les<=-k-1| | u==1) F[u]=k+1,++g[u]; else F[u]=les;} int main () {memset (h,-1,sizeof (h)); N=gi (); K=gi (); GI (); FP (i,1,n-1) {re int u=gi (), V=gi (); Add (u,v); add (V,u); } dfs (1,0); printf ("%d\n", g[1]); return 0;}
General luogu3942 Order