首先,我们将题目理解成若\(i\)与\(j\)距离恰好为\(3\),则不可能\(p_i \equiv p_j \equiv 1 \space or \space 2 (\bmod 3)\)。这就相当于我们要构造一个大小为\([\frac{n + 1}{3}]\)的点集\(A_2\),用来放所有模3余2的数,再构造一个大小为\([\frac{n + 2}{3}]\)的点集\(A_1\),用来放所有模3余1的数。需要满足这两个集合交集为空,且若\(i\)与\(j\)距离为\(3\),则它们不在同一个集合内。

发现距离为\(3\)的点连成的图性质是不好的,唯一好的性质就是:它是二分图。我们不妨大胆地把条件加强为:构造这样的点集\(A_1, A_2\),使得它们全部再二分图的同一边。

我们把树按深度奇偶染色,不妨设染一种颜色的点数为\(X\),另一种为\(Y\),且\(X \leq Y\)。

若\(X \ge [\frac{n + 1}{3}]\)且\(Y \ge [\frac{n + 2}{3}]\),我们就在\(X\)中任意取\([\frac{n + 1}{3}]\)个点作为\(A_2\),在\(Y\)中任意取\([\frac{n + 2}{3}]\)个点作为\(A_1\)即可。

否则我们容易证明\(Y \ge [\frac{n + 1}{3}] + [\frac{n + 2}{3}]\),在\(Y\)中任意找两个集合的点即可。

代码如下:

#include <bits/stdc++.h>
using namespace std; const int N = 200005; template <class T>
void read (T &x) {
int sgn = 1;
char ch;
x = 0;
for (ch = getchar(); (ch < '0' || ch > '9') && ch != '-'; ch = getchar()) ;
if (ch == '-') ch = getchar(), sgn = -1;
for (; '0' <= ch && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
x *= sgn;
}
template <class T>
void write (T x) {
if (x < 0) putchar('-'), write(-x);
else if (x < 10) putchar(x + '0');
else write(x / 10), putchar(x % 10 + '0');
} struct edge {
int to, nxt;
} tree[N << 1];
int n, col[N], head[N], perm[N], cnt = 0, cnt0 = 0, cnt1 = 0;
void addedge (int u, int v) {
edge e = {v, head[u]};
tree[head[u] = cnt++] = e;
} void dfs (int u, int fa) {
for (int i = head[u]; ~i; i = tree[i].nxt) {
int v = tree[i].to;
if (v != fa) {
col[v] = col[u] ^ 1;
dfs(v, u);
}
}
} int main () {
read(n);
for (int i = 1; i <= n; i++) head[i] = -1; for (int i = 1; i < n; i++) {
int u, v;
read(u), read(v);
addedge(u, v), addedge(v, u);
}
col[1] = 0, dfs(1, 0); for (int i = 1; i <= n; i++) {
if (col[i] == 0) cnt0++;
else cnt1++;
} if (cnt0 > cnt1) {
for (int i = 1; i <= n; i++) col[i] ^= 1;
swap(cnt0, cnt1);
}
int bound0 = n / 3, bound1 = (n + 1) / 3, bound2 = (n + 2) / 3;
if (cnt0 >= bound1 && cnt1 >= bound2) {
int tot0 = 0, tot1 = 0, tot2 = 0;
for (int i = 1; i <= n; i++) {
if (col[i] == 0) {
if (tot2 >= bound1) perm[i] = 3 * ++tot0;
else perm[i] = 3 * ++tot2 - 1;
}
else {
if (tot1 >= bound2) perm[i] = 3 * ++tot0;
else perm[i] = 3 * ++tot1 - 2;
}
}
}
else {
int tot0 = 0, tot1 = 0, tot2 = 0;
for (int i = 1; i <= n; i++) {
if (col[i] == 0) perm[i] = 3 * ++tot0;
else {
if (tot1 < bound2) perm[i] = 3 * ++tot1 - 2;
else if (tot2 < bound1) perm[i] = 3 * ++tot2 - 1;
else perm[i] = 3 * ++tot0;
}
}
}
for (int i = 1; i <= n; i++) write(perm[i]), putchar(' ');
putchar('\n');
return 0;
}

Social Infrastructure Information Systems Division, Hitachi Programming Contest 2020 C题题解的更多相关文章

  1. Social Infrastructure Information Systems Division, Hitachi Programming Contest 2020 D题题解

    将题意转换为一开始\(t = 0\),第\(i\)个操作是令\(t \leftarrow (a_i + 1) t + (a_i + b_i + 1)\).记\(A_i = a_i + 1, B_i = ...

  2. HHKB Programming Contest 2020 D - Squares 题解(思维)

    题目链接 题目大意 给你一个边长为n的正方形和边长为a和b的正方形,要求把边长为a和b的正方形放在长度为n的正方形内,且没有覆盖(可以相邻)求有多少种放法(mod 1e9+7) 题目思路 这个思路不是 ...

  3. (寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest(爽题)

    layout: post title: (寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest(爽题) author: " ...

  4. M-SOLUTIONS Programming Contest 2020 题解

    M-SOLUTIONS Programming Contest 2020 题解 目录 M-SOLUTIONS Programming Contest 2020 题解 A - Kyu in AtCode ...

  5. 2021.7.27--Benelux Algorithm Programming Contest 2020 补提

    I Jigsaw 题目内容: 链接:https://ac.nowcoder.com/acm/contest/18454/I 来源:牛客网 You have found an old jigsaw pu ...

  6. Yahoo Programming Contest 2019 补题记录(DEF)

    D - Ears 题目链接:D - Ears 大意:你在一个\(0-L\)的数轴上行走,从整数格出发,在整数格结束,可以在整数格转弯.每当你经过坐标为\(i-0.5\)的位置时(\(i\)是整数),在 ...

  7. 2017 ACM Arabella Collegiate Programming Contest div2的题,部分题目写个题解

    F. Monkeying Around   维护点在多少个线段上 http://codeforces.com/gym/101350/problem/F 题意:有m个笑话,每个笑话的区间是[L, R], ...

  8. 带权并查集:CF-2015 ACM Arabella Collegiate Programming Contest(F题)

    F. Palindrome Problem Description A string is palindrome if it can be read the same way in either di ...

  9. atcoder Keyence Programming Contest 2020 题解

    比赛地址 A 题意:给一个\(n*m\)的初始为白色的矩阵,一次操作可以将一行或一列染成 黑色,问至少染出\(k\)个黑点的最少操作次数. \(n\),\(m\)<=100,\(k\)<= ...

随机推荐

  1. spring中的事务有两种方式

    1种是我们常用的声明式事务,如注解,或者配置文件配置的. 2种是编程式事务,如 TransactionTemplate 类的使用.

  2. Linux(centos6.8)配置Tomcat环境

    1.下载Linux版的Tomcat包 (1)通过官方下载 tomcat官方:https://tomcat.apache.org/download-80.cgi (2)通过分享下载 如网盘分享等途径 2 ...

  3. Maven项目关系

    Maven是一个项目管理工具,它包含了一个项目对象模型 (Project Object Model),其中最重要的就是POM文件,可以指定项目类型,项目关系等信息,maven项目之间有三种关系. 依赖 ...

  4. UML中常见的类关系你了解吗?

    最近老大给我设计了一个微信扫码登录的通过工具包流程图,设计过程中使用了模板模式.面向接口编程等设计思路,让我很享受整个过程:下来我就接触了一下Java的设计模式,很是懵懂,听说这也是要靠经验来喂,才能 ...

  5. HDU100题简要题解(2070~2079)

    HDU2070 Fibbonacci Number 题目链接 Problem Description Your objective for this question is to develop a ...

  6. [原题复现+审计][ZJCTF 2019] WEB NiZhuanSiWei(反序列化、PHP伪协议、数组绕过)

    简介  原题复现:https://github.com/CTFTraining/zjctf_2019_final_web_nizhuansiwei/  考察知识点:反序列化.PHP伪协议.数组绕过   ...

  7. webug第一关:很简单的一个注入

    第一关:很简单的一个注入 上单引号报错 存在注入,用order  by猜列的个数 union select 出现显示位 查数据库版本,用户和当前数据库名 查表名和列名 最后,激动人心的拿flag

  8. Hadoop大数据平台搭建之前期配置(2)

    环境:CentOS 7.4 (1708  DVD) 工具:VMware.MobaXterm 一. 克隆大数据集群 1. 选中已经进行了基本配置的虚拟机,进行克隆. 2. 此处改为"创建完整克 ...

  9. JavaSE 学习笔记01丨开发前言与环境搭建、基础语法

    本蒟蒻学习过C/C++的语法,故在学习Java的过程中,会关注于C++与Java的区别.开发前言部分,看了苏星河教程中的操作步骤.而后,主要阅读了<Java核心技术 卷1 基础知识>(第8 ...

  10. Java基础教程——String类

    String类 Java程序中的所有字符串字面值(如 "abc" )都是String的实例 字符串是常量(因为 String 对象是不可变的,所以可以共享) 字符串的本质是字符数组 ...