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 ...
随机推荐
- FastDFS 分布式文件系统搭建
安装依赖环境yum install make cmake gcc gcc-c++ pcre-devel zlib-devel perl-devel 安装libfastcommon-master.zip ...
- lesson03
3.1. 画 点 3.2. 基准平面 (重要) 1. 关于 点 的使用() 1.画一条直线,在线上画一个点(利用该点占该线段的百分比画出),通过该点画一条直线 2. 画一个长方体,定位到上表面.选择( ...
- elasticsearch更改mapping(不停服务重建索引)
转载地址:http://donlianli.iteye.com/blog/1924721?utm_source=tuicool&utm_medium=referral Elasticsearc ...
- create-react-app中添加less支持
前言 使用 create-react-app 脚手架创建项目后,默认是不支持 less 的.所以我们需要手动添加. 第一步 暴露webpack配置文件 使用 create-react-app 创建的项 ...
- pytest 11 allure2生成html报告
allure是一个report框架,支持java的Junit/testng等框架,当然也可以支持python的pytest框架,也可以集成到Jenkins上展示高大上的报告界面. 环境准备 1.pyt ...
- django系列4 :创建管理员
以下复制粘贴自官网 创建管理员用户¶ 首先,我们需要创建一个可以登录管理站点的用户.运行以下命令: / $ python manage.py createsuperuser 输入所需的用户名, ...
- ArcGis地理坐标系转投影坐标系问题的思考与处理
博主又不知道标题该咋写了,凑合看吧. 国庆前帮人处理了这样一个问题:Shapefile数据,加载到ArcMap后呈懵逼状态,缩放功能完蛋.求助者希望对数据进行投影,由CGCS2000的GCS坐标系投影 ...
- [再寄小读者之数学篇](2014-06-20 求极限---Jordan 不等式的应用)
证明: 当 $\lm<1$ 时, $\dps{\lim_{R\to+\infty} R^\lm\int_0^{\pi/2} e^{-R\sin\tt}\rd \tt=0}$. 证明: 由 $$\ ...
- Web常见漏洞修复建议
一.SQL注入修复建议 1.过滤危险字符,例如:采用正则表达式匹配union.sleep.and.select.load_file等关键字,如果匹配到则终止运行. 2.使用预编译语句,使用PDO需要注 ...
- Scrapy框架-----爬虫
说明:文章是本人读了崔庆才的Python3---网络爬虫开发实战,做的简单整理,希望能帮助正在学习的小伙伴~~ 1. 准备工作: 安装Scrapy框架.MongoDB和PyMongo库,如果没有安装, ...