【codeforces 734E】Anton and Tree【缩点+DP】

本文提供Codeforces 734E题目的详细解答过程,通过缩点找到树的最长直径d,并证明了答案为(d+1)/2的正确性。文章包含完整的C++代码实现。

题目链接:codeforces 734E

题目大意:

来看题解的都是知道题目是什么意思的,我就不多嘴了。

题解

cf自题解已经非常清楚了。

先缩点,再找出该树一条最长的直径d,答案就是(d+1)/2
为什么呐??
假设树的直径是0,那么答案就是0吧。

然后我们就可以看到这个事实: the tree diameter can’t be decreased more than by two per one painting operation with the “compression”. So the answer cannot be less than(d+1)/2 , where d is the tree diameter.(懒得翻译)

现在来证明它的正确性:Find such vertex that the shortest path from it to any other vertex doesn’t exceed (d+1)/2. Such vertex can always be found, because otherwise the tree diameter won’t be less that d + 1, which is impossible. Now see that if we paint this vertex (d+1)/2 times, we will paint the tree in one color.

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;

#define N 200010
vector<int> g[N],gc[N];
bool vis[N];
int belong[N],col[N],dp[N];
int n,ans,u,v,cnt;

void dfs1(int u,int c,int rt)
{
    if(vis[u] || col[u] != c) return;
    vis[u] = 1;
    belong[u] = rt;
    for(int i = 0;i < g[u].size();i++)
        dfs1(g[u][i],c,rt);
}

void dfs2(int u,int fa)
{
    int mx = 0,mmx = 0;
    for(int i = 0;i < gc[u].size();i++)
    {
        int v = gc[u][i];
        if(v != fa)
        {
            dfs2(v,u);
            mmx = max(mmx,dp[v]+1);
            if(mmx > mx) swap(mmx,mx);
        }
    }
    dp[u] = mx;
    ans = max(ans,mx + mmx);
}

int main()
{
    scanf("%d",&n);
    for(int i = 1;i <= n;i++) scanf("%d",&col[i]);
    for(int i = 1;i < n;i++)
    {
        scanf("%d%d",&u,&v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for(int i = 1;i <= n;i++)
        if(!vis[i]) dfs1(i,col[i],++cnt);
    for(int i = 1;i <= n;i++)
        for(int j = 0;j < g[i].size();j++)
        {
            int v = g[i][j];
            if(belong[v] != belong[i]) gc[belong[i]].push_back(belong[v]);
        }
    dfs2(1,-1);
    printf("%d\n",(ans+1)/2);
    return 0;
}
当前提供的引用内容并未提及关于Codeforces比赛M1的具体时间安排[^1]。然而,通常情况下,Codeforces的比赛时间会在其官方网站上提前公布,并提供基于不同时区的转换工具以便参赛者了解具体开赛时刻。 对于Codeforces上的赛事而言,如果一场名为M1的比赛被计划举行,则它的原始时间一般按照UTC(协调世界时)设定。为了得知该场比赛在UTC+8时区的确切开始时间,可以遵循以下逻辑: - 前往Codeforces官网并定位至对应比赛页面。 - 查看比赛所标注的标准UTC起始时间。 - 将此标准时间加上8小时来获取对应的北京时间(即UTC+8)。 由于目前缺乏具体的官方公告链接或者确切日期作为依据,无法直接给出Codeforces M1比赛于UTC+8下的实际发生时段。建议定期访问Codeforces平台查看最新动态更新以及确认最终版程表信息。 ```python from datetime import timedelta, datetime def convert_utc_to_bj(utc_time_str): utc_format = "%Y-%m-%dT%H:%M:%SZ" bj_offset = timedelta(hours=8) try: # 解析UTC时间为datetime对象 utc_datetime = datetime.strptime(utc_time_str, utc_format) # 转换为北京时区时间 beijing_time = utc_datetime + bj_offset return beijing_time.strftime("%Y-%m-%d %H:%M:%S") except ValueError as e: return f"错误:{e}" # 示例输入假设某场Codeforces比赛定于特定UTC时间 example_utc_start = "2024-12-05T17:35:00Z" converted_time = convert_utc_to_bj(example_utc_start) print(f"Codeforces比赛在北京时间下将是:{converted_time}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值