CodeForces 682C Alyona and the Tree(广搜 + 技巧)
方法:从根节点开始广搜,如果遇到了应该删除的点,就再广搜删掉它的子树并标记,然后统计一下被标记的个数就是答案,所谓技巧就是从根节点开始搜索的时候,如果遇到了某个节点的距离<0,就让它是0,0可以消除负数效应,让后面的点重新参与正常删除操作,这个方法的正确性不难证明,可以自己画一下图。而且还有比较方便的方法就是,记录不被删除的点,然后n-他们就可以了。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
#define N 100010
#define INF 1e9
struct Node
{
int to,val;
};
vector<Node> vt[N];
int a[N],n,vis[N],del[N],fa[N],mark[N];
queue<int> que;
queue<int> q1;
void Delete(int u)
{
int len,v,now;
while(!q1.empty()) q1.pop();
q1.push(u);
del[u] = ;
while(!q1.empty())
{
now = q1.front();
q1.pop();
len = vt[now].size();
for(int i = ; i < len; i++)
{
v = vt[now][i].to;
if(!del[v] && fa[u] != v)
{
del[v] = ;
q1.push(v);
}
}
}
}
void bfs(int s)
{
que.push(s);
vis[s] = ;
int now,nxt,len,dis;
while(!que.empty())
{
now = que.front();
que.pop();
len = vt[now].size();
for(int i = ; i < len; i++)
{
nxt = vt[now][i].to;
dis = vt[now][i].val;
fa[nxt] = now;
if(vis[nxt] == INF)
{
vis[nxt] = vis[now] + dis;
if(vis[nxt] < ) vis[nxt] = ;
if(vis[nxt] > a[nxt])
{
Delete(nxt);
}
else que.push(nxt);
}
}
}
}
int main()
{
memset(del,,sizeof(del));
scanf("%d",&n);
for(int i = ; i <= n; i++)
{
scanf("%d",&a[i]);
vt[i].clear();
fa[i] = i;
vis[i] = INF;
}
int b,wei;
Node L,R;
for(int i = ; i <= n; i++)
{
scanf("%d%d",&b,&wei);
R.to = b; R.val = wei;
vt[i].push_back(R);
L.to = i; L.val = wei;
vt[b].push_back(L);
}
while(!que.empty()) que.pop();
bfs();
int ans = ;
for(int i = ; i <= n; i++)
{
if(del[i])
{
ans++;
}
}
printf("%d\n",ans);
return ;
}
CodeForces 682C Alyona and the Tree(广搜 + 技巧)的更多相关文章
- CodeForces 682C Alyona and the Tree (树+dfs)
Alyona and the Tree 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/C Description Alyona ...
- XJOI3363 树3/Codeforces 682C Alyona and the Tree(dfs)
Alyona decided to go on a diet and went to the forest to get some apples. There she unexpectedly fou ...
- codeforces 682C Alyona and the Tree(DFS)
题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...
- Codeforces 682C Alyona and the Tree (树上DFS+DP)
题目链接:http://codeforces.com/problemset/problem/682/C 题目大意:取树上任意一个点v,若点v的子树中有一个点u使得dist(v,u)>a[u]那么 ...
- Codeforces 682C Alyona and the Tree
题目链接:http://codeforces.com/problemset/problem/682/C 分析:存图,用dfs跑一遍,详细见注释 1 #include<iostream> 2 ...
- Codeforces 682C Alyona and the Tree(树形DP)
题目大概说给一棵点有权.边也有权的树.一个结点v不高兴当且仅当存在一个其子树上的结点u,使得v到u路径上的边权和大于u的权值.现在要不断地删除叶子结点使得所有结点都高兴,问最少删几个叶子结点. 一开始 ...
- codeforces 682C Alyona and the Tree DFS
这个题就是在dfs的过程中记录到根的前缀和,以及前缀和的最小值 #include <cstdio> #include <iostream> #include <ctime ...
- CodeForces 682C Alyona and the Tree (树上DFS)
题意:给定一棵树,每个叶子有一个权值,每条边也有一个权值,现在让你删最少的结点,使得从任何结点出发到另一个结点的边上权值和都小于两个结点的权值. 析:很明显是DFS,不过要想找出最少的结点可能不太容易 ...
- Codeforces E. Alyona and a tree(二分树上差分)
题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
随机推荐
- Gentoo本地化设置--时区、时钟、字体、中文环境
时区 你需要选择时区让系统知道你的地理位置,以保持正确的时间.在/usr/share/zoneinfo中查找你的时区.然后在/etc/conf.d/clock中设置时区.请忽略/usr/share/z ...
- 去除VisualStudio中拼写错误检测的红色波浪线
去除VisualStudio中拼写错误检测的红色波浪线 在Visual Assistant中将 Underline spelling errors in comments and strings us ...
- C的指针,真的很经典
工作以后,一直使用C++,也做过Objective C,各种类的方法封装得很好,使用很简单,今天偶尔翻看一下 严蔚敏 的 <数据结构>,第一个程序demo就看了半天,一是由于demo的变量 ...
- UI----安健2 UIswitch UIslider
- (void)viewDidLoad { [super viewDidLoad]; [self buttonswitch]; [self buttonslider]; } -(void)button ...
- asp下sha1加密函数
sha1.asp文件 <script language="javascript" type="text/javascript" runat="s ...
- getScrollX()理解
- TODO:小程序的使用体验
TODO:小程序的使用体验 2017.01.09小程序如期而至,话说十年前的今天2007.01.09是第一代iPhone发布日期. 清晨朋友圈发了一张小程序的截图,很多朋友问用什么版本的微信才有小程序 ...
- android 获取系统硬件信息
一,首先设置权限访问: <uses-permission android:name="android.permission.READ_PHONE_STATE" /> ...
- VBS虚拟键码
1 VK_LBUTTON 鼠标左键 2 VK_RBUTTON 鼠标右键 3 VK_CANCEL Ctrl+Break(通常不需要处理) 4 VK_MBUTTON 鼠标中键 8 VK_BACK Back ...
- leetcode21
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...