C. Queen Codeforces Round #549 (Div. 2) (搜索)
---恢复内容开始---
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
 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=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 .
 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≤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 1 , because it does not respect ancestors and all its children (the vertex 2 ) do not respect it, and 1 is the smallest index among such vertices;
- the vertex 2 will be connected with the vertex 3 after deletion;
- then you will delete the vertex 2 , because it does not respect ancestors and all its children (the only vertex 4 ) do not respect it;
- the vertex 4 will be connected with the vertex 3 ;
- then you will delete the vertex 4 , 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 4 ;
- there are no more vertices to delete.
题意:给你一棵树,每个节点有c值标记,当这个节点的被标记而且它儿子节点都被标记,就删除这个节点,有多个这样的节点就删除优先删除最小的,直到所有都无法删除。
思路:可以bfs直接从上到下删除,也可以dfs从下到上删除,显然删除并不会影响其他该删除的节点
bfs:
#include<bits/stdc++.h>
using namespace std; const int maxn = 1e5+; int n;
int p[maxn],c[maxn];
struct Node
{
int to,next;
Node(int x=,int y=):to(x),next(y){}
}node[maxn];
int head[maxn];
int ans[maxn];
int cnt,tot;
void add(int x,int y)
{
node[++cnt].to = y;
node[cnt].next = head[x];
head[x] = cnt;
} void bfs(int s)
{
queue<int>que;
while(!que.empty())que.pop();
que.push(s);
while(!que.empty())
{
int t = que.front();
que.pop();
int k = ;
int id=t;
for(int i=head[t];i;i=node[i].next)
{
int to =node[i].to;
if(!c[to])k = ;
que.push(to);
}
if(k && c[t])ans[++tot] = id;
}
}
int main()
{
scanf("%d",&n);
int s;
cnt = tot = ;
for(int i=;i<=n;i++)
{
scanf("%d%d",&p[i],&c[i]);
if(p[i] == -)s = i;
else add(p[i],i);
}
bfs(s);
sort(ans+,ans++tot);
if(!tot)printf("-1\n");
else
{
for(int i=;i<=tot;i++)
{
printf("%d ",ans[i]);
}
puts("");
}
}
dfs:
#include<bits/stdc++.h>
using namespace std; int n;
const int maxn = 1e5+;
int head[maxn];
int ans[maxn];
int p[maxn],c[maxn];
struct Node
{
int to,next;
Node(int to=,int next=):to(to),next(next){}
}node[maxn];
int cnt,tot;
void add(int x,int y)
{
node[++cnt].to = y;
node[cnt].next = head[x];
head[x] = cnt;
} void dfs(int s,bool turn)
{
int k=;
for(int i=head[s];i;i=node[i].next)
{
int to = node[i].to;
if(!c[to])k=;
dfs(to,c[to]);
}
if(k && turn)ans[++tot] = s;
} int main()
{
scanf("%d",&n);
int s;
cnt = tot = ;
for(int i=;i<=n;i++)
{
scanf("%d%d",&p[i],&c[i]);
if(p[i] == -)s=i;
else add(p[i],i);
}
dfs(s,);
sort(ans+,ans+tot+);
if(tot)
for(int i=;i<=tot;i++)printf("%d ",ans[i]);
else printf("-1\n");
}
C. Queen Codeforces Round #549 (Div. 2) (搜索)的更多相关文章
- C. Queen   Codeforces Round #549 (Div. 2)  dfs
		C. Queen time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ... 
- [题解] 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 ... 
- Codeforces Round #549 (Div. 2) 训练实录 (5/6)
		The Doors +0 找出输入的01数列里,0或者1先出完的的下标. Nirvana +3 输入n,求1到n的数字,哪个数逐位相乘的积最大,输出最大积. 思路是按位比较,从低到高,依次把小位换成全 ... 
- 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 ... 
- Codeforces Round #549 (Div. 1) 题解
		link 前几天补完了某一场很早以前的div1,突然想来更博客,于是就有了这篇文章 A The Beatles 显然若起点和第一次到达的位置距离为 d ,那么经过的不同站点数为 $\frac{nk}{ ... 
- Codeforces Round #549 (Div. 2)  Solution
		传送门 A.The Doors 看懂题目就会写的题 给一个 $01$ 序列,找到最早的位置使得 $0$ 或 $1$ 已经全部出现 #include<iostream> #include&l ... 
随机推荐
- poj  3764  The xor-longest Path  (01 Trie)
			链接:http://poj.org/problem?id=3764 题面: The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K ... 
- 【JVM】关于类加载器准备阶段的一道面试题目
			一个经典的延伸问题 我们来看一个经典的延伸问题,准备阶段谈到静态变量,那么对于常量和不同静态变量有什么区别? 需要明确的是,没有人能够精确的理解和记忆所有信息,如果碰到这种问题,有直接答案当然最好:没 ... 
- Linux-GitLab安装及汉化
			gitlab 安装及汉化 GitLab简介: GitLab是一个用于仓库管理系统的开源项目.使用Git作为代码管理工具,并在此基础上搭建起来的Web服务.可通过Web界面进行访问公开的或者私人项目.它 ... 
- Lightning Conductor 洛谷P3515 决策单调性优化DP
			遇见的第一道决策单调性优化DP,虽然看了题解,但是新技能√,很开森. 先%FlashHu大佬,反正我是看了他的题解和精美的配图才明白的,%%%巨佬. 废话不多说,看题: 题目大意 已知一个长度为n的序 ... 
- apache http跳转到https代码
			<VirtualHost *:> ServerAdmin webmasterexample.com DocumentRoot "/mnt/www/" ServerNam ... 
- 20165232 实现pwd
			20165232 实现mypwd 题目要求 学习pwd命令 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 实现mypwd 测试mypwd 学习pwd命令 用man pwd 查看 ... 
- RocketMQ_问题_启动控制台console报错,connect to <null> failed
			配置如图所示: 问题如图所示: 明明配置了nameserver参数为啥还是connect to null呢? 我的rocketmq部署在CentOS7虚拟机上,原来是没关闭防火墙,执行:systemc ... 
- MySQL查看SQL语句执行效率
			Explain命令在解决数据库性能上是第一推荐使用命令,大部分的性能问题可以通过此命令来简单的解决,Explain可以用来查看 SQL 语句的执行效 果,可以帮助选择更好的索引和优化查询语句,写出更好 ... 
- crm 动态一级二级菜单
			之前代码菜单是写是的 如何 让他 动态 生成了 首先 添加 2个字段 admin.py 更改 显示 from django.contrib import admin from rbac import ... 
- 浅谈PageHelper插件分页实现原理及大数据量下SQL查询效率问题解决
			前因:项目一直使用的是PageHelper实现分页功能,项目前期数据量较少一直没有什么问题.随着业务扩增,数据库扩增PageHelper出现了明显的性能问题.几十万甚至上百万的单表数据查询性能缓慢,需 ... 
