【题解】bzoj 4478 [Jsoi2013]侦探jyy
原题传送门
弱智搜索题
我们就枚举每个点,先判断它是否必须发生,如果没有必须发生,开始搜索它的祖先,如果祖先中有必须发生的,那么它就必须发生,如果祖先中没有必须发生的,那么搜索所有入度为0的点(除了它的祖先),搜完之后如果还有没被搜到的必须发生的事件,那么它也必须发生
#include <bits/stdc++.h>
#define N 1005
#define M 100005
#define getchar nc
using namespace std;
inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int read()
{
register int x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return x*f;
}
inline void write(register int x)
{
if(!x)putchar('0');if(x<0)x=-x,putchar('-');
static int sta[20];register int tot=0;
while(x)sta[tot++]=x%10,x/=10;
while(tot)putchar(sta[--tot]+48);
}
struct node{
int to,next;
}e1[M],e2[M];
int head1[N],head2[N],cnt1=0,cnt2=0,in[N];
inline void add1(register int u,register int v)
{
e1[++cnt1]=(node){v,head1[u]};
head1[u]=cnt1;
++in[v];
}
inline void add2(register int u,register int v)
{
e2[++cnt2]=(node){v,head2[u]};
head2[u]=cnt2;
}
int n,m,d;
int a[N],ans[N],tot=0,mu[N],tic=0,vis[N];
inline bool check(register int x)
{
++tic;
queue<int>q;
q.push(x);
vis[x]=tic;
while(!q.empty())
{
int u=q.front();
q.pop();
for(register int i=head2[u];i;i=e2[i].next)
{
int v=e2[i].to;
if(vis[v]!=tic)
{
q.push(v);
vis[v]=tic;
}
}
}
for(register int i=1;i<=d;++i)
if(vis[a[i]]==tic)
return true;
for(register int i=1;i<=n;++i)
if(!in[i]&&vis[i]!=tic)
q.push(i),vis[i]=tic;
while(!q.empty())
{
int u=q.front();
q.pop();
for(register int i=head1[u];i;i=e1[i].next)
{
int v=e1[i].to;
if(vis[v]!=tic)
{
q.push(v);
vis[v]=tic;
}
}
}
for(register int i=1;i<=d;++i)
if(vis[a[i]]!=tic)
return true;
return false;
}
int main()
{
n=read(),m=read(),d=read();
for(register int i=1;i<=m;++i)
{
int u=read(),v=read();
add1(u,v),add2(v,u);
}
for(register int i=1;i<=d;++i)
a[i]=read(),mu[a[i]]=true;
for(register int i=1;i<=n;++i)
if(mu[i]||check(i))
ans[++tot]=i;
for(register int i=1;i<=tot;++i)
write(ans[i]),putchar(' ');
return 0;
}
【题解】bzoj 4478 [Jsoi2013]侦探jyy的更多相关文章
- 「JSOI2013」侦探jyy
「JSOI2013」侦探jyy 传送门 个人感觉我写的复杂度不够优秀啊,但是好像没有别的办法了... 我们枚举每个点,考虑这个点能不能不发生. 首先我们从这个点开始,在反图上面 \(\text{BFS ...
- [BZOJ3523][Poi2014]KLO-Bricks——全网唯一 一篇O(n)题解+bzoj最优解
Description 有n种颜色的砖块,第i种颜色的砖块有a[i]个,你需要把他们放成一排,使得相邻两个砖块的颜色不相同,限定第一个砖块的颜色是start,最后一个砖块的颜色是end,请构造出一种合 ...
- BZOJ 4480 [JSOI2013] 快乐的jyy
思路 两个字符串都插入回文自动机中(每次重置last) 最后统计两个right集合的大小就好了 代码 #include <cstdio> #include <algorithm> ...
- BZOJ 4479: [Jsoi2013]吃货jyy
一句话题意:求必须包含某K条边的回路(回到1),使得总权值最小 转化为权值最小的联通的偶点 令F[i]表示联通状态为i的最小权值,(3^n状压)表示不在联通块内/奇点/偶点,连边时先不考虑必选的边的度 ...
- 题解 bzoj 4398福慧双修(二进制分组)
二进制分组,算个小技巧 bzoj 4398福慧双修 给一张图,同一条边不同方向权值不同,一条边只能走一次,求从1号点出发再回到1号点的最短路 一开始没注意一条边只能走一次这个限制,打了个从一号点相邻节 ...
- [题解]bzoj 3223 文艺平衡树
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3884 Solved: 2235[Submit][Sta ...
- [题解]bzoj 1861 Book 书架 - Splay
1861: [Zjoi2006]Book 书架 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 1396 Solved: 803[Submit][Stat ...
- bzoj 4459: [Jsoi2013]丢番图 -- 数学
4459: [Jsoi2013]丢番图 Time Limit: 10 Sec Memory Limit: 64 MB Description 丢番图是亚历山大时期埃及著名的数学家.他是最早研究整数系 ...
- bzoj 4465: [Jsoi2013]游戏中的学问
4465: [Jsoi2013]游戏中的学问 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 121 Solved: 59[Submit][Statu ...
随机推荐
- [LeetCode] 619. Biggest Single Number_Easy tag: SQL
Table number contains many numbers in column num including duplicated ones.Can you write a SQL query ...
- [LeetCode] 607. Sales Person_Easy tag: SQL
Description Given three tables: salesperson, company, orders.Output all the names in the table sales ...
- CentOS6.5 安装openssl
安装比较简单! 1.下载安装包 [root@mycentos ~]# wget http://www.openssl.org/source/openssl-1.0.2f.tar.gz 2.解压和编译 ...
- Util.FSUtils: Waiting for dfs to exit safe mode
有好几次,启动Hadoop和HBase之后,执行jps命令,已经看到有HMaster的进程, 但是进入到HBase的shell,执行一个命令,会出现下面的错误: ERROR: org.apache.h ...
- Kotlin 范型约束
官方的示意及其简约,该说的一概没说 我在这里给大家一个完整的例子 //test.kt fun <T> cloneWhenGreater(list: List<T>, thres ...
- 阿里云服务器用smtp发送邮件返失败
阿里云使用SMTP发送邮件失败,因为阿里云服务器屏蔽了25端口,所以发送不成功,解决办法改用587发送QQ邮件,且必须使用SSL,否则不成功. 经测试QQ的465,995不能使用. https://b ...
- jQuery-设计模式
[目录] 一.选择网页元素 二.改变结果集 三.链式操作 四.元素的操作:取值和赋值 五.元素的操作:移动 六.元素的操作:复制.删除和创建 七.工具方法 八.事件操作 九.特殊效果 [正文] 一.选 ...
- URL List by Category
URLs List AI https://www.cnblogs.com/zlel/p/8882129.html Javascript Promise http://liubin.org/promis ...
- 17. Letter Combinations of a Phone Number(bfs)
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
- html5-article元素
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...