标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 840 Accepted Submission(s): 285
// bitset::reset #include <iostream> // std::cout #include <string> // std::string #include <bitset> // std::bitset int main () { std::bitset<4> foo (std::string("1011")); std::cout << foo.reset(1) << ‘\n‘; // 1001 std::cout << foo.reset() << ‘\n‘; // 0000 return 0; } // bitset::operator[] #include <iostream> // std::cout #include <bitset> // std::bitset int main () { std::bitset<4> foo; foo[1]=1; // 0010 foo[2]=foo[1]; // 0110 std::cout << "foo: " << foo << ‘\n‘; return 0; }
#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #include<bitset> #include<vector> using namespace std; const int maxn=1e4+20; #define min(a,b) ((a)<(b)?(a):(b)) vector<int>G[maxn]; int color[maxn],d[maxn][3]; int n,m; void dfs(int cc,int u,int col){ // printf("%d---%d----\n",u,cc); if(!color[u]){ color[u]=col; d[cc][col+1]++; } for(int i=0;i<G[u].size();i++){ int v=G[u][i]; if(!color[v]){ dfs(cc,v,-col); } } } int main(){ int t,a,b; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); for(int i=0;i<m;i++){ scanf("%d%d",&a,&b); G[a].push_back(b); G[b].push_back(a); } int c=0; for(int i=1;i<=n;i++){ if(!color[i]){ c++; dfs(c,i,1); } } const int V=5001; bitset<V>dp; dp.reset(); dp[0]=1; for(int i=1;i<=c;i++){ dp=(dp<<d[i][0])|(dp<<d[i][2]); } int k=1; for(int i=n/2;i>=1;i--){ if(dp[i]){ k=i; break; } } printf("%d\n",(n-k)*k-m); for(int i=0;i<=n;i++) G[i].clear(); memset(color,0,sizeof(color)); memset(d,0,sizeof(d)); } return 0; } /* 8 8 6 1 2 2 3 2 4 5 6 6 7 6 8 */
HDU 5313——Bipartite Graph——————【二分图+dp+bitset优化】
标签:
原文地址:http://www.cnblogs.com/chengsheng/p/4679746.html