http://codeforces.com/contest/764/problem/C

题意:在n个顶点中随便删除一个,然后分成若干个连通子图,要求这若干个连通子图的颜色都只有一种。

记得边是双向的,wa15的可能是不知道边是双向的吧。

一个观察:如果某条边连接的两个顶点的颜色不同,那么可以看看删除这两个顶点,成立就成立,不成立就不成立。

因为必定要把这两个顶点分开。

然后就是暴力dfs了。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset> const int maxn = 2e6 + ;
struct node {
int u, v, tonext;
}e[maxn];
int num;
int first[maxn];
void add(int u, int v) {
num++;
e[num].u = u;
e[num].v = v;
e[num].tonext = first[u];
first[u] = num;
}
int c[maxn], in[maxn];
int u[maxn], v[maxn];
set<int>ss;
int vis[maxn];
int DFN;
int dfs(int cur, int no) {
ss.insert(c[cur]);
if (ss.size() >= ) return false;
bool flag = true;
for (int i = first[cur]; i; i = e[i].tonext) {
if (e[i].v == no) continue;
if (vis[e[i].v] == DFN) continue;
vis[e[i].v] = DFN;
flag = flag && dfs(e[i].v, no);
}
return flag;
}
bool del(int cur) {
for (int i = first[cur]; i; i = e[i].tonext) {
ss.clear();
DFN++;
vis[cur] = DFN;
vis[e[i].v] = DFN;
if (dfs(e[i].v, inf) == false) return false;
}
return true;
}
void work() {
int n;
cin >> n;
for (int i = ; i <= n - ; ++i) {
cin >> u[i] >> v[i];
}
for (int i = ; i <= n; ++i) {
cin >> c[i];
}
int which = inf;
for (int i = ; i <= n - ; ++i) {
if (c[u[i]] != c[v[i]] && which == inf) which = i;
add(u[i], v[i]);
add(v[i], u[i]);
}
if (which == inf) {
cout << "YES" << endl;
cout << << endl;
return;
}
// cout << which << " " << root << endl;
if (del(u[which])) {
cout << "YES" << endl;
cout << u[which] << endl;
return;
}
if (del(v[which])) {
cout << "YES" << endl;
cout << v[which] << endl;
return;
}
cout << "NO" << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

C. Timofey and a tree 观察题 + dfs模拟的更多相关文章

  1. Codeforces 763A. Timofey and a tree

    A. Timofey and a tree 题意:给一棵树,要求判断是否存在一个点,删除这个点后,所有连通块内颜色一样.$N,C \le 10^5$ 想法:这个叫换根吧.先求出一个点合法即其儿子的子树 ...

  2. 763A - Timofey and a tree

    A. Timofey and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  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. Hackonacci Matrix Rotations 观察题 ,更新了我的模板

    https://www.hackerrank.com/contests/w27/challenges/hackonacci-matrix-rotations 一开始是没想到观察题的.只想到直接矩阵快速 ...

  5. 【codeforces 764C】Timofey and a tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. Codeforces 764C Timofey and a tree

    Each New Year Timofey and his friends cut down a tree of n vertices and bring it home. After that th ...

  7. Vijos P1114 FBI树【DFS模拟,二叉树入门】

    描述 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串. FBI树是一种二叉树1,它的结点类型也包括F结点,B结点和I结点三种 ...

  8. poj 1005:I Think I Need a Houseboat(水题,模拟)

    I Think I Need a Houseboat Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 85149   Acce ...

  9. HDU 5438 Ponds dfs模拟

    2015 ACM/ICPC Asia Regional Changchun Online 题意:n个池塘,删掉度数小于2的池塘,输出池塘数为奇数的连通块的池塘容量之和. 思路:两个dfs模拟就行了 # ...

随机推荐

  1. RAM、ROM和磁盘

     计算机存储数据的存储器主要分为RAM(随机訪问存储器).ROM.磁盘. RAM又分为SRAM和DRAM两种,SRAM用作快速缓存,DRAM用作主存. 1.SRAM SRAM又被称为静态RAM.利 ...

  2. 【Java 安全技术探索之路系列:J2SE安全架构】之二:安全管理器

    作者:郭嘉 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells github:https://github.com/AllenWell 一 ...

  3. linux中用anaconda使用不同版本python

    1.使用命令conda create --name python36 python=3.6  #你想使用哪个版本就下载哪个版本,--name后面跟的是该虚拟环境的名称 2.需要使用python3.6时 ...

  4. Highcharts报表——让你的网页上图表画的飞起

    Highcharts是一款纯javascript编写的图表库,能够很简单便捷的在Web网站或Web应用中添加交互性的图表,Highcharts目前支持直线图.曲线图.面积图.柱状图.饼图.散点图等多达 ...

  5. 关于Address already in use: connect问题分析及解决方案

    最近给一个公司做项目的时候,在完成上报的功能 的时候,发现数据量稍微大的时候,会出现这样的问题: 错误描述: com.microsoft.sqlserver.jdbc.SQLServerExcepti ...

  6. iPhone开发关于UDID和UUID的一些理解【转】

    原文地址:http://blog.csdn.net/xunyn/article/details/13629071 一.UDID(Unique Device Identifier) UDID是Uniqu ...

  7. 大话设计模式C++实现-第14章-观察者模式

    一.UML图 关键词:Subject维护一个Observer列表.Subject运行Notify()时就运行列表中的每一个Observer的Update(). 二.概念 观察者模式:定义了一种一对多的 ...

  8. SSH无密码验证可能出现的问题

    雪影工作室版权所有,转载请注明[http://blog.csdn.net/lina791211] 一.安装和启动SSH协议 假设没有安装ssh和rsync,可以通过下面命令进行安装. sudo apt ...

  9. XMU 1605 nc与数列 【动态规划】

    1605: nc与数列 Time Limit: 2000 MS  Memory Limit: 64 MBSubmit: 84  Solved: 13[Submit][Status][Web Board ...

  10. YTU 2428: C语言习题 计算该日在本年中是第几天

    2428: C语言习题 计算该日在本年中是第几天 时间限制: 1 Sec  内存限制: 128 MB 提交: 1505  解决: 857 题目描述 定义一个结构体变量(包括年.月.日).编写一个函数d ...