4579: [Usaco2016 open]closing the Farm

Source: Internet
Author: User

4579: [Usaco2016 open]closing the FarmDescriptionFarmer John and his cows is 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 NN barns connected with MM bidirectional paths between some pairs of barns (1≤n,m≤200,000). To shut the farm down, FJ plansTo 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 @ each point in time (initially, and after each CLOsing) Whether his farm was "fully connected"--meaning that it was possible to travel from any open BArn to all 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 be even start out fully connected.InputThe first line of input contains N and M. The next M lines each describe a path in terms of the pairof Barns It connects (barns is conveniently numbered 1 ... N). The final N lines give a permutation oF 1 ... N describing the order in which the barns 'll be a closed.OutputThe output consists of N lines, each containing "YES" or "NO". The first line indicates whether theInitial farm is fully connected, and line i+1 indicates whether the farm was fully connected after the iith closing.Sample Input4 3
1 2
2 3
3 4
3
4
1
2Sample OutputYES
NO
YES
YESThe following:

First of all, what does this problem mean???

It's about a farm connected by M-bar, every time you close a barn and ask you if the whole farm is completely interconnected before you close the barn.

Know the meaning after this is to very naked and check set, maintain the number of Unicom block, see if it is 1 on the line ...

#include <stdio.h>#include<iostream>using namespacestd;Const intn=200005;intN,m,i,j,x,y,fx,fy,cnt,a[n],p[n],ans[n],f[n];inttot,head[n],next[n<<1],to[n<<1];voidAddintXinty) {Tot++; To[tot]=y; Next[tot]=Head[x]; HEAD[X]=tot;}int Get(intx) {    if(f[x]==x)returnXElse returnf[x]=Get(F[x]);}intMain () {scanf ("%d%d",&n,&m);  for(i=1; i<=n;i++) Head[i]=-1;  for(i=1; i<=m;i++) {scanf ("%d%d",&x,&y);        Add (x, y);    Add (y,x); }     for(i=1; i<=n;i++) F[i]=i;  for(i=1; i<=n;i++) scanf ("%d",&A[i]); CNT=0;  for(i=n;i>=1; i--) {CNT++; X=A[i];  for(j=head[x];j!=-1; j=Next[j]) {y=To[j]; if(p[y]==1) {FX=Get(x); FY=Get(y); if(fx!=FY) {CNT--; F[FX]=fy; }}} Ans[i]=CNT; P[X]=1; }     for(i=1; i<=n;i++)        if(ans[i]==1) printf ("yes\n");Elseprintf"no\n"); return 0;}

4579: [Usaco2016 open]closing the Farm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.