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的更多相关文章

  1. P5157 [USACO18DEC]The Cow Gathering

    首先考虑怎么check一个点是否能被最后一个删除. 可以这么建图,以这个点建有根树,边全部向上指,再加上剩下的有向边. 很明显,这里的一条边的定义就变成了只有删去这个点,才可以删去它指向的点. 因此, ...

  2. [USACO18DEC]The Cow Gathering P

    首先可以思考一下每次能删去的点有什么性质. 不难发现,每次能删去的点都是入度恰好为 \(1\) 的那些点(包括 \(a_i \rightarrow b_i\) 的有向边). 换句话说,每次能删去的点既 ...

  3. BZOJ1827[USACO 2010 Mar Gold 1.Great Cow Gathering]——树形DP

    题目描述 Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来举办这次集会.每个奶牛居住在 N(1<=N<=100,000) 个农场 ...

  4. 【luoguP2986】[USACO10MAR]伟大的奶牛聚集Great Cow Gathering

    题目链接 先把\(1\)作为根求每个子树的\(size\),算出把\(1\)作为集会点的代价,不难发现把集会点移动到\(u\)的儿子\(v\)上后的代价为原代价-\(v\)的\(size\)*边权+( ...

  5. P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…

    题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...

  6. 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…(树规)

    题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...

  7. 洛谷 P2986 [USACO10MAR]Great Cow Gat…(树形dp+容斥原理)

    P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat… 题目描述 Bessie is planning the annual Great Cow Gathering for c ...

  8. [USACO10MAR]伟大的奶牛聚集Great Cow Gat…

    题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...

  9. 【题解】Luogu p2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat 树型dp

    题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...

随机推荐

  1. C++ Primer 笔记——顺序容器

    1.标准库中定义了一些顺序容器,所有顺序容器都提供了快速顺序访问元素的能力. 2.如果容器的元素类型没有默认构造函数,那么在构造这个容器的时候不能只指定这个容器的数目,因为没有办法默认构造这些元素. ...

  2. Android取得系统时间

    Time t = new Time();//实例化Time类 t.setToNow();//取得当前的系统时间 int month = t.month;//获取月 int year = t.year; ...

  3. mongodb数据中的复制(副本集)

    ---恢复内容开始--- 什么是复制 复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性,并可以保证数据的安全性 复制还允许从硬件故障和服务中断中恢复数据 为什么要复制 数据备 ...

  4. POJ 1064 Cable master (二分法+精度控制)

    Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 65358   Accepted: 13453 De ...

  5. 静态属性property

    静态属性property(是通过对象去使用) property是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值 1 . 通过@property修饰过的函数属性,调用的时候无需在加() cla ...

  6. Supervisor Linux程序进程管理

    Supervisor 介绍 在linux或者unix操作系统中,守护进程(Daemon)是一种运行在后台的特殊进程,它独立于控制终端并且周期性的执行某种任务或等待处理某些发生的事件.由于在linux中 ...

  7. IIS中配置访问HTTPS

    1,新建网站,选中类型为 https,然后更改SSL证书为你配置的SSL证书, 对于SSL证书的配置是这样的 点开第二步,然后点击 创建自签名证书 确定以后点开网站看到有个SSL, 双击进去,再选中 ...

  8. CentOS下配置SFTP操作日志

    1.修改ssh的配置 vi /etc/ssh/sshd_config 在36行左右修改如下配置 Subsystem sftp /usr/libexec/openssh/sftp-server -l I ...

  9. 解决redis aof文件过大的问题

    执行BGREWRITEAOF命令对redis的AOF进行重写 redis-cli BGREWRITEAOF 相关解释: Redis的AOF机制有点类似于Mysql binlog,是Redis的提供的一 ...

  10. (转载)dotnet core 中文乱码 codepages

    引子 转载自:http://www.jianshu.com/p/1c9c59c5749a 参考:.Net Core 控制台输出中文乱码 上文中我查阅了一些cli的源码, 闲来无事就继续翻代码, 冥冥之 ...