C. Timofey and a tree 观察题 + dfs模拟
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模拟的更多相关文章
- Codeforces 763A. Timofey and a tree
A. Timofey and a tree 题意:给一棵树,要求判断是否存在一个点,删除这个点后,所有连通块内颜色一样.$N,C \le 10^5$ 想法:这个叫换根吧.先求出一个点合法即其儿子的子树 ...
- 763A - Timofey and a tree
A. Timofey and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- 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 ...
- Hackonacci Matrix Rotations 观察题 ,更新了我的模板
https://www.hackerrank.com/contests/w27/challenges/hackonacci-matrix-rotations 一开始是没想到观察题的.只想到直接矩阵快速 ...
- 【codeforces 764C】Timofey and a tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 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 ...
- Vijos P1114 FBI树【DFS模拟,二叉树入门】
描述 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串. FBI树是一种二叉树1,它的结点类型也包括F结点,B结点和I结点三种 ...
- poj 1005:I Think I Need a Houseboat(水题,模拟)
I Think I Need a Houseboat Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 85149 Acce ...
- HDU 5438 Ponds dfs模拟
2015 ACM/ICPC Asia Regional Changchun Online 题意:n个池塘,删掉度数小于2的池塘,输出池塘数为奇数的连通块的池塘容量之和. 思路:两个dfs模拟就行了 # ...
随机推荐
- 微信小程序 常见问题 小结
1.微信小程序 尺寸单位 rpx单位是微信小程序中css的尺寸单位,rpx可以根据屏幕宽度进行自适应.规定屏幕宽为750rpx.如在 iPhone6 上,屏幕宽度为375px,共有750个物理像素,则 ...
- bug集合及其解决方法
点击查看原文 1. java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 1 column 27 ...
- java里int类型转byte类型
今天在做书上的一个例子的时候, 要使用byte类型,首先我很直接的就写到了byte b = 0XAA, 结果报错, 说从int转换到byte可能会有损失. 我当时就很奇怪, 为什么会出现这种情况呢? ...
- JavaScript语句-流程控制语句
JavaScript定义了一组语句,语句通常用于执行一定的任务.语句可以很简单,也可以很复杂. 选择结构,可以在程序中创建交叉结构来指定程序流的可能方向.JavaScript中有四种选择结构: 1.单 ...
- UVa 12403 - Save Setu
题目:有两种操作:1.当前数值添加.2.输出当前数值. 分析:简单题.模拟就可以. 说明:A+B. #include <iostream> #include <cstdlib> ...
- 设置Table边框的CSS
<!DOCTYPE html> <html> <head> <style> table, td, th { border: 1px solid blac ...
- Lightoj 1003 - Drunk(拓扑排序)
One of my friends is always drunk. So, sometimes I get a bit confused whether he is drunk or not. So ...
- YTU 1098: The 3n + 1 problem
1098: The 3n + 1 problem 时间限制: 1 Sec 内存限制: 64 MB 提交: 368 解决: 148 题目描述 Consider the following algor ...
- mysql sakila 执行失败
1.下载 https://dev.mysql.com/doc/index-other.html 2.解压 3.将解压的文件放入某个位置,必须tmp下面 4.登录mysql 进行source处理 mys ...
- codeforces 688A A. Opponents(水题)
题目链接: A. Opponents time limit per test 1 second memory limit per test 256 megabytes input standard i ...