C. Queen Codeforces Round #549 (Div. 2) dfs
1 second
256 megabytes
standard input
standard output
You are given a rooted tree with vertices numerated from 11 to nn. A tree is a connected graph without cycles. A rooted tree has a special vertex named root.
Ancestors of the vertex ii are all vertices on the path from the root to the vertex ii, except the vertex ii itself. The parent of the vertex ii is the nearest to the vertex ii ancestor of ii. Each vertex is a child of its parent. In the given tree the parent of the vertex ii is the vertex pipi. For the root, the value pipi is −1−1.
An example of a tree with n=8n=8, the root is vertex 55. The parent of the vertex 22 is vertex 33, the parent of the vertex 11 is vertex 55. The ancestors of the vertex 66 are vertices 44 and 55, the ancestors of the vertex 77 are vertices 88, 33 and 55
You noticed that some vertices do not respect others. In particular, if ci=1ci=1, then the vertex ii does not respect any of its ancestors, and if ci=0ci=0, it respects all of them.
You decided to delete vertices from the tree one by one. On each step you select such a non-root vertex that it does not respect its parent and none of its children respects it. If there are several such vertices, you select the one with the smallest number. When you delete this vertex vv, all children of vv become connected with the parent of vv.
An example of deletion of the vertex 77.
Once there are no vertices matching the criteria for deletion, you stop the process. Print the order in which you will delete the vertices. Note that this order is unique.
The first line contains a single integer nn (1≤n≤1051≤n≤105) — the number of vertices in the tree.
The next nn lines describe the tree: the ii-th line contains two integers pipi and cici (1≤pi≤n1≤pi≤n, 0≤ci≤10≤ci≤1), where pipi is the parent of the vertex ii, and ci=0ci=0, if the vertex ii respects its parents, and ci=1ci=1, if the vertex ii does not respect any of its parents. The root of the tree has −1−1 instead of the parent index, also, ci=0ci=0 for the root. It is guaranteed that the values pipi define a rooted tree with nn vertices.
In case there is at least one vertex to delete, print the only line containing the indices of the vertices you will delete in the order you delete them. Otherwise print a single integer −1−1.
5
3 1
1 1
-1 0
2 1
3 0
1 2 4
5
-1 0
1 1
1 1
2 0
3 0
-1
8
2 1
-1 0
1 0
1 1
1 1
4 0
5 1
7 0
5
The deletion process in the first example is as follows (see the picture below, the vertices with ci=1ci=1 are in yellow):
- first you will delete the vertex 11, because it does not respect ancestors and all its children (the vertex 22) do not respect it, and 11 is the smallest index among such vertices;
- the vertex 22 will be connected with the vertex 33 after deletion;
- then you will delete the vertex 22, because it does not respect ancestors and all its children (the only vertex 44) do not respect it;
- the vertex 44 will be connected with the vertex 33;
- then you will delete the vertex 44, because it does not respect ancestors and all its children (there are none) do not respect it (vacuous truth);
- you will just delete the vertex 44;
- there are no more vertices to delete.

In the second example you don't need to delete any vertex:
- vertices 22 and 33 have children that respect them;
- vertices 44 and 55 respect ancestors.

In the third example the tree will change this way:
这个题目也比较简单,就是一个dfs,我发现一般的图都可以用dfs解决,这个题目看起来还比较可怕,实际很简单啦。看代码吧。
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 100;
struct node
{
int to, dist;
node(int to=0,int dist=0):to(to),dist(dist){}
};
vector<node>vec[maxn];
int cnt=0,a[maxn]; int dfs(int s,int d)
{
int flag = 1;
int len = vec[s].size();
// if (len == 0 && d) return 1;
// if (len == 0 && !d) return 0;
for(int i=0;i<len;i++)
{
if (dfs(vec[s][i].to, vec[s][i].dist) == 0) flag = 0;
}
if (d)
{
//printf("%d %d %d\n",cnt, s, d);
if(flag) a[cnt++] = s;
return 1;
}
return 0;
} int main()
{
int n;
cin >> n;
int root = 0;
for(int i=1;i<=n;i++)
{
int b, x;
cin >> b >> x;
if (b == -1) root = i;
else
{
vec[b].push_back(node(i, x));
}
}
cnt = 0;
int ans=dfs(root,0);
// printf("cnt=%d\n", cnt);
sort(a, a+cnt);
if(cnt==0)
{
printf("-1\n");
return 0;
}
for(int i=0;i<cnt-1;i++)
{
printf("%d ", a[i]);
}
printf("%d\n", a[cnt - 1]);
return 0;
}
C. Queen Codeforces Round #549 (Div. 2) dfs的更多相关文章
- C. Queen Codeforces Round #549 (Div. 2) (搜索)
---恢复内容开始--- You are given a rooted tree with vertices numerated from 11 to nn . A tree is a connect ...
- [题解] Codeforces Round #549 (Div. 2) B. Nirvana
Codeforces Round #549 (Div. 2) B. Nirvana [题目描述] B. Nirvana time limit per test1 second memory limit ...
- Codeforces Round #549 (Div. 1)
今天试图用typora写题解 真开心 参考 你会发现有很多都是参考的..zblzbl Codeforces Round #549 (Div. 1) 最近脑子不行啦 需要cf来缓解一下 A. The B ...
- Codeforces Round #549 (Div. 2)C. Queen
C. Queen time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- B. Nirvana Codeforces Round #549 (Div. 2) (递归dfs)
---恢复内容开始--- Kurt reaches nirvana when he finds the product of all the digits of some positive integ ...
- Codeforces Round #549 (Div. 2) 训练实录 (5/6)
The Doors +0 找出输入的01数列里,0或者1先出完的的下标. Nirvana +3 输入n,求1到n的数字,哪个数逐位相乘的积最大,输出最大积. 思路是按位比较,从低到高,依次把小位换成全 ...
- Codeforces Round #549 (Div. 2) Solution
传送门 A.The Doors 看懂题目就会写的题 给一个 $01$ 序列,找到最早的位置使得 $0$ 或 $1$ 已经全部出现 #include<iostream> #include&l ...
- CodeForces Round #549 Div.2
A. The Doors 代码: #include <bits/stdc++.h> using namespace std; ; int N; , One = ; int a[maxn], ...
- [ Codeforces Round #549 (Div. 2)][D. The Beatles][exgcd]
https://codeforces.com/contest/1143/problem/D D. The Beatles time limit per test 1 second memory lim ...
随机推荐
- PHP 依赖注入和控制反转再谈(二)
今天有个朋友看到yii2中介绍的依赖注入一头雾水,之前我写过类似的文章发给他看了,可能还没深入理解吧,这里我再通俗点描述下依赖注入的原理吧,尽可能滴说通俗易懂一点吧:先还是扯下概念性滴问题(概念问题我 ...
- (8)Microsoft office Word 2013版本操作入门_制作传单海报
1.纸张大小,方向设定. 1.1纸张大小: [页面布局]----[纸张大小] 可以选择已有的尺寸,也可以选择其他自定义的大小. 1.2 方向设定: [页面布局]--[纸张方向]选择 横向或者纵向 2. ...
- SpringMVC表单验证与Velocity整合
阅读本文约“1.2分钟” 定义表单类 以Login为例,有username和password两个字段 import javax.validation.constraints.NotNull; impo ...
- js动画 Css提供的运动 js提供的运动
1. 动画 (1) Css样式提供了运动 过渡的属性transition 从一种情况到另一种情况叫过渡 Transition:attr time linear delay: ...
- 用Python实现Zabbix-API 监控
做运维的朋友应该知道,公司IDC机房经常有上架.下架.报修和报废的服务器.如果服务器数量很多的时候很容易造成监控遗漏. 大的互联网公司把监控系统和CMDB(资产管理系统|配置管理数据库系统 ...
- Spring的xml解析原理分析【转载】
一:前言 二:spring的配置文件 三:依赖的第三方库.使用技术.代码布局 四:Document实现 五:获取Element的实现 六:解析Element元素 七:Bean创造器 八:Ioc容器的创 ...
- 课程作业——熟悉常用的Linux操作
cd命令:切换目录 (1) 切换到目录 /usr/local cd /usr/local (2) 去到目前的上层目录 cd .. (3) 回到自己的主文件夹 cd ~ ls命令:查看文件与目录 (4) ...
- 如何将字符串格式的对象转换成真正的js对象?
1.如何将字符串格式的对象转换成真正的js对象? <script>//eval 的作用eval('var a = 100');console.log(a);</script> ...
- JavaScript之Number、String、Array常用属性与方法手册
Number isFinite函数 Number.isFinite() 方法用来检测传入的参数是否是一个有穷数(finite number). 语法: Number.isFinite(value) 例 ...
- 2018-11-29 VS Code英汉词典插件v0.0.6-改为TS实现, 加测试
如前文VS Code英汉词典插件v0.0.4-驼峰下划线命名打算, 首先将JS源码改为TypeScript实现, 并添加了必要的测试. 昨天得知vue.js 3.0会用TypeScript实现, 正好 ...