Codeforces Round #379 (Div. 2) E. Anton and Tree

时间:2023-02-18 08:15:35

题意:
给一颗树 每个节点有黑白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的更多相关文章

  1. Codeforces Round &num;379 &lpar;Div&period; 2&rpar; E&period; Anton and Tree 缩点 直径

    E. Anton and Tree 题目连接: http://codeforces.com/contest/734/problem/E Description Anton is growing a t ...

  2. Codeforces Round &num;379 &lpar;Div&period; 2&rpar; E&period; Anton and Tree —— 缩点 &plus; 树上最长路

    题目链接:http://codeforces.com/contest/734/problem/E E. Anton and Tree time limit per test 3 seconds mem ...

  3. Codeforces Round &num;379 &lpar;Div&period; 2&rpar; E&period; Anton and Tree 树的直径

    E. Anton and Tree time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...

  4. Codeforces Round &num;379 &lpar;Div&period; 2&rpar; E&period; Anton and Tree 缩点 树的直径

    传送门 题意: 这道题说的是在一颗有两种颜色的树上,每操作一个节点,可以改变这个节点颜色和相邻同色节点的颜色.问最少操作次数,使得树上颜色相同. 思路: 先缩点,把相同的颜色的相邻节点缩在一起.再求出 ...

  5. Codeforces Round &num;379 &lpar;Div&period; 2&rpar; D&period; Anton and Chess 水题

    D. Anton and Chess 题目连接: http://codeforces.com/contest/734/problem/D Description Anton likes to play ...

  6. Codeforces Round &num;379 &lpar;Div&period; 2&rpar; C&period; Anton and Making Potions 枚举&plus;二分

    C. Anton and Making Potions 题目连接: http://codeforces.com/contest/734/problem/C Description Anton is p ...

  7. Codeforces Round &num;379 &lpar;Div&period; 2&rpar; B&period; Anton and Digits 水题

    B. Anton and Digits 题目连接: http://codeforces.com/contest/734/problem/B Description Recently Anton fou ...

  8. Codeforces Round &num;379 &lpar;Div&period; 2&rpar; A&period; Anton and Danik 水题

    A. Anton and Danik 题目连接: http://codeforces.com/contest/734/problem/A Description Anton likes to play ...

  9. Codeforces Round &num;379 &lpar;Div&period; 2&rpar; D&period; Anton and Chess 模拟

    题目链接: http://codeforces.com/contest/734/problem/D D. Anton and Chess time limit per test4 secondsmem ...

随机推荐

  1. &lbrack;CLR via C&num;&rsqb;16&period; 数组

    数组是允许将多个数据项当作一个集合来处理的机制.CLR支持一维数组.多维数组和交错数据(即由数组构成的数组).所有数组类型都隐式地从System.Array抽象类派生,后者又派生自System.Obj ...

  2. &lbrack;译&rsqb;SQL Server 之 查询优化器

    因为生成查询计划的代价比较大,所以查询计划将会被缓存. 树形结构 SQL 查询首先被转化为树形结构,每个节点都是一个查询操作.例如: SELECT * FROM Customers C INNER J ...

  3. gc 辅助打印信息

    cat xx.xml|grep GC <jvm-arg>-XX:+PrintGCDetails</jvm-arg> <jvm-arg>-XX:+PrintGCDat ...

  4. 单元测试之获取Spring下所有Bean

    单元测试中,针对接口的测试是必须的,但是如何非常方便的获取Spring注册的Bean呢? 如果可以获取所有的Bean,这样就可以将这个方法放到基类中,方便后面所有单元测试类的使用,具体实现如下: im ...

  5. ASP&period;NET MVC – 关于Action返回结果类型的事儿(上)

    原文:ASP.NET MVC – 关于Action返回结果类型的事儿(上) 本文转自:博客园-文超的技术博客 一.         ASP.NET MVC 1.0 Result 几何? Action的 ...

  6. 2015 11 27编写JAVA程序

    在任意文件下 ,建立一个文本文档,更改其txt格式为java格式, 打开此程序的同时打开eclipse可编写代码. public class 文件名{ public static void main( ...

  7. city-picker插件使用-移动h5三级联动

    首先访问该链接:http://www.jq22.com/jquery-info12914 看看是否是你要找的三级联动插件,(主要看注释的部分!) 好了,不知道是不是我傻,没有找到初始化数据的方法,本人 ...

  8. Protocol Buffers(1):序列化、编译与使用

    目录 序列化与反序列化 Protocol Buffers概览 Protocol Buffers C++ 编译 Protocol Buffers C++ 使用 Protocol Buffers的可读性 ...

  9. First Unique Character in a String

    Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...

  10. gcc各个版本下载

    http://www.gnu.org/order/ftp.html http://ftp.gnu.org/gnu/gcc/