CSP training match 0925

Hey still too naiive
AB question came about simply knocked 1.5HB problem also took the 1H and then I zz
but still very careful
final inspection found that there can be a sensitive issue 0

C questions I began to see? ? This problem is so water do not mate for every two nodes on the line Well also do half
right I'm thinking there must be a mistake! ! !
Then look at the large sample to think I must be wrong!
. . . .
Then I played the last few minutes of practice a wave table Oh my God this is not what I want to do

Mentality actually quite stable but still too naiive the

Mainly it was because the sample did not understand the experience. . .

The sample should be carefully read to play table ah

3, split cable (tree.pas / cpp)
[Problem Description]
between penguin States Internet are connected to each other by a cable, a tree structure is formed. Now that the winter, the heating unit
door lack of fuel, so they decided to dismantle some of the network cable to do the fuel. But now there are K penguins to the Internet and others online
games, so they need to put K penguins arrange a different room (two penguins in the same room will fight), then
removed after a number of cable, but need to ensure that each penguins can at least stay through the cable and at least one other penguin online
game.
So they want to know the minimum number of network cable need to be retained?

: Solution
maximum independent set
DFS or from the beginning greedy leaf node or DP
maximum number of edges defined F [I] [1/0] i selected subtree node i as the maximum point of the set of independent

\(f[i][0]=\sum f[u][1]\)

\(f[i][1]=max(f[i][0]-f[u][1]+1);\)


code:

 

#include<stdio.h>
#include<bits/stdc++.h> 
using namespace std; 
#define maxnn 200100 
#define ll long long 
int las[maxnn],nex[maxnn],en[maxnn],tot; 
int n; 
int f[maxnn>>1][2];  
int T; 
int m; 
void add(int a,int b) { 
    en[++tot]=b; 
    nex[tot]=las[a]; 
    las[a]=tot; 
} 
void dfs(int v,int fa) 
{ 
    for(int i=las[v];i;i=nex[i]) 
    { 
        int u=en[i]; 
        if(u!=fa) 
        { 
            dfs(u,v); 
            f[v][0]+=f[u][1]; 
        } 
    } 
        for(int i=las[v];i;i=nex[i]) 
    { 
        int u=en[i]; 
        if(u!=fa) 
        { 
            f[v][1]=max(f[v][1],f[v][0]+f[u][0]-f[u][1]+1); 
        } 
    } 
     
} 
int main() { 
//  freopen("tree.in","r",stdin);
//  freopen("ooo","w",stdout);
    cin>>T; 
    int x,y,z; 
    while(T--) 
    { 
         for(int i=1;i<=n;i++) 
         f[i][0]=f[i][1]=0;
        scanf("%d%d",&n,&m);  
         for(int i=1;i<=tot;i++)
        {
            las[i]=0;
            nex[i]=0;
            en[i]=0;
        }
        tot=0;
        for(int i=1;i<n;i++)
       {
            scanf("%d",&x); 
            add(x,i+1); 
            add(i+1,x); 
        }
        dfs(1,1);
    
        int d=max(f[1][1],f[1][0]); 
        
        if(2*d>=m) 
        { 
            printf("%d\n",(m+1)/2); 
        } 
        else 
        { 
            printf("%d\n",d+m-2*d); 
        } 
    } 
} 

Guess you like

Origin www.cnblogs.com/OIEREDSION/p/11588403.html