Codeforces Round #395 (Div. 1) A. Timofey and a tree(深搜)
Description
Each New Year Timofey and his friends cut down a tree of n vertices and bring it home. After that they paint all the n its vertices, so that the i-th vertex gets color ci.
Now it's time for Timofey birthday, and his mother asked him to remove the tree. Timofey removes the tree in the following way: he takes some vertex in hands, while all the other vertices move down so that the tree becomes rooted at the chosen vertex. After that Timofey brings the tree to a trash can.
Timofey doesn't like it when many colors are mixing together. A subtree annoys him if there are vertices of different color in it. Timofey wants to find a vertex which he should take in hands so that there are no subtrees that annoy him. He doesn't consider the whole tree as a subtree since he can't see the color of the root vertex.
A subtree of some vertex is a subgraph containing that vertex and all its descendants.
Your task is to determine if there is a vertex, taking which in hands Timofey wouldn't be annoyed.
Input
The first line contains single integer n (2 ≤ n ≤ 105) — the number of vertices in the tree.
Each of the next n - 1 lines contains two integers u and v (1 ≤ u, v ≤ n, u ≠ v), denoting there is an edge between vertices u and v. It is guaranteed that the given graph is a tree.
The next line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 105), denoting the colors of the vertices.
Output
Print "NO" in a single line, if Timofey can't take the tree in such a way that it doesn't annoy him.
Otherwise print "YES" in the first line. In the second line print the index of the vertex which Timofey should take in hands. If there are multiple answers, print any of them.
Sample Input
4
1 2
2 3
3 4
1 2 1 1
3
1 2
2 3
1 2 3
4
1 2
2 3
3 4
1 2 1 2
Sample Output
YES
2
YES
2
NO
思路
题意:给定一棵树,每个结点有一种颜色,是否存在一个结点,以它为根,使得这棵树的每个子树仅有一种颜色。
题解:方法一:若这个树每个结点颜色相同,则肯定存在;若有不同颜色结点,我们只需随便找到两个相邻的不同颜色的结点,分别以之为根搜索看其每个子树是否只有一种颜色,若这两种情况都不成立,则最后可推之肯定不存在这样的结点。
方法二:换一个结点使成为根后,其子树所有结点颜色相同,那么如果这样的树存在说明了,与这个结点相连的结点都跟这个结点颜色不相同,因此找出结点连接数等于相邻节点颜色不同的总数,这个点可调整成为根。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #include<bits/stdc++.h> using namespace std; const int maxn = 100005; int color[maxn]; vector< int >itv[maxn]; bool ok; int curr_color; void dfs( int v, int fa){ ok = ok & (curr_color == color[v]); for ( auto u : itv[v]){ if (u == fa) continue ; dfs(u,v); } } bool solve( int root){ bool res = true ; for ( auto u : itv[root]){ curr_color = color[u]; ok = true ; dfs(u,root); res = res & ok; } return res; } int main(){ int n,u,v,root1 = -1,root2 = -1; scanf ( "%d" ,&n); for ( int i = 1;i < n;i++){ scanf ( "%d%d" ,&u,&v); itv[u].push_back(v); itv[v].push_back(u); } for ( int i = 1;i <= n;i++) scanf ( "%d" ,&color[i]); for ( int i = 0;i < n;i++){ for ( auto u: itv[i]){ if (color[u] != color[i]){ root1 = u; root2 = i; } } } if (root1 == -1){ printf ( "YES\n1" ); } else if (solve(root1)) printf ( "YES\n%d\n" ,root1); else if (solve(root2)) printf ( "YES\n%d\n" ,root2); else printf ( "NO\n" ); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #include<bits/stdc++.h> using namespace std; const int maxn = 100005; int color[maxn],degree[maxn]; struct Edge{ int u,v; }edge[maxn]; int main(){ int n,cnt = 0; scanf ( "%d" ,&n); for ( int i = 0;i < n - 1;i++){ scanf ( "%d%d" ,&edge[i].u,&edge[i].v); } for ( int i = 1;i <= n;i++) scanf ( "%d" ,&color[i]); for ( int i = 0;i < n - 1;i++){ if (color[edge[i].u] != color[edge[i].v]){ cnt++; degree[edge[i].u]++; degree[edge[i].v]++; } } bool flag = false ; for ( int i = 1;i <= n;i++){ if (degree[i] == cnt){ printf ( "YES\n%d\n" ,i); flag = true ; break ; } } if (!flag){ printf ( "NO\n" ); } return 0; } |
┆ 凉 ┆ 暖 ┆ 降 ┆ 等 ┆ 幸 ┆ 我 ┆ 我 ┆ 里 ┆ 将 ┆ ┆ 可 ┆ 有 ┆ 谦 ┆ 戮 ┆ 那 ┆ ┆ 大 ┆ ┆ 始 ┆ 然 ┆
┆ 薄 ┆ 一 ┆ 临 ┆ 你 ┆ 的 ┆ 还 ┆ 没 ┆ ┆ 来 ┆ ┆ 是 ┆ 来 ┆ 逊 ┆ 没 ┆ 些 ┆ ┆ 雁 ┆ ┆ 终 ┆ 而 ┆
┆ ┆ 暖 ┆ ┆ 如 ┆ 地 ┆ 站 ┆ 有 ┆ ┆ 也 ┆ ┆ 我 ┆ ┆ 的 ┆ 有 ┆ 精 ┆ ┆ 也 ┆ ┆ 没 ┆ 你 ┆
┆ ┆ 这 ┆ ┆ 试 ┆ 方 ┆ 在 ┆ 逃 ┆ ┆ 会 ┆ ┆ 在 ┆ ┆ 清 ┆ 来 ┆ 准 ┆ ┆ 没 ┆ ┆ 有 ┆ 没 ┆
┆ ┆ 生 ┆ ┆ 探 ┆ ┆ 最 ┆ 避 ┆ ┆ 在 ┆ ┆ 这 ┆ ┆ 晨 ┆ ┆ 的 ┆ ┆ 有 ┆ ┆ 来 ┆ 有 ┆
┆ ┆ 之 ┆ ┆ 般 ┆ ┆ 不 ┆ ┆ ┆ 这 ┆ ┆ 里 ┆ ┆ 没 ┆ ┆ 杀 ┆ ┆ 来 ┆ ┆ ┆ 来 ┆
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)