[USACO18DEC]The Cow Gathering
Description:
给定一棵树,每次删去叶子,有m个限制,分别为(a,b)表示a需要比b先删,为每个点能否成为最后被删的点
Hint:
\(n,m \le 10^5\)
Solution:
手模后会发现一个十分不显然的规律: 若a比b先删,则a在以b为根的子树中的点,都不能最后删,
于是这样就转化为这个题了:https://www.cnblogs.com/list1/p/10497877.html
每次直接打个差分标记
这题还要判无解的情况(这谁想得到啊)
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define ls p<<1
#define rs p<<1|1
using namespace std;
typedef long long ll;
const int mxn=5e5+5;
int n,m,cnt,tot,in[mxn],hd[mxn],vis[mxn],hd2[mxn],sz[mxn],dep[mxn],ans[mxn],tag[mxn],dfn[mxn],f[mxn][19];
inline int read() {
char c=getchar(); int x=0,f=1;
while(c>'9'||c<'0') {if(c=='-') f=-1;c=getchar();}
while(c<='9'&&c>='0') {x=(x<<3)+(x<<1)+(c&15);c=getchar();}
return x*f;
}
inline int chkmax(int &x,int y) {if(x<y) x=y;}
inline int chkmin(int &x,int y) {if(x>y) x=y;}
struct ed {
int to,nxt;
}t[mxn<<1];
inline void add(int u,int v) {
t[++cnt]=(ed) {v,hd[u]}; hd[u]=cnt;
}
inline void add2(int u,int v) {
t[++cnt]=(ed) {v,hd2[u]}; hd2[u]=cnt;
++in[v];
}
int up(int u,int k)
{
for(int i=30;i>=0;--i)
if(k&(1<<i)) u=f[u][i];
return u;
}
void dfs(int u,int fa)
{
f[u][0]=fa; dfn[u]=++tot; dep[u]=dep[fa]+1; sz[u]=1;
for(int i=hd[u];i;i=t[i].nxt) {
int v=t[i].to;
if(v==fa) continue ;
dfs(v,u); sz[u]+=sz[v];
}
}
void solve(int u,int fa,int val)
{
val+=tag[u]; ans[u]=val>0;
for(int i=hd[u];i;i=t[i].nxt) {
int v=t[i].to;
if(v==fa) continue ;
solve(v,u,val);
}
}
int flag=0;
void check()
{
queue<int > q;
for(int i=1;i<=n;++i)
if(in[i]<2) q.push(i),vis[i]=1;
while(!q.empty()) {
int u=q.front(); q.pop();
for(int i=hd[u];i;i=t[i].nxt) {
int v=t[i].to; --in[v];
if(!vis[v]&&in[v]<2) {
vis[v]=1;
q.push(v);
}
}
for(int i=hd2[u];i;i=t[i].nxt) {
int v=t[i].to; --in[v];
if(!vis[v]&&in[v]<2) {
vis[v]=1;
q.push(v);
}
}
}
for(int i=1;i<=n;++i) if(!vis[i]) flag=1;
}
int main()
{
n=read(); m=read(); int u,v,w;
for(int i=1;i<n;++i) {
u=read(); v=read();
add(u,v); add(v,u);
++in[u]; ++in[v];
}
dfs(1,1); f[1][0]=1;
for(int j=1;j<=18;++j)
for(int i=1;i<=n;++i)
f[i][j]=f[f[i][j-1]][j-1];
for(int i=1;i<=m;++i) {
u=read(); v=read(); add2(u,v);
if(dfn[v]>dfn[u]&&dfn[v]<dfn[u]+sz[u]) {
int pos=up(v,dep[v]-dep[u]-1);
--tag[pos];
++tag[1];
}
else ++tag[u];
}
check();
if(flag) {
for(int i=1;i<=n;++i)
printf("0\n");
return 0;
}
solve(1,1,0);
for(int i=1;i<=n;++i) printf("%d\n",ans[i]^1);
return 0;
}
[USACO18DEC]The Cow Gathering的更多相关文章
- P5157 [USACO18DEC]The Cow Gathering
首先考虑怎么check一个点是否能被最后一个删除. 可以这么建图,以这个点建有根树,边全部向上指,再加上剩下的有向边. 很明显,这里的一条边的定义就变成了只有删去这个点,才可以删去它指向的点. 因此, ...
- [USACO18DEC]The Cow Gathering P
首先可以思考一下每次能删去的点有什么性质. 不难发现,每次能删去的点都是入度恰好为 \(1\) 的那些点(包括 \(a_i \rightarrow b_i\) 的有向边). 换句话说,每次能删去的点既 ...
- BZOJ1827[USACO 2010 Mar Gold 1.Great Cow Gathering]——树形DP
题目描述 Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来举办这次集会.每个奶牛居住在 N(1<=N<=100,000) 个农场 ...
- 【luoguP2986】[USACO10MAR]伟大的奶牛聚集Great Cow Gathering
题目链接 先把\(1\)作为根求每个子树的\(size\),算出把\(1\)作为集会点的代价,不难发现把集会点移动到\(u\)的儿子\(v\)上后的代价为原代价-\(v\)的\(size\)*边权+( ...
- P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…(树规)
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- 洛谷 P2986 [USACO10MAR]Great Cow Gat…(树形dp+容斥原理)
P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat… 题目描述 Bessie is planning the annual Great Cow Gathering for c ...
- [USACO10MAR]伟大的奶牛聚集Great Cow Gat…
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- 【题解】Luogu p2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat 树型dp
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
随机推荐
- Jmeter 传值对比
${XXX}----------------------->在同一线程内可任务调用变量: vars.put(row,rowNum); ----------------------->var ...
- web页在微信中访问增加遮罩层 右上角弹出在浏览器中打开
https://blog.csdn.net/zgsdzczh/article/details/79744838 web页在微信中访问增加遮罩层 右上角弹出在浏览器中打开 <style typ ...
- 十六进制的ASCII码 "\u6cf0\u56fd" 解码成unicode
转码方法: C#: string a = "\u6cf0\u56fd"; string b = Encoding.UTF8.GetString(Encoding.UTF8.GetB ...
- TFS: 解决The build agent error - the session for this agent already exists
来源:http://ericphan.net/blog/2016/6/10/solving-the-tfs-build-agent-error-the-session-for-this-agent-a ...
- 记JS一个错误,
在定义变量时候,如果直接定义 var obj=null;那么,在转码时候,比如转为base64,此时或出现特殊的文字(也是汉字,只是很少见少用的文字,而且,出现的文字是根据你给obj赋值决定的,) 由 ...
- [已解决]virtualBox安装CentOS-6.3-x86_64-bin-DVD1.iso为什么总是显示命令行界面
CentOS 6.3的安装界面分为2种,一种是图形化安装界面,另一种则类似于Dos系统的纯文本安装界面. 进入图形安装界面的必要条件是硬件系统的物理内存大于628M以上即可,因为之前在VBox虚拟机里 ...
- Orchard是如何工作的?
文章翻译自http://docs.orchardproject.net/Documentation/How-Orchard-works 对Orchard的理解还不深刻,翻译可能有不好的地方. ...
- 【Arduino】开源开发板说明
来自世界各地的新型微控制器层出不穷,这类开发板多数都是通过Arduino改进的版本,例如由Arduino所改良的Yún一样,主要是针对网状网路进行改进或升级了其它无线功能. 但一些开发板也有着其独到的 ...
- loadrunner场景报错:Error: CCI compilation error -/tmp/brr_5d65oV/netdir/E/\320\324/Action.c (318): undeclared identifier `LAST'解决思路
在windows下写的脚本编译通过 但是拿到linux agent场景执行中就会提示如下,同样的脚本在windows agent下没有任何问题 agent连的是linux负载机 通过脚本一行一行排查, ...
- TreeSet集合的自然排序与比较器排序、Comparable接口的compareTo()方法
[自然排序] package com.hxl; public class Student implements Comparable<Student> { private String n ...