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
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 .
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 ...
随机推荐
- mongoDB 文档操作_改
mongoDB 更改操作 格式对比 MySQL update table set .... where .... db.collection.updateOne(query,update,upsert ...
- NowCoder -- 牛客小白月赛10
A--勘测 推下公式 a[i] = a[i-1] + a[i-2] +2 #include<stdio.h> #include<string.h> ]; int main() ...
- 题解-洛谷P1303 A*B Problem(高精)
https://www.luogu.org/problemnew/show/P1303(题目传送门) 看到数据范围,显然要用高精度算法(乘法). 首先用字符串读下这最多达10^2000的数,并判断符号 ...
- AGC027B Garbage Collector
一道很好的构造题 原题链接 很快就能想到,捡每个垃圾的能量可以最后再算.然后,对于每个垃圾,在路上耗费的能量仅与它是第几个被捡的有关,于是我们考虑将垃圾分组. 首先,我们定义\(F(x,i)\)为某次 ...
- 金融量化分析【day112】:均值回归策略
一.均值回归策略 1.什么是回归策略 二.归一标准化 import numpy as np a = np.random.uniform(100,5000,1000) b = np.random.uni ...
- Fiddler--Filters
本篇主要介绍Fiddler中Filters(过滤器)选项功能. 先看看Filters的界面: 一.模块一 当勾选“Use Filters”,Filters才开始工作:否则Filters中的设置内容将无 ...
- LINQ to SQL 调用 SQL Server 的系统函数
Ø 简介 在 C# 中比较常用的 ORM(Object Relational Mapping)框架就是 EF 了,EF 经常结合 LINQ to SQL 来操作数据库.本文主要讨论如何在 LINQ ...
- SSH框架之hibernate《二》
Hibernate第二天 一.hibernate的持久化类和对象标识符 1.1持久化类的编写规范 1.1.1什么是持久化类: ...
- java(8)二重循环
一.二重循环 1.循环中,嵌套另外一个循环,将内层的循环,看成外层循环的一个循环操作 2.常见的二重循环 形式1: 外层while或do…while 内层为for循环 形式2: 外层.内层都 ...
- EasyUI datagrid 的多条件查询
<script type="text/javascript"> $(function () { $("#dg" ...