[luogu3155 CQOI2009] coloring of leaves (tree DP)

Source: Internet
Author: User

Transmission Door

Solution

A very simple tree-shaped dpqwq, transfer relationship: Father dyed his son without dyeing
Just make sure the root is a simple tree dp, and the root can take a non-leaf node
We can discuss the situation and find out that the answer won't change.

Code
by Menteur_hxy#include <cmath> #include <vector> #include <cstdio> #include <cstdlib># Include <cstring> #include <iostream> #include <algorithm> #define RE register#define Fo (i,a,b) for (re    int i= (a), _= (b), i<=_;i++) #define RO (i,a,b) for (Re int i= (b), _= (a); i>=_;i--) using namespace Std;inline int Read () {    int X=0,f=1;char C=getchar ();    while (!isdigit (c)) {if (c== '-') F=-f;c=getchar ();}    while (IsDigit (c)) x= (x<<1) + (x<<3) +c-48,c=getchar (); return x*f;} const int N=1e4+10,inf=0x3f3f3f3f;int n,m;int f[n][2],col[n];vector <int> v[n];void dfs (int u,int pre) {f[u][0]=    F[u][1]=1;    if (u<=m) F[u][!col[u]]=inf;    int siz=v[u].size (), V;        Fo (I,0,siz-1) if ((v=v[u][i))!=pre) {DFS (v,u);        F[u][1]+=min (F[v][1]-1,f[v][0]);    F[u][0]+=min (f[v][0]-1,f[v][1]);    }}int Main () {N=read (), M=read ();//due to custom N,m interchange Fo (i,1,m) col[i]=read ();        Fo (i,1,n-1) {int a=read (), B=read (); V[a].push_back (b); V[B]. push_back (a);    } dfs (m+1,0);    printf ("%d", min (f[m+1][0],f[m+1][1])); return 0;}

[luogu3155 CQOI2009] coloring of leaves (tree dp)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.