ps:写在飞书上的,对于不想看英文的同学很不友好,就先用中文说一说思路:首先题目要求我们对一个树上的结点进行重新relabel使 第一轮保证Eikooc胜利的情况他能够选择的结点最多,首先猜测有一种relabeling的方法使无论Eikooc无论选择哪一个结点,另一个人不能走第二步,这就要求对于所有的满足?的结点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: 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 s.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;
}
}
|