洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver
来源:互联网 发布:手机p2p软件下载 编辑:程序博客网 时间:2024/05/21 10:55
题目描述
Farmer John and his cows are planning to leave town for a long vacation, and so FJ wants to temporarily close down his farm to save money in the meantime.
The farm consists of N barns connected with M bidirectional paths between some pairs of barns (1≤N,M≤3000). To shut the farm down, FJ plans to close one barn at a time. When a barn closes, all paths adjacent to that barn also close, and can no longer be used.
FJ is interested in knowing at each point in time (initially, and after each closing) whether his farm is "fully connected" -- meaning that it is possible to travel from any open barn to any other open barn along an appropriate series of paths. Since FJ's farm is initially in somewhat in a state of disrepair, it may not even start out fully connected.
FJ和他的奶牛们正在计划离开小镇做一次长的旅行,同时FJ想临时地关掉他的农场以节省一些金钱。
这个农场一共有被用M条双向道路连接的N个谷仓(1<=N,M<=3000)。为了关闭整个农场,FJ 计划每一次关闭掉一个谷仓。当一个谷仓被关闭了,所有的连接到这个谷仓的道路都会被关闭,而且再也不能够被使用。
FJ现在正感兴趣于知道在每一个时间(这里的“时间”指在每一次关闭谷仓之后的时间)时他的农场是否是“全连通的”——也就是说从任意的一个开着的谷仓开始,能够到达另外的一个谷仓。注意自从某一个时间之后,可能整个农场都开始不会是“全连通的”。
输入输出格式
输入格式:The first line of input contains N and M. The next M lines each describe a
path in terms of the pair of barns it connects (barns are conveniently numbered
1…N). The final N lines give a permutation of 1…N
describing the order in which the barns will be closed.
输出格式:The output consists of N lines, each containing "YES" or "NO". The first line
indicates whether the initial farm is fully connected, and line i+1 indicates
whether the farm is fully connected after the ith closing.
输入输出样
4 31 22 33 43412
YESNOYESYES
可以倒着加入边,然后每次暴力的去找根的个数,如果大于1就是NO
储存起来倒序输出。
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#define MAXN 3010 using namespace std;int n,m,k,s=0,c=1;int fa[MAXN],head[MAXN],que[MAXN];bool flag[MAXN],ans[MAXN];struct node{int next,to,last;}a[MAXN<<1];inline int read(){ int date=0,w=1;char c=0; while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();} while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();} return date*w;}int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}void uniun(int x,int y){x=find(x);y=find(y);if(x!=y)fa[x]=y;}void add(int x,int y){a[c].to=y;a[c].last=x;a[c].next=head[x];head[x]=c++;a[c].to=x;a[c].last=y;a[c].next=head[y];head[y]=c++;}int main(){int x,y;n=read();m=read();for(int i=1;i<=n;i++)fa[i]=i;memset(a,0,sizeof(a));for(int i=1;i<=m;i++){x=read();y=read();add(x,y);}for(int i=1;i<=n;i++)que[i]=read();for(int i=n;i>=1;i--){flag[que[i]]=true;for(int j=head[que[i]];j;j=a[j].next)if(flag[a[j].last]&&flag[a[j].to])uniun(a[j].last,a[j].to);s=0;for(int j=1;j<=n;j++)if(flag[j]&&fa[j]==j)s++;if(s==1)ans[i]=true;}printf("YES\n");for(int i=2;i<=n;i++){if(ans[i])printf("YES\n");else printf("NO\n");}return 0;}
- 洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver
- P3144 [USACO16OPEN]关闭农场Closing the Farm
- P3144 [USACO16OPEN]关闭农场Closing the Farm
- [P3144][USACO16OPEN]关闭农场Closing the Farm
- 洛谷 P3144 [USACO16OPEN]关闭农场Closing the Farm
- |洛谷|并查集|P3144 [USACO16OPEN]关闭农场Closing the Farm
- [USACO16OPEN]Closing the Farm_Silver(联通分量+逆向边处理+并查集)
- 洛谷P3147 [USACO16OPEN]262144
- 洛谷 P3147 [USACO16OPEN]262144
- Problem A. Closing the Loop 问题A.关闭的环 解决办法
- codechef Closing the Tweets 题解
- BZOJ 4579 Closing the Farm
- Google Code Jam 2011 Qualification Round 资格赛 Problem A. Closing the Loop 问题A.关闭的环
- 在Closing事件里阻止窗口关闭
- 在Closing事件中取消窗体关闭
- closing
- bzoj 4579 [Usaco2016 Open]Closing the Farm
- USACO——Closing the farm
- 485. Max Consecutive Ones
- 浅谈JVM(二)——内存分配和垃圾回收
- 深拷贝与浅拷贝
- 批量xlsx表格转json
- 欢迎使用CSDN-markdown编辑器
- 洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver
- 并查集的启发式合并
- Scala 强大的集合数据操作示例
- json_list
- 利用代理IP爬取网页的小心得
- C++ 任意进制转换为十进制,十进制转换为任意进制
- PTA 7-17(查找) 字符串关键字的散列映射(25 分) 25分代码
- TensorFlow中实现minst代码
- 编译原理 (预处理>编译>汇编>链接)