Title Description
Farmer John and his cows is planning to leave town for a long vacation, and so FJ wants to temporarily close he far M to save money in the meantime.
The farm consists ofNn barns connected With M m bidirectional paths between some pairs of barns (1 \leq N, M \leq 30001≤n,m≤300 0). 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 is used.
FJ is interested in knowing @ each point in time (initially, and after each closing) whether he farm is "fully connected "--meaning that it's 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 could not be even start out fully connected.
FJ and his cows are planning to leave town for a long trip, while FJ wants to temporarily turn off his farm to save some money.
The farm has a total of N barns (1<=n,m<=3000) that are connected by a two-way road with M. To shut down the whole farm, FJ plans to shut down a barn every time. When a barn is closed, all roads connected to the barn will be closed and no longer be used.
FJ is now interested in knowing whether his farm is "fully connected" at each time (the "time" here refers to the time before each closure of the barn)-that is to say, starting from any open barn and reaching another barn. Note that since a certain time, it is possible that the whole farm will not start to be "fully connected".
Input/output format
Input format:
The first line of input contains nN and mm. The next mm lines each describe a
Path in terms of the pair of barns it connects (Barns is conveniently numbered
1 \ldots N1... N). The final nn lines give a permutation of 1 \ldots N1... N
Describing the order in which the barns would be a closed.
Output format:
The output consists of nn lines, each containing "YES" or "NO". the first line
Indicates whether the initial farm is fully connected, and line i+1i+1 indicates
Whether the farm is fully connected after the iith closing.
Input and Output Sample input example # #:
4 31 22) 33 43412
Sample # # of output:
Yesnoyesyes
This problem has a connection process, should immediately think of and check this kind of good things, but we from a set to dismantle will be very troublesome, time also can not be allowed, then we are difficult to reverse, since is pushing more trouble, then why don't we turn this off process upside down imagine open, just start is closed state , then open each, and finally inverted output can be, not much to say on the code
#include <cstdio>#include<algorithm>#include<cstring>using namespacestd;intn,m,a[3005][3005],p[3005],check[3005],fa[3005],ans[3005];intFindintx) { if(fa[x]==x)returnx; returnfa[x]=find (Fa[x]);}intMain () {scanf ("%d%d",&n,&m); for(intI=1; i<=m;i++){ intx, y; scanf ("%d%d",&x,&y); A[x][y]=a[y][x]=1; } for(intI=1; i<=n;i++) {Fa[i]=i; scanf ("%d",&P[i]); } for(inti=n;i>=1; i--) {Check[p[i]]=1; for(intj=1; j<=n;j++) if(check[j]&&A[p[i]][j]) { //if (i==2) printf ("%d%d\n", p[i],j);Fa[find (P[i])]=find (j); } intCnt=0; for(intj=1; j<=n;j++) if(CHECK[J]&&FA[J]==J) cnt++; if(cnt>1) ans[i]=1; } for(intI=1; i<=n;i++) if(Ans[i]) printf ("no\n"); Elseprintf"yes\n"); return 0;}
Luogu P3144 [Usaco16open] Close farm Closing the Farm_silver Problem solving report