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. python中深拷贝与浅拷贝

    # 1.浅拷贝(复制东西)a = [11,22,33] # 实际上是浅拷贝# 没有把这个变量的值赋进去,而是把另一个变量的地址拿过去了,就叫浅拷贝.b = a # print(id(a))# prin ...

  2. Aws云服务EMR使用

    Aws云服务EMR使用 创建表结构 创建abc库下的abc_user_i表字段s3://abc-server/abc-emr/shell/ABC_USER_HIVE.q: EXTERNAL 指定为外部 ...

  3. 字符转ASCII码

    char k = '成'; int str = (int)k; Console.WriteLine(str); 结果25104就是‘成’对应的ASCII值

  4. Supervisor Linux程序进程管理

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

  5. IIS7 禁止目录运行脚本

    早晨看博客说有人被黑了:http://www.cnblogs.com/sanshi/p/3150639.html 看回复建议禁用上传目录的脚本运行权限 在IIS6上还是比较容易的,直接右键--属性,把 ...

  6. 请推荐几个asp.net下做网站的好的开源框架

    1.We7 CMS We7 CMS是由西部动力开发的一款充分发掘互联网Web2.0(如博客.RSS等)的信息组织优势,将其理念利用到政府企事业网站的构建.组织.管理中的网站建设和管理方面的产品. 系统 ...

  7. 一起学Hadoop——MapReduce原理

        一致性Hash算法. Hash算法是为了保证数据均匀的分布,例如有3个桶,分别是0号桶,1号桶和2号桶:现在有12个球,怎么样才能让12个球平均分布到3个桶中呢?使用Hash算法的做法是,将1 ...

  8. fillder--信息面板显示请求耗时列

    class Handlers--------->Ctrl+R,找到该方法,加上以下方法即可 { // 显示每行请求的发起时间:时分秒毫秒 public static BindUIColumn(& ...

  9. php json_encode转换中文乱码

    $arr = ["a"=>'范德萨似懂非懂']; echo json_encode($arr,JSON_UNESCAPED_UNICODE);

  10. JAVA中值类型和引用类型的不同(面试常考)

    转载:https://www.cnblogs.com/1ming/p/5227944.html 1. JAVA中值类型和引用类型的不同? [定义] 引用类型表示你操作的数据是同一个,也就是说当你传一个 ...