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 ...
随机推荐
- 【BZOJ5502】[GXOI/GZOI2019]与或和(单调栈)
[BZOJ5502][GXOI/GZOI2019]与或和(单调栈) 题面 BZOJ 洛谷 题解 看到位运算就直接拆位,于是问题变成了求有多少个全\(0\)子矩阵和有多少个全\(1\)子矩阵. 这两个操 ...
- Count on a tree SPOJ - COT (主席树,LCA)
You are given a tree with N nodes. The tree nodes are numbered from 1 to N. Each node has an integer ...
- Springboot 4.Springboot 集成SwaggerUi
SwaggerUi就是自动生成接口文档的这么一个类似于插件的工具,可以直接访问接口. 首先打开pom文件,将插件引进来,然后增加一个属性<properties>,用来设置版本号的,然后直接 ...
- django系列4 :创建管理员
以下复制粘贴自官网 创建管理员用户¶ 首先,我们需要创建一个可以登录管理站点的用户.运行以下命令: / $ python manage.py createsuperuser 输入所需的用户名, ...
- 从浅入深详解独立ip网站域名恶意解析的解决方案
立IP空间的好处想必大家都能耳熟闻详,稳定性强,利于seo等让大家选择了鼎峰网络香港独立IP空间.那么, 网站独享服务器IP地址,独立IP空间利于百度收录和权重的积累.不受牵连.稳定性强等诸多优势为一 ...
- css at @ 规则
css相信我们都不陌生,但是我们真的对它非常了解吗? css主要分为两种规则组成: 一种被称为 at-rule,也就是 at 规则 另一种是 qualified rule,也就是普通规则 今天让我们来 ...
- Windows下配置eclipse写WordCount
1 下载插件 hadoop-eclipse-plugin-2.7.2.jar github上下载源码后需要自己编译.这里使用已经编译好的插件即可 2 配置插件 把插件放到..\eclipse\plug ...
- tomcat无法正常关闭问题分析及解决
问题描述 通常,我们都会直接使用tomcat提供的脚本执行关闭操作,如下: # sh bin/shutdown.sh Using CATALINA_BASE: /usr/local/apache-to ...
- ArcGis汇总篇
ArcGis-javascript-API下载 bigemap.太乐地图 可下载地图文件 用arcgis for js 可以河流流域水质图 ArcGis导出shp文件(dbf.prj.sbn.sbx. ...
- cmd 命令添加防火墙端口
windows dos 命令添加防火墙端口. 示例 123 端口: netsh firewall add portopening protocol = UDP port = name = NTPSER ...