Codeforces 321C

本文介绍了一种解决树形结构中城市等级分配问题的算法。该算法通过深度优先搜索从叶子节点开始分配最低等级,并确保同一等级的城市间路径上至少存在一个更高级别的城市。文章提供了详细的算法实现思路及C++代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


题意概要
给一颗树,树的节点表示一个城市,然后城市有一个等级rank(A-Z), A为最高等级,Z为最低等级。要求如果两个城市等级一样,那么他们路径中间(树两点路径是唯一的)必须存在一个比他们两个等级高的节点。求一个分配城市等级的解决方案。

解题难点
我们首先用DFS深入到叶子节点去,从叶子往根进行处理,给叶子节点分配好rank最低的之后,更新父亲节点。
其中用ans[]表示答案。用cnt[i][j]表示i节点的所有子节点(就是它的儿子,没有包括儿子的子树)用等级j用了次数。

      
<span style="white-space:pre">	</span>if (!cnt[now][i]) p = i;
        if (cnt[now][i] >= 2) break;



以上表示如果子节点没用过,那么可以使用这个rank,但是如果存在这个等级的儿子点个数>=2,那么表示必须使用比这个等级更高的rank,所以退出。因为儿子的子树是不需要父节点关心的,所以有:
 
        for (int i = p + 1; i < 26; i++)
            cnt[now][i] = 0; 
        for (int i = 0; i < 26; i++)
            cnt[par][i] += cnt[now][i];


程序
#include <cstdio>
#include <vector>
using namespace std;
const int MAX_N = 200005;
bool vis[MAX_N];
int cnt[MAX_N][30];
int ans[MAX_N];
vector <int> A[MAX_N];
bool ok;
void dfs(int now, int par)
{
    vis[now] = 1;
    if (!ok) return;
    for (int i = 0; i < A[now].size(); i++)
    {
        if (vis[A[now][i]]) continue;
        dfs(A[now][i], now);
    }


    int p = -1;
    for (int i = 0; i < 26; i++)
    {
        if (!cnt[now][i]) p = i;
        if (cnt[now][i] >= 2) break;
    }
    if (p == -1) { ok = false; return; }
    else 
    {
        ans[now] = p; cnt[now][p] = 1;
        for (int i = p + 1; i < 26; i++)
            cnt[now][i] = 0;
        for (int i = 0; i < 26; i++)
            cnt[par][i] += cnt[now][i];
        return;
    }
}


int main()
{
   // freopen("nimo.in", "r", stdin);
    int n;
    scanf("%d", &n);
    for (int i = 1; i < n; i++)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        A[x].push_back(y);
        A[y].push_back(x);
    }
    ok = 1;
    dfs(1, 0);
    if (!ok)
    {
        printf("Impossible!\n");
        return 0;
    } else {
        int tmp = 30;
        for (int i = 1; i <= n; i++)
            if (ans[i] < tmp) tmp = ans[i];


        for (int i = 1; i <= n; i++)
            printf("%c ", 'A' + ans[i] - tmp);
        printf("\n");
        return 0;
    }
}



### Codeforces Problem 1332C Explanation The provided references pertain specifically to problem 742B on Codeforces rather than problem 1332C. For an accurate understanding and solution approach for problem 1332C, it's essential to refer directly to its description and constraints. However, based on general knowledge regarding competitive programming problems found on platforms like Codeforces: Problem 1332C typically involves algorithmic challenges that require efficient data structures or algorithms such as dynamic programming, graph theory, greedy algorithms, etc., depending upon the specific nature of the task described within this particular question[^6]. To provide a detailed explanation or demonstration concerning **Codeforces problem 1332C**, one would need direct access to the exact statement associated with this challenge since different tasks demand tailored strategies addressing their unique requirements. For obtaining precise details about problem 1332C including any sample inputs/outputs along with explanations or solutions, visiting the official Codeforces website and navigating to contest number 1332 followed by examining section C is recommended. ```python # Example pseudo-code structure often seen in solving competitive coding questions. def solve_problem_1332C(input_data): # Placeholder function body; actual logic depends heavily on the specifics of problem 1332C. processed_result = process_input(input_data) final_answer = compute_solution(processed_result) return final_answer input_example = "Example Input" print(solve_problem_1332C(input_example)) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值