2503: Picture frame time Limit:3 Sec Memory limit:128 MB
submit:71 solved:31
[Submit] [Status] [Discuss] Description p large basic Circuit experimental class is an extremely boring class. Every experiment, T June is always done ahead of time, the administrator does not let T-June leave, T June can only sit there doing nothing. First of all, the experiment class, is nothing more than a few wires and some components (resistors, capacitors, inductors, etc.) with solder together. In order to pass the time, T June after each experiment is finished welding some strange things, this is his masterpiece:
T-June is not satisfied with the welding of grotesque works, the strong destructive desire to drive him to tear down the work, and then weld it into a regular shape. At this moment, T June is about to transform this monster into a ring , as his frame, the steps are as follows:
T-June agreed on two actions:
1. Fusing a solder joint: to separate or maintain some conductors connected to the solder joint (it can be understood that the wires on the solder joints are divided into several classes, the conductors in the same class are connected, and the conductors between the different classes are separated)
2. Weld The free end of the two conductors (i.e. one end not connected to any wire).
For example, in the above steps, the point A is melted, so that the conductor 1 and the conductor 2, 4 points separated, and then the D point is melted, so that 4, 5 and 3, 7 are absent, and then the melting E, 7 and 6, 8, and finally connect 1, 7.
T June want to use the least action to transform the original work into a picture frame (to use all the wires).
Input
The first line of the input file has a total of two integers n and m -the number of solder joints and conductors representing the original work, respectively (0≤ n ≤1 000, 2≤ m ≤50 000). The weld point is labeled 1~n. The next m -line has two integers per line-the label of the two solder joints connected at each end of the wire, and if not connected to any solder joint, the end is labeled 0.
The original work may not be connected.
Some solder joints may have only one wire connected to it, and these solder joints are not allowed to be melted until the end of the wire is connected to other conductors.
Some solder joints do not even have any wires attached to them, since T June only cares about the wires, so these solder joints can not be considered.
Output
The output file contains only an integer--that means T-June needs to transform the original work into a minimum number of steps in the frame.
Sample INPUT6 8
1 2
1 3
3 4
1 4
4 6
5 6
4 5
1 5
Sample Output4hint
30% of the data in n≤ 10.
100% of the data in n≤ 1000.
Source
2011 Fujian Training
Solution
The idea is a good question.
The answer is related to the degree of each melting point, and it is clear that the final case is: All sides in a connected block, and the degree of each melting point =2
So obviously for a melting point, if the degree is 0, obviously can be thrown away, if its degree >2 then obviously need to fuse him, split into a lot of small melting point, and ensure that the new melting point <=2 (priority = 2, if there is the remainder of the =1),
Then we need to merge each of the connected blocks and consider two things:
1. All the edges are in a connected block, we only need to merge the new melting point 22 with a degree of 1. (eventually merged into a ring)
2. All the edges are not in a connected block, the two connected blocks must be fused into the chain, and then the chain is melted into a large ring
And the second situation, is to make the first case of the looping chain, so special treatment
here 's a look at the order: "We're going to mark 1 if we split the point, because if we split the point we can also split into two degrees 1 points, and a heap of 2 points, this only takes one operation, if we have to dismantle the words will be one more operation, note here." ”
Code
#include <iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<ctime>using namespacestd;intMain () {Srand (0)); Freopen ("2503data.out","W", stdout); intN=rand ()% ++1, M=rand ()%50000+N; printf ("%d%d\n", n,m); for(intI=1; i<=m; i++) { intU=rand ()%n+1, V=rand ()%N; while(v==u) V=rand ()%N; printf ("%d%d\n", u,v); } return 0;}
Data Generators
#include <iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>using namespaceStd;inlineintRead () {intx=0;CharCh=GetChar (); while(ch<'0'|| Ch>'9') {ch=GetChar ();} while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx;}#defineMAXN 200010intn,m;intFa[maxn],size[maxn],d[maxn];inlineintFintx) {if(Fa[x]==x | | fa[x]==0)returnFa[x]=x;Else returnfa[x]=F (fa[x]);} InlinevoidMerge (intXintY) {if(F (x)!=f (y)) fa[f (x)]=F (y);} InlineintGetnum () {intRe=0; for(intI=1; i<=n; i++)if(F (i) ==i && d[i]) re++;returnre;}BOOLVis[maxn],mark[maxn];//vis Indicates whether the connecting block requires a crafting chain, and Mark indicates whether the connected block has been splitintMain () {N=read (), m=read (); intt=0, ans=0;//t record the number of melting points for(intI=1; i<=m; i++) { intU=read (), v=read (); if(!u) U=++n; d[u]++; if(!V) V=++n; d[v]++; Merge (U,V); } intnum=Getnum (); for(intI=1; i<=n; i++) if(D[i]) {if(d[i]&1) t++,vis[f (i)]=1; if(d[i]>2) ans++,mark[f (i)]=1; } for(intI=1; i<=n; i++) if(D[i] && i==f (i) &&!vis[i] && num>1) {T+=2; if(!mark[i]) ans++; } ans+=t>>1; printf ("%d\n", ans); return 0;}
"BZOJ-2503" Photo frame and check set + category discussion