Original title: http://acm.fzu.edu.cn/problem.php?pid=2112
First of all, the points that are not mentioned on the ticket are not required.
Then we first consider the figure has a few unicom components, we can use a and check set to maintain, false with N Unicom components, we need to n-1 to connect them together.
Finally for each unicom component, we want to enable it to go through, is to ask him whether to meet the Oraton road, that is, the unicom component of up to 2 degrees is an odd point, each more than 2 degrees is an odd point, more need an edge (because the sum of all points of a single connected component is an even number, So it is impossible to have odd number of points in odd degrees).
1#include <stdio.h>2#include <string.h>3#include <algorithm>4 #defineMAXN 1111115 using namespacestd;6 intF[MAXN],DU[MAXN],ODD[MAXN],BOOK[MAXN];7 intGETF (intv) {8 if(F[v] = =v) {9 returnv;Ten}Else{ OneF[V] =GETF (F[v]); A returnF[v]; - } - } the voidMergeintUintv) { - intA =getf (u); - intb =GETF (v); - if(a!=b) +F[B] =A; - } + intMain () { A intT; atscanf"%d",&t); - while(t--){ -Memset (Du,0,sizeof(du));//count the degrees of each node -memset (Odd,0,sizeof(odd));//count the number of nodes with odd degrees for each Unicom component -memset (book,0,sizeof(book));//Mark whether the point needs to go - intn,m; inscanf"%d%d",&n,&m); - for(inti =1; i<=n;i++)//Initialize toF[i] =i; + intu,v; - while(m--){ thescanf"%d%d",&u,&v); *du[u]++; $du[v]++;Panax NotoginsengBook[u] =1; -BOOK[V] =1; the merge (u,v); + } A intCNT =0;//number of edges required the for(inti =1; i<=n;i++){ + if(Book[i]) { - if(F[i] = =i) $cnt++; $ if(du[i]&1)//when the number of degrees is odd, it is updated with the value of the root node of the collection -ODD[GETF (i)]++; - } the } - for(inti =1; i<=n;i++){Wuyi if(F[i] = =i) theCNT + = max (0, (odd[i]-2)/2);//count the number of edges required for nodes with odd degrees of each unicom component - } Wuprintf"%d\n", cnt-1); - } About return 0; $}
Fzu 2112 and Euler pathway