Codeforces Round #395 (Div. 2) C
题意 : 给出一颗树 每个点都有一个颜色 选一个点作为根节点 使它的子树各自纯色
我想到了缩点后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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #395 (Div. 1)
比赛链接:http://codeforces.com/contest/763 A题: #include <iostream> #include <cstdio> #includ ...
- Codeforces Round #395 (Div. 2)(未完)
2.2.2017 9:35~11:35 A - Taymyr is calling you 直接模拟 #include <iostream> #include <cstdio> ...
- Codeforces Round #395 (Div. 2)
今天自己模拟了一套题,只写出两道来,第三道时间到了过了几分钟才写出来,啊,太菜了. A. Taymyr is calling you 水题,问你在z范围内 两个序列 n,2*n,3*n...... ...
- 【分类讨论】Codeforces Round #395 (Div. 2) D. Timofey and rectangles
D题: 题目思路:给你n个不想交的矩形并别边长为奇数(很有用)问你可以可以只用四种颜色给n个矩形染色使得相接触的 矩形的颜色不相同,我们首先考虑可不可能,我们分析下最多有几个矩形互相接触,两个时可以都 ...
- 【树形DP】Codeforces Round #395 (Div. 2) C. Timofey and a tree
标题写的树形DP是瞎扯的. 先把1看作根. 预处理出f[i]表示以i为根的子树是什么颜色,如果是杂色的话,就是0. 然后从根节点开始转移,转移到某个子节点时,如果其子节点都是纯色,并且它上面的那一坨结 ...
随机推荐
- hoj 2715 (费用流 拆点)
http://acm.hit.edu.cn/hoj/problem/view?id=2715 将每个格子 i 拆成两个点 i’, i’’并加边(i’, i’’, 1, -Vi), (i’, i’’, ...
- 男神的约会(状压dp)
有一天男神约了学姐姐去看电影,电影院有一个活动,给你一个10*10的矩阵,每一个格子上都有一个0-9的整数,表示一共十种优惠券中的一种. 观众从左上角的格子开始走,走到右下角.每走到一个有着a号优惠券 ...
- C#处理MySql多个返回集
关于Mysql返回多个集java和Php的较多,但是C#的完整代码好像没见过,研究了一下做个封装以后用 做一个Mysql的简单分页查询,有两个返回集 Sql语句如下 SELECT COUNT(*) f ...
- ASIHttprequest 报错
(void)requestReceivedResponseHeaders:(NSMutableDictionary *)newResponseHeaders { if ([self error] || ...
- asp.net mvc4连接mysql
环境:vs2013+mysql5.6+mysql connector for .net 6.8.3+MySQL for Visual Studio 1.1.3 参考:http://dev.mysql. ...
- API网关性能比较:NGINX vs. ZUUL vs. Spring Cloud Gateway vs. Linkerd API 网关出现的原因
API网关性能比较:NGINX vs. ZUUL vs. Spring Cloud Gateway vs. Linkerd http://www.infoq.com/cn/articles/compa ...
- 解决Initializing Java Tooling 和 Initializing Java Tooling卡死问题
遇到了如题的问题,使用如下方法解决了 工作中eclipse崩溃,再次启动后,状态栏一直显示 Initializing Javascript Tooling . 解决方案: 删除\workspace\ ...
- Security Report: Stop using relative path to import CSS files
Detecting and exploiting path-relative stylesheet import (PRSSI) vulnerabilities Early last year G ...
- Django视图views--白话聊Django系列
继续看上图,讲完控制器后,我们接下来看看视图部分 客户发来请求,首先经过控制器,然后到达视图,所以视图负责接收请求和作出响应,所以在视图里只需要关注两个:HttpRequest和HttpRespons ...
- css选择器的权重
权重会叠加!