题意 : 给出一颗树 每个点都有一个颜色 选一个点作为根节点 使它的子树各自纯色

我想到了缩点后check直径 当<=3的时候可能有解 12必定有解 3的时候需要check直径中点的组成点里是否有一个点连接了另外所有的点 如果有就是ans 没有就是no

这个方法是对了 不过比较麻烦..需要很多check

但是看div1的做法 有很棒的姿势用来解这个题

扫一下所有的边 如果两边的点颜色不一样 就增加一个连通分量数 这样可以很方便的算出有多少连通分量 同时记录这个点连着多少个其余的颜色(连通分量)

然后扫一下所有的点 看哪个点连着其余的连通分量 如果存在就是ans 没有就是no

果然还是应当多看一下别人的解法来学习 大写的服字 QAQ

我的迷茫解法 : 

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define L long long
int n ;
vector<int >q[100050] ;
int c[100050] ;
int id[100050] ;
int gm[100050];
int cnt ;
void bfs(int s) {
queue<int >que ;
que.push(s) ;
while(!que.empty()) {
int u = que.front();
que.pop();
for(int i = 0; i <q[u].size(); i ++ ) {
int v = q[u][i];
if(id[v] == -1 && c[v] == c[u]) {
id[v] = cnt ;
gm[cnt] ++ ;
que.push(v) ;
}
}
}
}
vector<int >w[100050] ;
int vis[100050] ;
int res[100050];
int main(){
scanf("%d",&n) ;for(int i = 1; i <=n;i++)q[i].clear();
for(int i = 1; i < n ; i ++ ) {
int u , v;
scanf("%d%d",&u,&v);
q[u].push_back(v);
q[v].push_back(u);
}
for(int i = 1; i<=n;i++)scanf("%d",&c[i]) ;
cnt = 0;
memset(id,-1,sizeof(id)) ;
memset(gm,0,sizeof(gm));
for(int i = 1; i <= n ;i ++ ) {
if(id[i]==-1){
cnt ++ ;
id[i] = cnt ;
gm[cnt] = 1;
bfs(i) ;
res[cnt] = i ;
}
}
if(cnt == 1) {
printf("YES\n");
printf("1\n");
return 0 ;
}
if(cnt == 2) {
printf("YES\n");
for(int i = 1; i <= n ; i ++ ){
for(int j = 0 ; j < q[i].size() ; j ++) {
int v = q[i][j] ;
if(c[i]!=c[v]) {
printf("%d\n",i);
return 0;
}
}
}
}
for(int i = 1; i <= cnt ; i ++ )w[i].clear() ;
for(int i = 1 ; i <= n ; i ++ ){
for(int k = 0; k < q[i].size(); k ++ ){
int v = q[i][k] ;
if(id[v] != id[i]) {
int uu = id[i] ;
int vv = id[v] ;
w[uu].push_back(vv);
}
}
}
int d = 0;
int ans = 0;
for(int i = 1; i <= cnt ;i ++ ){
if(w[i].size() >= 2 ){
d ++ ;
if(d > 1){
ans = -1;
break ;
}
else {
ans = i ;
}
}
}
if(ans!= -1 && d == 1){
int rr = -1;
int cn = 0;
for(int i = 1 ; i <= n ; i ++ ) {
if(id [i] == ans ){
int bb = 0;
for (int j = 0 ; j < q[i].size(); j ++ ){
int v = q[i][j] ;
if(id[v] != id[i])bb ++ ;
}
if(bb == w[ans].size()) {
rr = i ;
cn ++ ;
}
}
}
if(cn == 1) {
printf("YES\n");
printf("%d\n",rr) ;
}
else printf("NO\n") ;
}
else printf("NO\n") ;
}

学习的姿势 : 

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define L long long
int x[100050] ;
int y[100050] ;
int c[100050] ;
int n ;
int a[100050] ;
int main(){
scanf("%d",&n) ;
for(int i = 1; i < n ; i ++ ){
scanf("%d%d",&x[i] , &y[i]) ;
}
for(int i = 1; i <= n ; i ++ ){
scanf("%d",&c[i]) ;
}
memset(a , 0 , sizeof(a)) ;
int b = 0 ;
for(int i = 1 ; i < n ; i ++ ){
if(c[x[i]] != c[y[i]]) {
b ++ ;
a[x[i]] ++ ;
a[y[i]] ++ ;
}
}
int ans = -1 ;
for(int i = 1 ; i <= n ; i ++ ){
if(a[i] == b) {
ans = i;
break ;
}
}
if(ans == -1 )printf("NO\n") ;
else {
printf("YES\n");
printf("%d\n",ans) ;
}
}

  

Codeforces Round #395 (Div. 2) C的更多相关文章

  1. Codeforces Round #395 (Div. 2)(A.思维,B,水)

    A. Taymyr is calling you time limit per test:1 second memory limit per test:256 megabytes input:stan ...

  2. Codeforces Round #395 (Div. 2) D. Timofey and rectangles

    地址:http://codeforces.com/contest/764/problem/D 题目: D. Timofey and rectangles time limit per test 2 s ...

  3. Codeforces Round #395 (Div. 2) C. Timofey and a tree

    地址:http://codeforces.com/contest/764/problem/C 题目: C. Timofey and a tree time limit per test 2 secon ...

  4. Codeforces Round #395 (Div. 2)B. Timofey and cubes

    地址:http://codeforces.com/contest/764/problem/B 题目: B. Timofey and cubes time limit per test 1 second ...

  5. Codeforces Round #395 (Div. 1)

    比赛链接:http://codeforces.com/contest/763 A题: #include <iostream> #include <cstdio> #includ ...

  6. Codeforces Round #395 (Div. 2)(未完)

    2.2.2017 9:35~11:35 A - Taymyr is calling you 直接模拟 #include <iostream> #include <cstdio> ...

  7. Codeforces Round #395 (Div. 2)

    今天自己模拟了一套题,只写出两道来,第三道时间到了过了几分钟才写出来,啊,太菜了. A. Taymyr is calling you 水题,问你在z范围内  两个序列  n,2*n,3*n...... ...

  8. 【分类讨论】Codeforces Round #395 (Div. 2) D. Timofey and rectangles

    D题: 题目思路:给你n个不想交的矩形并别边长为奇数(很有用)问你可以可以只用四种颜色给n个矩形染色使得相接触的 矩形的颜色不相同,我们首先考虑可不可能,我们分析下最多有几个矩形互相接触,两个时可以都 ...

  9. 【树形DP】Codeforces Round #395 (Div. 2) C. Timofey and a tree

    标题写的树形DP是瞎扯的. 先把1看作根. 预处理出f[i]表示以i为根的子树是什么颜色,如果是杂色的话,就是0. 然后从根节点开始转移,转移到某个子节点时,如果其子节点都是纯色,并且它上面的那一坨结 ...

随机推荐

  1. ubuntu虚拟化平台libvrit-bin

    http://download.cirros-cloud.net/0.3.5/ 下载 cirros-0.3.5-x86_64-disk.img 因为 KVM(准确说是 Libvirt)默认不接受远程管 ...

  2. [HAOI2012] 容易题[母函数]

    794. [HAOI2012] 容易题 ★★☆   输入文件:easy.in   输出文件:easy.out   简单对比时间限制:1 s   内存限制:128 MB 秒 输入:easy.in 输出: ...

  3. 《从零开始学Swift》学习笔记(Day 35)——会使用下标吗?

    原创文章,欢迎转载.转载请注明:关东升的博客 看下面的示例代码是不是使用过: var studentList: String[] = ["张三","李四",&q ...

  4. 爬虫实战【6】Ajax内容解析-今日头条图集

    Ajax技术 AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). Ajax并不是新的编程语言,而是一种使用现有标准的新方法,当然 ...

  5. 自制的几个jquery插件

    1.颜色插件,比用css方便些 //1.插件编写 ;(function ($) { $.fn.extend({ "color":function(value){ return th ...

  6. Error: member names cannot be the same as their enclosing type

    在编译的时候会遇到如下问题:member names cannot be the same as their enclosing type 原因:方法名和类名不能一样,如果一样就是一个构造函数.而构造 ...

  7. 关于:before :after

    首先要明白一种思想:结构和样式分离. 结构和样式分离,就意味着:没有样式表,HTML文档也是一个完整的文档:没有样式表,也能正常阅读用HTML表达的所有内容.明白这种思想就能很好理解样式表中使用--- ...

  8. 什么是lambda函数?它有什么好处?

    lambda 函数是一个可以接收任意多个参数(包括可选参数)并且返回单个表达式值的函数. lambda 函数不能包含命令,它们所包含的表达式不能超过一个.不要试图向lambda 函数中塞入太多的东西: ...

  9. unknown encoder libvpx

    brew install ffmpeg --with-libvpx or brew reinstall ffmpeg --with-libvpx

  10. 024-Spring Boot 应用的打包和部署

    一.概述 二.手工打包[不推荐] 打包命令:maven clean package 打包并导出依赖:maven clean package dependency:copy-dependencies 1 ...