首页 > 其他分享 >CodeForces - 212E IT Restaurants

CodeForces - 212E IT Restaurants

时间:2022-11-16 18:33:24浏览次数:55  
标签:结点 int 染色 CodeForces son define 212E now Restaurants

题意:给一棵树的结点染色,每个结点可以染红色、蓝色或不染色。相邻两个结点必须染同一种颜色,或者一个染色一个不染色。求整棵树染色结点最多,且在整棵树至少有一个结点染红色,一个结点染蓝色的情况下,红蓝两色各自有多少结点。

解:如果只是染色最多,可以整棵树染一种颜色;现要求两种颜色都要有,那只能挑一个节点不染色,因此最多染色n-1个结点。枚举这个不染色的结点,再枚举这个不染色结点的子树,用01背包求其组合方式。

代码:

#include <bits/stdc++.h>
using namespace std;
#define maxx 5005
#define maxn 25
#define maxm 205
#define ll long long
#define inf 1000000009
#define mod 2520
struct edge{
    int u,v,w,next;
}e[maxx*2];
int head[maxx]={0},cnt=0;
void add(int u,int v){
    e[++cnt].u=u;e[cnt].v=v;e[cnt].next=head[u];head[u]=cnt;
}

int n;
int son[maxx]={0};
int a[maxx]={0};
bitset<5005> ans;
void dfs1(int now,int fa){
    son[now]=1;
    for(int i=head[now];i;i=e[i].next){
        int to=e[i].v;
        if(to==fa) continue;
        dfs1(to,now);
        son[now]+=son[to];
    }
}
void dfs2(int now,int fa){
    int cnt2=0,sum=0;
    for(int i=head[now];i;i=e[i].next){
        int to=e[i].v;
        if(to==fa) continue;
        sum+=son[to];
        a[++cnt2]=son[to];
    }
    a[++cnt2]=n-sum-1;
    bitset<5005> dp;
    dp[0]=1;
    for(int i=1;i<=cnt2;i++){
        for(int j=n;j>=a[i];j--){
            if(dp[j-a[i]]) dp[j]=1;
        }
    }
    ans|=dp;
    for(int i=head[now];i;i=e[i].next){
        int to=e[i].v;
        if(to==fa) continue;
        dfs2(to,now);
    }
}
signed main() {
    scanf("%d",&n);
    for(int i=1;i<n;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        add(x,y);
        add(y,x);
    }
    dfs1(1,0);
    dfs2(1,0);
    int ans2=0;
    for(int i=1;i<n-1;i++){
        if(ans[i]) ans2++;
    }
    printf("%d\n",ans2);
    for(int i=1;i<n-1;i++){
        if(ans[i]) printf("%d %d\n",i,n-i-1);
    }
    return 0;
}
//dp[i][j]
View Code

 

 

标签:结点,int,染色,CodeForces,son,define,212E,now,Restaurants
From: https://www.cnblogs.com/capterlliar/p/16897093.html

相关文章

  • From CodeForces Catlogs
    2022/11/1https://codeforces.com/blog/entry/106346On"isthisgreedyorDP",forcingandrubberbandsreadingotherpeople'sthoughtprocessesTheylookatth......
  • Codeforces Round #180 (Div. 2) 解题报告
    ​​题目链接​​A.​​SnowFootprints​​​​A-SnowFootprints​​Thestartingpositioncanbeanywherewithafootprint.Thefootprintscanbecategorized......
  • codeforces补题计划
    11.15CodeforcesRound#833(Div.2)知识点:D:高位和对低位无影响E:笛卡尔树上dp补题传送门......
  • Codeforces Round #833 (Div. 2)补题
    CodeforcesRound#833(Div.2)D.ConstructOR知识点:高位和对低位无影响一开始以为和广州的M一样,是数位dp,后来发现只要找到一个就行果然无论什么时候都要看清题目......
  • Codeforces Round #833 (Div. 2)
    CodeforcesRound#833(Div.2)做题记录A.TheUltimateSquare略过B.DiverseSubstrings思路:我们发现字符数只有0~9十种字符,也就是说,如果我们固定一个左端点\(l\),......
  • Codeforces #816 1715 C
    题面假设我们有一个函数$g(1,n)$表示$i=1\simn-1$中满足$a_i\neqa_{i+1}$的$i$的数量。现在有$m$个询问,每个询问将会让$x\rightarrow......
  • [VP]Codeforces Round #390 (Div. 2)
    和\(\color{black}{a}\color{red}{rtalter}\)一起打的顺嘴说一下今天(周日)早晨的趣逝事情发生在吃完早饭后,\(\color{grey}{WintersRain}\)和\(\color{black}{S}\color......
  • Codeforces 1748 D
    这场D还是很有趣的,值得探讨。首先\(a|x,b|x\)是两个数,不相同时不太好做,所以我们能否找到一个\(x\),满足\(a|x=b|x\)并且都是\(d\)的倍数呢?然后我们让\(d\)特殊一些——我......
  • Codeforces Round #833 (Div. 2)(持续更新)
    Preface我是大FW,B因为本地调试的时候把数组大小改成200忘记该回去了浪费了很多时间和罚时C刚开始也没想清楚WA了两发心态爆炸,然后D其实想出了一种做法但是心态崩了就没写......
  • Educational Codeforces Round 35 (Rated for Div. 2) F. Tree Destruction(树的直径/
    题目大意是给定一棵树,每次选择两个叶子将其删除,同时将两个叶子之间的简单路径长度加到答案上,找到一种方案使答案最大化,并输出删除的顺序。首先有一个结论是,距离树上某个节......