Codeforces Round #754 (Div. 2), problem: (D)

ps:写在飞书上的,对于不想看英文的同学很不友好,就先用中文说一说思路:首先题目要求我们对一个树上的结点进行重新relabel使 第一轮保证Eikooc胜利的情况他能够选择的结点最多,首先猜测有一种relabeling的方法使无论Eikooc无论选择哪一个结点,另一个人不能走第二步,这就要求对于所有的满足2^i<=j<k<2^s,s = i+1 的结点j,k,它们之间不能直接相连,之后考虑将树的结点分为深度为奇数和偶数的两种,(因为同一深度的结点必定不能直接相连,而深度差为2的倍数的结点也必定不能直接相连)这样必有一种的个数小于等于n/2(n为树的结点的个数),之后将这个数分为2的幂的和,将对应的数分给这些结点,另外的数分给另一种结点,题目就做完了。传送门:Problem - D - Codeforces

ps:这题如果使用队列来考察深度会爆栈。

It's my first time to write a blog entry and I am a newcomer.

So I am going to tell you what I had been through when I tried to solve the problem: (D) of Codeforces Round #754 (Div. 2).

Firstly,I was so careless that I thought the problem wanted me to find a possible relabeling that guarantee a win for Eikooc.(that is I negelecting the requirement that the number of nodes that can be chosen in the first turn is to be maximized).That is easily achieved.You only have to choose a leaf node and the node that is directly connected to it and relabel them as 1 and 2 and that's fine.So, without doubt,I failed the first time.(wrong solving)

After reviewing the problem,I found my mistake.Later,I had an idea.I thought that in the end,there exists a relabeling that whichever node you choose,you can't find a second point that can be chosen and in this case Eikooc wins all the time.Although I can't prove my assumption,I worked on it because I had no time.So I found that if you make all k,j which satisfies the following requirement:2^i<=j<k<2^s,s = i+1 don't have direct connections to each other and that's OK.But I can't change my idea into code.

1

2 3

4 5 6 7

...

1st

2rd

3th

nth

After the contest,I found that any positive number can be divided into the sum of different 2^is.So it turns out that my idea really works.You only have to find nodes of depth of 1,3,5...and that's ok.

By the way,if the number of nodes of depth 1,3,5,... is larger than the other part,you have to consider the other part.

It took me 13 submissions to be accepted.Anyway,the problem is wonderful.

AC code:AC code

Source:

#include <bits/stdc++.h>
using namespace std;
#define N 200005
int t;
int n;
long long num2[35];
bool vis[N];
bool isOdd[N];
int cnt;
vector<int> vec[N];
void init()
{
    num2[1] = 1;
    for (int i = 1; i < 30; i++)
    {
        num2[i + 1] = 2 * num2[i];
    }
}
void divide(int num)
{
    for (int i = 30; i >= 1; i--)
    {
        if ((long long)num >= num2[i])
        {
            int p = num2[i];
            for (int s = p; s < p * 2; s++)
            {
                vis[s] = true;
            }
            num -= num2[i];
        }
    }
}
void dfs(int now, int fa)
{
    for (auto k : vec[now])
        if (k != fa)
        {
            isOdd[k] = !isOdd[now];
            if (!isOdd[k])
                cnt++;
            dfs(k, now);
        }
}
int main()
{
    init();
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        if (n == 1)
        {
            cout << 1 << endl;
            continue;
        }
        memset(vis, false, sizeof(vis));
        for (int j = 1; j < n; j++)
        {
            int u, v;
            cin >> u >> v;
            vec[u].push_back(v);
            vec[v].push_back(u);
        }
        cnt = 1;
        dfs(1, -1);
        for (int i = 1; i <= n; i++)
            vec[i].clear();
        bool flag = true;
        if (cnt > n - cnt)
        {
            flag = false;
            cnt = n - cnt;
        }
        divide(cnt);
        int point1 = 1;
        int point2 = 1;
        // for (int p = 1; p <= n; p++)
        //     cout << p<<"vis:" << vis[p] << endl;
        // for (int p = 1; p <= n; p++)
        //     cout << p<<"isOdd:" << isOdd[p] << endl;
        // cout << "success" << endl;
        if (flag)
        {
            for (int p = 1; p <= n; p++)
            {
                if (isOdd[p])
                {
                    while (vis[point1])
                    {
                        point1++;
                    }
                    cout << point1 << " ";
                    point1++;
                }
                else
                {
                    while (!vis[point2])
                    {
                        point2++;
                    }
                    cout << point2 << " ";
                    point2++;
                }
            }
        }
        else
        {
            for (int p = 1; p <= n; p++)
            {
                if (isOdd[p])
                {
                    while (!vis[point1])
                    {
                        point1++;
                    }
                    cout << point1 << " ";
                    point1++;
                }
                else
                {
                    while (vis[point2])
                    {
                        point2++;
                    }
                    cout << point2 << " ";
                    point2++;
                }
            }
        }
        // cout << "point1:" << point1 << " point2:" << point2 << endl;
        cout << endl;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值