Codeforces Round #379 (Div. 2) E. Anton and Tree
题意:
给一颗树 每个节点有黑白2色
可以使一个色块同事变色,问最少的变色次数。
思路:
先缩点 把一样颜色的相邻点 缩成一个
然后新的树 刚好每一层是一个颜色。
最后的答案就是树的直径/2
不过我用的树上的dp,强行求了以每个点为根时树的深度
答案就是最小的深度-1
具体见代码:
const int maxn = + ;
int n;
int color[maxn];
int pa[maxn];
vector<int> G[maxn], G2[maxn];
void init()
{
scanf("%d", &n);
for (int i = ; i <= n; i++)
{
scanf("%d", color + i);
}
int u, v;
for (int i = ; i < n; i++)
{
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
} int find(int x)
{
return pa[x] != x ? pa[x] = find(pa[x]) : x;
} int fa[maxn];
void getTree()
{
queue<int> q;
q.push();
color[] = -;
while (!q.empty())
{
int u = q.front(); q.pop();
if (color[fa[u]] == color[u]) pa[u] = find(fa[u]);
else G2[fa[pa[u]]].push_back(u);
for (int i = ; i < G[u].size(); i++)
{
int v = G[u][i];
if (find(v) == find(fa[u])) continue;
fa[v] = pa[u];
q.push(v);
}
}
swap(G, G2);
} void pg()
{
cout << "Graph:" << endl;
for (int i = ; i <= n; i++)
{
for (int j = ; j < G[i].size(); j++)
{
cout << i << " " << G[i][j] << endl;
}
}
} int son1[maxn], son2[maxn]; //i节点的最大的儿子 和 次大的儿子的下标 int deep[maxn];
int deepFa[maxn];//i的父亲除了i以外的最深深度 int d[maxn];//以i为根时树的深度 d[i] = max(deep[i], deepFa[i] + 1) int dfs(int u) //得到每个节点最深儿子的深度
{
deep[u] = ;
for (int i = ; i < G[u].size(); i++)
{
int v = G[u][i];
fa[v] = u;
int tmp = dfs(v);
if (tmp >= deep[u])
{
son2[u] = son1[u];
son1[u] = v;
deep[u] = tmp;
}
else
{
if (tmp > deep[son2[u]]) son2[u] = v;
}
}
deep[u]++;
return deep[u];
} int bfs()
{
queue<int> q;
for (int i = ; i < G[].size(); i++) q.push(G[][i]);
int ans = d[] = deep[];
while (!q.empty())
{
int u = q.front(); q.pop();
if (son1[fa[u]] == u) deepFa[u] = deep[son2[fa[u]]] + ;
else deepFa[u] = deep[son1[fa[u]]] + ; deepFa[u] = max(deepFa[u], deepFa[fa[u]] + ); d[u] = max(deep[u], deepFa[u] + );
ans = min(ans, d[u]);
for (int i = ; i < G[u].size(); i++)
{
q.push(G[u][i]);
}
}
return ans - ;
} void solve()
{
for (int i = ; i <= n; i++) pa[i] = i;
getTree();
memset(fa, -, sizeof(fa));
for (int i = ; i <= n; i++) son1[i] = son2[i] = n + ;
dfs();
cout << bfs() << endl;
} int main()
{
init();
solve();
return ;
}
Codeforces Round #379 (Div. 2) E. Anton and Tree的更多相关文章
- 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 —— 缩点 + 树上最长路
题目链接:http://codeforces.com/contest/734/problem/E E. Anton and Tree time limit per test 3 seconds mem ...
- 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 ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 树的直径
传送门 题意: 这道题说的是在一颗有两种颜色的树上,每操作一个节点,可以改变这个节点颜色和相邻同色节点的颜色.问最少操作次数,使得树上颜色相同. 思路: 先缩点,把相同的颜色的相邻节点缩在一起.再求出 ...
- Codeforces Round #379 (Div. 2) D. Anton and Chess 水题
D. Anton and Chess 题目连接: http://codeforces.com/contest/734/problem/D Description Anton likes to play ...
- Codeforces Round #379 (Div. 2) C. Anton and Making Potions 枚举+二分
C. Anton and Making Potions 题目连接: http://codeforces.com/contest/734/problem/C Description Anton is p ...
- Codeforces Round #379 (Div. 2) B. Anton and Digits 水题
B. Anton and Digits 题目连接: http://codeforces.com/contest/734/problem/B Description Recently Anton fou ...
- Codeforces Round #379 (Div. 2) A. Anton and Danik 水题
A. Anton and Danik 题目连接: http://codeforces.com/contest/734/problem/A Description Anton likes to play ...
- Codeforces Round #379 (Div. 2) D. Anton and Chess 模拟
题目链接: http://codeforces.com/contest/734/problem/D D. Anton and Chess time limit per test4 secondsmem ...
随机推荐
- UpdatePanel里的Repeater和DropDownList
在updatepanel里使用dropdownlist的AutoPostBack,正常情况下都可以局部刷新. 但是,如果updatepanel下是Repeater,repeater里绑定dropdow ...
- 如何解决Selenium中"Cannot find function addEventListener in object [object HTMLDocument]"的错误
project: blog target: how-to-resolve-cannot-find-function-addEventListener-error-in-selenium.md stat ...
- cocos2d-x 常用UI
CCSprite* sprite = CCSprite::create("CloseNormal.png"); sprite->setPosition(ccp(50, 50) ...
- CxImage图像库的使用 .
http://blog.csdn.net/wangjie0377/article/details/7830405 CxImage图像库 CxImage下载地址:http://www.codeproje ...
- 禁用 baloo_file_extractor 加速 ubuntu 14.04 (KDE)
在复制了一堆零散文件后,系统同然变得奇卡,看看cpu和ram都占用不高,但看到这个进程 baloo_file_extractor 时不时地冒一下泡,怀疑是它在频繁访问硬盘.禁止它自动启动的方式: $ ...
- sudo apt-get install apache2 php7.0 php7.0-mysql mysql-server
sudo apt-get install apache2 php7.0 php7.0-mysql mysql-server sudo apt-get install libapache2-mod-ph ...
- BZOJ 3270 && BZOJ 1778 (期望DP && 高斯消元)
BZOJ 3270 :设置状态为Id(x,y)表示一人在x,一人在y这个状态的概率. 所以总共有n^2种状态. p[i]表示留在该点的概率,Out[i]=(1-p[i])/Degree[i]表示离开该 ...
- js/jquery 回调函数的定义方法
基本写法: 带参数的回调函数 以上回调函数,直接传入function作为参数,同样,还可以传入json对象作为参数...如下. 该方法的优势是可以定义多个回调函数....类似$.ajax回调函数中的s ...
- Android 终于官方支持按百分比来设置控件的宽高了
dependencies { compile 'com.android.support:percent:22.2.0' } 支持布局 PercentRelativeLayout <android ...
- vb小程序浅析
系统 : Windows xp 程序 : BJCM10B 程序下载地址 :http://pan.baidu.com/s/1dFyXe29 要求 : 编写注册机 使用工具 : OD 可在看雪论坛中查找关 ...