标签:
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 barns connected with bidirectional paths between some pairs of barns (). 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 and . The next lines each describe a
path in terms of the pair of barns it connects (barns are conveniently numbered
). The final lines give a permutation of
describing the order in which the barns will be closed.
输出格式:
The output consists of lines, each containing "YES" or "NO". The first line
indicates whether the initial farm is fully connected, and line indicates
whether the farm is fully connected after the th closing.
4 3
1 2
2 3
3 4
3
4
1
2
YES
NO
YES
YES
离线倒序处理,用并查集维护连通的点的数量,就可以知道是不是所有可用的点都连通了。
1 /*by SilverN*/ 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 using namespace std; 8 const int mxn=6500; 9 int read(){ 10 int x=0,f=1;char ch=getchar(); 11 while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 12 while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 13 return x*f; 14 } 15 struct edge{ 16 int v,w; 17 int nxt; 18 }e[mxn]; 19 int hd[mxn],mct=0; 20 void add_edge(int u,int v){ 21 e[++mct].v=v;e[mct].nxt=hd[u];hd[u]=mct; 22 return; 23 } 24 int n,m; 25 int fa[mxn]; 26 int num[mxn]; 27 int ord[mxn]; 28 bool ans[mxn]; 29 bool ban[mxn]; 30 void init(){ 31 for(int i=1;i<=n;i++)fa[i]=i,num[i]=1; 32 return; 33 } 34 int find(int x){ 35 if(fa[x]==x)return x; 36 return fa[x]=find(fa[x]); 37 } 38 int main(){ 39 n=read();m=read(); 40 init(); 41 int i,j; 42 int u,v; 43 for(i=1;i<=m;i++){ 44 u=read();v=read(); 45 add_edge(u,v); 46 add_edge(v,u); 47 } 48 for(i=1;i<=n;i++){ord[i]=read();ban[ord[i]]=1;} 49 printf("YES\n"); 50 for(i=n;i;i--){ 51 u=ord[i];ban[u]=0; 52 for(j=hd[u];j;j=e[j].nxt){ 53 v=e[j].v;if(ban[v])continue; 54 int fax=find(u); 55 int fay=find(v); 56 if(fax!=fay){ 57 fa[fay]=fax; 58 num[fax]+=num[fay]; 59 } 60 } 61 ord[n]=find(ord[n]); 62 // cout<<ord[n]<<" "<<(num[ord[n]])<<endl; 63 if(num[ord[n]]==n-i+1)ans[i]=1; 64 else ans[i]=0; 65 } 66 for(i=2;i<=n;i++) 67 if(ans[i]==1)printf("YES\n"); 68 else printf("NO\n"); 69 return 0; 70 }
洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm
标签:
原文地址:http://www.cnblogs.com/SilverNebula/p/5928457.html