Topic:
https://www.luogu.org/problem/show?pid=1041
This is a good question;
My first idea is greedy +dfs, each time cut the largest point of the sub-tree edge, can be updated again;
But this greed is wrong, but Noip data good (hen) Heart (shui), can take 90 points;
For example, the following illustration:
You can see it, HH.
According to this greed we will cut 2, but in this way, the number of infections is 4, (1,3,6,7);
But if you cut 3, the number of infections is 2 (up);
So can search, layer by layer to search;
Since the height of the tree is uncertain,
If the tree is a chain, O (1) can be solved;
If the tree is very "full", each layer we need to cut one, the tree height of about Logn, a total of cut logn times, about O (n^2) around. I'm a jive.
I am too weak to estimate the complexity, if any big guy knows, please @ me, thank you;
Ps:
In fact, there is a kind of writing called randomized search, because the data is small, so it can be completely off, but seemingly very unreliable, so do not write; in fact, I will not write
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace
Std
const int maxn=1001;
int DEEP[MAXN],FST[MAXN],NXT[MAXN],FA[MAXN],SIZ[MAXN],CNT[MAXN],RT[MAXN][MAXN];
int n,m,tot,ss,ans=21487485;
BOOL VIS[MAXN]; struct HH {int from,to,cost;}
MA[MAXN];
void build (int f,int t) {tot++;
Ma[tot]= (hh) {f,t};
NXT[TOT]=FST[F];
Fst[f]=tot;
Return
} void Dfs_init (int x,int f)//preprocessing;
{deep[x]=deep[f]+1,siz[x]=1,fa[x]=f;
Ss=max (Ss,deep[x]);
Rt[deep[x]][++rt[deep[x]][0]]=x;
for (int i=fst[x];i;i=nxt[i]) {int v=ma[i].to;
if (v==f) continue;
Dfs_init (V,X);
SIZ[X]+=SIZ[V];
} return;
} void cut (int x,bool flag)//go or tag;
{Vis[x]=flag;
for (int i=fst[x];i;i=nxt[i]) {int v=ma[i].to;
if (v==fa[x]) continue;
Cut (V,flag);
} return;
} void Dfs (int depth,int sum,int Geli)//depth, number of infections, number of isolates; {int num=0; if (depth==ss+1 | |GELI+SUM==N)//Note the end condition, for the case of the chain, only the first judgment is not enough; {ans=min (ans,sum);
Return
} for (int i=1;i<=rt[depth][0];i++) {int v=rt[depth][i];
if (Vis[v]) num++;
} for (int i=1;i<=rt[depth][0];i++) {int v=rt[depth][i];
if (!vis[v]) {cut (v,1);
DFS (Depth+1,sum+rt[depth][0]-num-1,geli+siz[v]);
Cut (v,0);
}} return;
} void Solve () {scanf ("%d%d", &n,&m);
for (int i=1;i<=m;i++) {int x, y;
scanf ("%d%d", &x,&y);
Build (x, y), build (Y,X);
} dfs_init (1,0);
DFS (2,1,0);//Start searching from the second-level point, because we map the edge to the point, that is, from the edge of the first layer to search; cout<<ans;
} int main () {solve ();
return 0; }