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. ldd

    ldd命令用于判断某个可执行的 binary 档案含有什么动态函式库 [diego@localhost ~/work/branch_dispatch_201511/rtqa_center/source ...

  2. Python中的shelve模块

    shelve中有用的函数就是open(),但是下面编写的数据库函数中调用路径是经常出错,如果直接调用一个从来没有用过的文件却能正常运行,暂时没有找出原因. 调用shelve.open()会返回一个sh ...

  3. 【转】SQL中的取整函数FLOOR、ROUND、CEIL、TRUNC、SIGN

    --------------------------------------------------------------------------1 trunc(value,precision)按精 ...

  4. 【转载】C# 装箱和拆箱[整理]

    1.      装箱和拆箱是一个抽象的概念 2.      装箱是将值类型转换为引用类型 :拆箱是将引用类型转换为值类型       利用装箱和拆箱功能,可通过允许值类型的任何值与Object 类型的 ...

  5. 【IOS】启动画面

    总述: 两种方式,一种是使用系统自带的.按规则定义启动图片名称就可以,显示为1秒,要想延长时间,用[nsthread ​ sleepForTimeInterval:5.0] ,还有一种就是自己定义ui ...

  6. 一些java错误

    @Override must override a superclass method 问题解决 如果在使用Eclipse开发Java项目时,在使用 @Override 出现以下错误: The met ...

  7. Hibernate中二级缓存指的是什么?

    一.一级缓存.二级缓存的概念解释 (1)一级缓存就是Session级别的缓存,一个Session做了一个查询操作,它会把这个操作的结果放在一级缓存中,如果短时间内这个 session(一定要同一个se ...

  8. git 团队代码管理交流共同进步

    Installation methods for GitLab | GitLab https://about.gitlab.com/installation/#centos-7 gittutorial ...

  9. Spring Cloud 学习总结001-服务治理-Eureka

    学习参考:http://blog.didispace.com/Spring-Cloud%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B/ spring cloud由[服务注册中 ...

  10. [NOI 2012] 美食节

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2879 [算法] 首先 , 将每种食物建一个点 , 将每位厨师做的每一道菜建一个点 建 ...