Test instructions: Soda has a $n$ point $m$ side of the dichotomy, he wants to add edge to make this graph into a number of sides of the most complete binary graph. So he wanted to know how many new edges he could add. Note that heavy edges are not allowed.
Idea: Two-figure coloring this simple, mainly DP, there is a time limit. I think it should be to find out all the connected components, the number of points on each side of the connected component is stored, followed by a unified DP. But!! Time is only 1s, but also 100 examples, that is, about 1 million of the complexity of the line, but there are 10,000 points, when m=5000, this is not.
I do not understand how to start the problem can be guaranteed to be correct and do not time out, should be optimized very badly ~
Post a tle code that I think is right:
1#include <bits/stdc++.h>2 #defineLL Long Long3 #definePII pair<int,int>4 #defineINF 0x7f7f7f7f5 using namespacestd;6 Const intn=10100;7vector<int>Vect[n];8 intCol[n];9 intSet1, Set2;Ten One voidColorintUintc) A { -col[u]=C; - if(col[u]==1) set1++;//Count the numbers on both sides the Elseset2++; - - for(intI=0; I<vect[u].size (); i++) - { + intt=Vect[u][i]; - if(!col[t])//No Color +Color (t,3-Col[u]); A } at } - - -bitset<n/2>Dp[n]; - ints1[n/2],s2[n/2]; - intCalintNintm) in { - if(m==n/2* (n-n/2) )return 0; to for(intI=0; i<=n; i++) Vect[i].clear (), Dp[i].reset (); + -memset (Col,0,sizeof(col)); the intcn1=0, k=0; * for(intI=1; i<=n; i++) $ {Panax Notoginseng if(!Col[i]) - { theSet1=set2=0; +Color (i,1); A theS1[k]=set1;//points on both sides of each connected component +s2[k++]=Set2; - } $ } $ - if(k==1)returns1[0] * s2[0]-M;//Connectivity Diagram -dp[0][0]=1; the for(intI=0; i<k; i++)//in this DP, try to balance the points on both sides - {Wuyians=0; theset1=S1[i]; -Set2=S2[i]; Wu - for(intj=n/2; j>=set1; j--) About if(Dp[i][j-set1]) dp[i+1][j]=1, ans=Max (ans,j); $ - for(intj=n/2; j>=set2; j--) - if(Dp[i][j-set2]) dp[i+1][j]=1, ans=Max (ans,j); - } A returnans* (N-ans)-m; + } the - $ intMain () the { the //freopen ("Input.txt", "R", stdin); the intN, M, T, p, Q, A, B; theCin>>T; - in while(t--) the { thescanf"%d%d",&n,&m); About for(intI=0; i<m; i++) the { thescanf"%d%d",&a,&b); the Vect[a].push_back (b); + Vect[b].push_back (a); - } theprintf"%d\n", Cal (n, m));Bayi } the return 0; the}
AC Code
HDU 5313 bipartite graph (binary graph coloring, DP) TLE!!!