UVA 1627 Team them up!
来源:互联网 发布:淘宝不能好评返现了 编辑:程序博客网 时间:2024/06/07 03:28
题目链接:http://acm.hust.edu.cn/vjudge/problem/51188
题意:n个人要分为两组,每个组都不可以为空,给出这n个人的关系(单向的),要求分完组之后,每个组内的人必须两两都互相认识。问怎么分可以使得两组人数差最小。
思路:参考书上的思路,将非互相认识的人之间建一条无向边,这样n个点可能会分为若干个联通子图,对于一个联通的子图进行黑白染色,所以黑白子的个数是固定的,黑子和白字分别分到两个集合里去。然后拿每个联通子图的黑白子数差去做一下背包,最接近0的可行值就是答案。
#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <cstdlib>#include <iostream>#include <algorithm>#include <stack>#include <map>#include <set>#include <vector>#include <sstream>#include <queue>#include <utility>using namespace std;#define rep(i,j,k) for (int i=j;i<=k;i++)#define Rrep(i,j,k) for (int i=j;i>=k;i--)#define Clean(x,y) memset(x,y,sizeof(x))#define LL long long#define ULL unsigned long long#define inf 0x7fffffff#define mod 100000007const int maxn = 109;const int m = 100;int n,tot;bool can;bool a[maxn][maxn];vector<int> edge[maxn];int flag[maxn];vector<int> S[2][maxn]; // S[0][i]表示第i个联通子图黑子的集合 S[1][i]表示第i个联通子图白子的集合int num[maxn]; //第i个联通子图 黑 - 白 的差bool dp[maxn][maxn*2];int last[maxn][maxn*2];int sign[maxn][maxn*2];int dfs( int x , int type ){ int ans = 1; flag[x] = type; for( int i = 0; i < (int)(edge[x].size()); i++ ) if ( !flag[ edge[x][i] ] ) ans -= dfs( edge[x][i] , -type ); else if ( flag[ edge[x][i] ] == type ) can = false; if ( type == 1 ) S[0][tot].push_back(x); else S[1][tot].push_back(x); return ans;}void init(){ scanf("%d",&n); Clean(a,false); rep(i,1,n) { edge[i].clear(); S[0][i].clear(); S[1][i].clear(); int temp; while( scanf("%d",&temp) == 1 ) { if ( !temp ) break; a[i][temp] = true; } } rep(i,1,n) rep(j,1,n) if ( i != j && !(a[i][j]&&a[j][i]) ) edge[i].push_back(j);//将两个非互相认识的人建边}bool solve(){ Clean(flag,0); tot = 0; //联通子图的个数 can = true; rep(i,1,n) if ( !flag[i] ) //每个点是否访问过 { num[++tot] = dfs( i , 1 ); if ( !can ) return false; //联通子图内有矛盾 } Clean(dp,false); dp[0][m] = true; rep(i,1,tot) { rep(j,-n,n) if ( dp[i-1][j+m] ) //每次放两边,分别是黑 白放A、B集合或者黑白放B、A集合 { dp[i][j+m+num[i]] = true; sign[i][j+m+num[i]] = 1;//正着放,打印解需要用 dp[i][j+m-num[i]] = true; sign[i][j+m-num[i]] = -1;//反过来放 } } return true;}void print(){ int ans = 0; rep(i,0,n) { if ( dp[tot][m+i] ) //找到最小的解 {ans = i;break;} } vector<int> out1,out2; out1.clear(); out2.clear(); int pos = ans; Rrep(i,tot,1) { if ( sign[i][m+pos] == 1 )//第i个联通图是正着放的 { rep(j,0,(int)(S[0][i].size()-1)) out1.push_back(S[0][i][j]); rep(j,0,(int)(S[1][i].size()-1)) out2.push_back(S[1][i][j]); pos -= num[i]; } else { rep(j,0,(int)(S[1][i].size()-1)) out1.push_back(S[1][i][j]); rep(j,0,(int)(S[0][i].size()-1)) out2.push_back(S[0][i][j]); pos += num[i]; } } printf("%d",out1.size()); rep(i,0,(int)out1.size()-1) printf(" %d",out1[i]); puts(""); printf("%d",out2.size()); rep(i,0,(int)out2.size()-1) printf(" %d",out2[i]); puts("");}int main(){ int T; cin>>T; while(T--) { init(); if (!solve()) puts("No solution"); else print(); if ( T ) puts(""); } return 0;}
0 0
- Uva-1627-Team them up!
- UVA 1627 Team them up!
- Team them up! UVA
- Team them up! UVA
- Team them up! UVA
- uva 1627 Team them up! 团队分组
- Uva-1627 Team them up!(背包变形)
- 1627 - Team them up!
- UVa-10618 Tango Tango Insurrection&& UVa-1627 Team them up!
- UVa #1627 Team them up! (例题9-19)
- uva 1627 Team them up! 二分图 + 连通分量 + 背包
- 【暑假】[深入动态规划]UVa 1627 Team them up!
- Uva 1627 Team them up!(dp+二分图染色)
- poj1112 Team Them Up!
- poj1112 Team Them Up!
- UVA 1627 Team them up!(二分图染色+并查集+背包)
- POJ 1112 Team Them Up
- [ZOJ1462][POJ1112] Team Them Up!
- current_timestamp
- 关于大神--->>>快速集成iOS基于RTMP的视频推流
- linux内核 策略路由之添加
- CF 9B模拟
- UVA 1252 Twenty Questions
- UVA 1627 Team them up!
- UVA 10934 Dropping water balloons
- 简单算法题之 Two Sum
- #ifdef #else #endif #fi #ifndef 的用法
- java IDEA加密/解密 源码
- POJ 3311 Hie with the Pie (Floyd + 暴力全排列 || Floyd + 状态压缩DP (已补充))
- linux内核 策略路由之查找
- 【并查集】【模拟】Codeforces 698B & 699D Fix a Tree
- struts2配置