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

我想到了缩点后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. rest规范 ; restful 风格; gradel介绍 ; idea安装 ;

    [说明]上午整理了一下心情:下午继续开始任务,了解了restful,知道了那个牛人的博士论文,下载了管理工具gradle,并且部署了环境:晚上安装了idea继承环境并且建了一个简单的gradle项目( ...

  2. 【BZOJ3994】[SDOI2015]约数个数和 莫比乌斯反演

    [BZOJ3994][SDOI2015]约数个数和 Description  设d(x)为x的约数个数,给定N.M,求   Input 输入文件包含多组测试数据. 第一行,一个整数T,表示测试数据的组 ...

  3. 爬虫入门【8】Python连接MongoDB的用法简介

    MongoDB的连接和数据存取 MongoDB是一种跨平台,面向文档的NoSQL数据库,提供高性能,高可用性并且易于扩展. 包含数据库,集合,文档等几个重要概念. 我们在这里不介绍MongoDB的特点 ...

  4. DropdownList异步刷新GridView数据

    前台代码: <div style=" clear:both; width:800px; text-align:center; margin-left:auto; margin-righ ...

  5. 如何枚举 Windows 顶级桌面窗口?

    bool is_top_level_window(HWND hwnd) { if (!IsWindow(hwnd)) return false; DWORD dw_style = GetWindowL ...

  6. 简述Python的深浅拷贝以及应用场景

    深浅拷贝的原理 深浅拷贝用法来自copy模块. 导入模块:import copy 浅拷贝:copy.copy 深拷贝:copy.deepcopy 字面理解:浅拷贝指仅仅拷贝数据集合的第一层数据,深拷贝 ...

  7. Mac下最好用的文本编辑器

    友情提醒:图多杀猫. 曾经在Windows下一直用gVim.能够用键盘控制一切,操作起来是又快又爽,还支持一大堆插件.想怎么玩就怎么玩.后来转Mac后,也沿袭着之前的习惯.一直在用终端的Vim.偶尔会 ...

  8. linux 安装zip/unzip/g++/gdb/vi/vim等软件

    近期公司新配置了一台64位云server.去部署的时候发现,没有安装zip/unzip压缩解压软件. 于是仅仅好自己安装这两个软件.linux最好用的还是yum. 两个指令就安装好了. 首先把软件安装 ...

  9. 剑指offer 面试47题

    面试47题:题:礼物的最大价值 题目:在一个mxn的棋盘的每一格都放有一个礼物,每个礼物都有一定的价值(价值大于0),你可以从棋盘的左上角开始拿格子里的礼物,并每次向右或者向下移动一格,直到到达棋盘的 ...

  10. Java 如何读取resources

    Sample in Github 1.一般使用Maven创建Java工程,代码文件在src/main/java文件夹中,资源文件在src/main/resources文件夹中,Java代码为什么可以读 ...