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. 然后从根节点开始转移,转移到某个子节点时,如果其子节点都是纯色,并且它上面的那一坨结 ...
随机推荐
- 扩展类 HOW TO EXTEND CLASSES TO MAKE NEW CLASSES IN PYTHON
How to Extend Classes to Make New Classes in Python - dummies https://www.dummies.com/programming/py ...
- Difference between .classpath and MANIFEST.MF
What is the difference between adding a dependency jar to the .classpath file in a RAD project and a ...
- cxGrid时间格式与导出Excel
引用cxFormats单元: ShortDateFormat := 'dd/mm/yyyy'; DateSeparator := '/'; cxFormatController.UseDelphiDa ...
- AWS入门-1
对于 Amazon Linux AMI,用户名为 ec2-user. 对于 RHEL AMI,用户名称是 ec2-user 或 root. 对于 Ubuntu AMI,用户名称是 ubuntu 或 r ...
- MySQL中哈希表
也称为散列表 由直接寻址表改进而来.先看直接寻址表 当关键字的全域U比较小时,直接寻址是一种简单而有效的技术.加入某应用要用到一个动态集合,其中每个元素都有一个取自全域U={0,1,...,m-1}的 ...
- jQuery解决鼠标单双击问题
html代码如下: <button>点击</button> JQ代码如下: <script> $(function () { // 编写相关jQuery代码 // ...
- 007-搭建框架-开发AOP框架
一.代码地址 https://github.com/bjlhx15/smart-framework.git 二.代码编写 2.1.定义切面注解 增加Aspect注解 package com.lhx.s ...
- Python学习笔记2_Python基础
一.变量(给数据起个名字) 变量是计算机内存中的一块区域,变量可以存储规定范围内的值,而且值可以改变. 1.变量的命名方法 -变量名有字母.数字.下划线组成 -不能以数字开头 -不可以使用关键字 -a ...
- Java - 在控制台中执行一个可执行jar
1.Maven打包一个可执行jar: <build> <plugins> <plugin> <groupId>org.apache.maven.plug ...
- JSP页面传递参数乱码问题整理
1.JSP页面之间传递中文参数乱码 (1).a.jsp中正常传递参数,b.jsp 中 <% String projectName = new String(request.getParamete ...