[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 ...
随机推荐
- asp.net core MVC 控制器,接收参数,数据绑定
1.参数 HttpRequest HttpRequest 是用户请求对象 QueryString Form Cookie Session Header 实例: public IActionResult ...
- ***在Linux环境下mysql的root密码忘记解决方法(三种)-推荐第三种
MySQL密码的恢复方法之一 1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库. 因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的 状态 ...
- 547. Friend Circles
There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...
- 基于Redis的分布式锁到底安全吗
http://zhangtielei.com/posts/blog-redlock-reasoning.html
- Java集合源码学习(二)ArrayList
1.关于ArrayList ArrayList直接继承AbstractList,实现了List. RandomAccess.Cloneable.Serializable接口,为什么叫"Arr ...
- 【NOI2017】泳池
题解: 满分的笛卡尔树以后再学吧.. 40分还是比较好想的 但是状态挺复杂的 直接贴代码了 代码: #include <bits/stdc++.h> using namespace std ...
- csv导入数据到mongodb3.2
mongoimport.exe -d paper -c paper K:\paper.csv --type csv -f id,name 数据库名 表名 文件所在位置 文件类型 ...
- lrzsz linix 远程文件传输工具。
安装方法 #yum install lrzsz -y 使用方法 #rz -y 上传指定文档到当前目录
- C# 之 GUID格式化
Guid的带参数的ToString()方法来实现格式化,如下: //// 摘要: // 根据所提供的格式说明符,返回此 System.Guid 实例值的字符串表示形式. //// 参数: // ...
- parted 分区命令
fdisk 是针对 MBR的分区 ,因为MBR分区空间最大不能超过2T 最多分4个主分区 , 所以parted可以修改磁盘为GPT 可以支持更大的分区,更多的分区 1 查看分区 : #part ...