IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> Codeforces Round #754 (Div. 2) problem: (D) -> 正文阅读

[数据结构与算法]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;
    }
}

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-11-14 21:58:23  更:2021-11-14 21:59:15 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 10:26:56-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码