Codeforces734 E. Anton and Tree
传送门:>Here<
题意:给出一颗树,节点不是黑色就是白色,每次可以将一个颜色相同的块变颜色,问最少变几次才能让其变为同色
解题思路:
我们考虑由于每一次都是把同样颜色的色块进行变色,因此同样颜色的色块可以看成一个点。所以我们先将同一个色块缩成一个点。
然后我们有一个结论,我们最后的答案就是缩点完成的这棵树的直径+1再除以2.
我们很容易发现,缩点完成以后的树相邻的两个点颜色一定是不同的,否则就能继续缩。因此我们可以每次选择直径中间的那个点,改变它的颜色,然后它就与周围的那些点融合成为一个新的点,然后再找到中间的,继续重复如上步骤。最后我们会发现,恰好是$(dis+1) / 2$次。这个证明不是很严谨,不过感性地理解一下吧
Code
细节不多
/*by DennyQi*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#define r read()
#define Max(a,b) (((a)>(b))?(a):(b))
#define Min(a,b) (((a)<(b))?(a):(b))
using namespace std;
typedef long long ll;
const int MAXN = ;
const int INF = 0x3f3f3f3f;
const int MOD = ;
inline int read(){
int x = ; int w = ; register unsigned char c = getchar();
for(; c^'-' && (c < '' || c > ''); c = getchar());
if(c == '-') w = -, c = getchar();
for(; c >= '' && c <= ''; c = getchar()) x = (x<<) + (x<<) + c - '';
return x * w;
}
int N,cur_num;
int color[MAXN],nod[MAXN],x[MAXN],y[MAXN],d[MAXN],vis[MAXN];
vector <int> g[MAXN],G[MAXN];
queue <int> q;
inline void add(int u, int v){
g[u].push_back(v);
}
inline void Add(int u, int v){
G[u].push_back(v);
}
void dfs(int u){
int sz = g[u].size(),v;
nod[u] = cur_num;
for(int i = ; i < sz; ++i){
v = g[u][i];
if(!nod[v] && (color[v] == color[u])){
dfs(v);
}
}
}
inline void BFS(int s){
memset(d, 0x3f, sizeof(d));
memset(vis,,sizeof(vis));
d[s] = ;
q.push(s);
vis[s] = ;
int u,sz,v;
while(!q.empty()){
u = q.front();q.pop();
sz = G[u].size();
for(int i = ; i < sz; ++i){
v = G[u][i];
if(!vis[v]){
vis[v] = ;
d[v] = d[u] + ;
q.push(v);
}
}
}
}
int main(){
// freopen(".in","r",stdin);
N=r;
for(int i = ; i <= N; ++i){
color[i]=r;
}
for(int i = ; i < N; ++i){
x[i]=r,y[i]=r;
add(x[i], y[i]);
add(y[i], x[i]);
}
for(int i = ; i <= N; ++i){
if(!nod[i]){
++cur_num;
dfs(i);
}
}
for(int i = ; i < N; ++i){
if(nod[x[i]] != nod[y[i]]){
Add(nod[x[i]], nod[y[i]]);
Add(nod[y[i]], nod[x[i]]);
}
}
/* for(int i = 1; i <= cur_num; ++i){
printf("%d: ", i);
for(int j = 0; j < G[i].size(); ++j){
printf("%d,",G[i][j]);
}
printf("\n");
}*/
BFS();
int _max = -,p;
for(int i = ; i <= cur_num; ++i){
if(d[i] > _max){
_max = d[i];
p = i;
}
}
/* printf("p = %d\n", p);
for(int i = 1; i <= cur_num; ++i){
printf("%d ",d[i]);
}
printf("\n");*/
BFS(p);
_max = -;
for(int i = ; i <= cur_num; ++i){
if(d[i] > _max){
_max = d[i];
}
}
/* for(int i = 1; i <= cur_num; ++i){
printf("%d ",d[i]);
}
printf("\n");
printf("_max = %d\n", _max);*/
printf("%d",(_max+)/);
return ;
}
Codeforces734 E. Anton and Tree的更多相关文章
- Codeforces 734E. Anton and Tree 搜索
E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 直径
E. Anton and Tree 题目连接: http://codeforces.com/contest/734/problem/E Description Anton is growing a t ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree 树的直径
E. Anton and Tree time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...
- Anton and Tree
Anton and Tree 题目链接:http://codeforces.com/contest/734/problem/E DFS/BFS 每一次操作都可以使连通的结点变色,所以可以将连通的点缩成 ...
- Codeforces 734E Anton and Tree(缩点+树的直径)
题目链接: Anton and Tree 题意:给出一棵树由0和1构成,一次操作可以将树上一块相同的数字转换为另一个(0->1 , 1->0),求最少几次操作可以把这棵数转化为只有一个数字 ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree —— 缩点 + 树上最长路
题目链接:http://codeforces.com/contest/734/problem/E E. Anton and Tree time limit per test 3 seconds mem ...
- 【27.91%】【codeforces 734E】Anton and Tree
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree
题意: 给一颗树 每个节点有黑白2色 可以使一个色块同事变色,问最少的变色次数. 思路: 先缩点 把一样颜色的相邻点 缩成一个 然后新的树 刚好每一层是一个颜色. 最后的答案就是树的直径/2 不过我用 ...
- cf734 E. Anton and Tree
这个题的题意还是很劲的.搞了好久才知道是怎么变得. (假设已经缩好了点,每次边中间颜色不同的,然后和就和他外面的相同,继续再变这个大的,依次类推,最多就是树的直径D/2) (还是英语水平太弱了(吐槽+ ...
随机推荐
- Jenkins - 构建Allure Report
前言 本文为Pytest+Allure定制报告进阶篇,集成Jenkins,在Jenkins中直接生成报告,更方便测试人员查看. 一.安装插件allure-jenkins-plugin 1.进入系统管理 ...
- c++入门之类——进一步剖析
通常的,关于一个类,包含了下面几个方面: 1 声明类成员和接口:2 定义类接口函数(方法)3通过接口调用类 下面先给出第一条:声明类成员和接口 # ifndef MYTIME0_H_ # defin ...
- openstack-KVM管理工具
一. virsh 通过libvirt API管理Hpervisor.node.domain,实现多数功能调用. 即统一管理多台计算机上的域. 1.管理其他服务器(node) (1)修改配置文件:vim ...
- mysql_查的小理解
show create table employee; 对这个语句的小理解: 顿悟呀,之前一直不太理解这条语句,现在忽然觉得明朗起来.他就是展示创建这个表格时的SQL语句.执行上述代码之后结果如下: ...
- CRM系统(第四部分)
阅读目录 1.引入权限组件rbac 2.分配权限 3.登录.引入中间件 1.引入权限组件rbac 1.settings配置app.中间件 INSTALLED_APPS = [ ... ... ...
- Linux 典型应用之服务管理
crontab 定时任务 用户所建立的crontab文件中,每一行都代表一项任务,每行的每个字段代表一项设置,它的格式共分为六个字段,前五段是时间设定段,第六段是要执行的命令段,格式如下: minut ...
- JAVAString初始化的引用问题
1 String a="Hello JAVA"; 2 3 String b=a; 4 5 System.out.println(a); 6 7 System.out.println ...
- 关于标准的知识 GB ISO 等内容
1. 来自百度知道: GB:GB 即"国标"的汉语拼音缩写,为中华人民共和国国家标准的意思. ISO:国际标准化组织的英语简称.其全称是International Organiza ...
- Oracle字符函数length substr concat实例
--字符函数 --伪表dual --(1)求字符串长度 select length('123.456/-*') from dual --(2)截取函数求字符串的子串 ,) from dual --(3 ...
- Day 4-8 hashlib加密模块
HASH Hash,一般翻译做“散列”,也有直接音译为”哈希”的,就是把任意长度的输入(又叫做预映射,pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值.这种转换是一种压缩映射 ...