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 ...
随机推荐
- BZOJ2616PERIODNI
题目描述 给定一个N列的表格,每列的高度各不相同,但底部对齐,然后向表格中填入K个相同的数,填写时要求不能有两个数在同一列,或同一行,下图中b是错误的填写,a是正确的填写,因为两个a虽然在同一行,但它 ...
- bzoj3527: [Zjoi2014]力 卷积+FFT
先写个简要题解:本来去桂林前就想速成一下FFT的,结果一直没有速成成功,然后这几天断断续续看了下,感觉可以写一个简单一点的题了,于是就拿这个题来写,之前式子看着别人的题解都不太推的对,然后早上6点多推 ...
- redis的主从模式搭建及注意事项
前言:本文先分享下如何搭建redis的主从模式配置,以及主从模式配置的注意事项.后续会继续分享如何实现一个高可用的redis服务,redis的Sentinel 哨兵模式及集群搭建. 安装: 1,yum ...
- Burnside引理的感性证明
\(Burnside\)引理的感性证明: 其中:\(G\)是置换集合,\(|G|\)是置换种数,\(T_i\)是第\(i\)类置换中的不动点数. \[L = \frac{1}{|G|} * \sum ...
- Vue(项目踩坑)_解决vue中axios请求跨域的问题
一.前言 今天在做项目的时候发现axios不能请求跨域接口 二.主要内容 1.之前直接用get方式请求聚合数据里的接口报错如下 2.当前请求的代码 3.解决方法 (1)在项目目录中依次找到:confi ...
- CMDB资产管理系统开发【day25】:表结构设计2
表结构设计1详细注释代码 # _*_coding:utf-8_*_ __author__ = 'luoahong' from assets.myauth import UserProfile from ...
- 第七节:WebApi与Unity整合进行依赖注入和AOP的实现
一. IOC和DI 1. 通过Nuget引入Unity程序集. PS:[版本:5.8.6] 2. 新建DIFactory类,用来读取Unity的配置文件并创建Unity容器,需要注意的是DIFacto ...
- 第十七节: EF的CodeFirst模式的四种初始化策略和通过Migration进行数据的迁移
一. 四种初始化策略 EF的CodeFirst模式下数据库的初始化有四种策略: 1. CreateDatabaseIfNotExists:EF的默认策略,数据库不存在,生成数据库:一旦model发生变 ...
- 【转载】C++ vector的用法
http://www.cnblogs.com/Nonono-nw/p/3462183.html
- [物理学与PDEs]第2章习题10 一维理想流体力学方程组的 Lagrange 形式
试证明: 一维理想流体力学方程组的 Lagrange 形式 (5. 22)-(5. 24) 也可写成如下形式 $$\beex \bea \cfrac{\p \tau}{\p t}-\cfrac{\p ...