UVA1627TeamThemUp
来源:互联网 发布:起点 科幻 知乎 编辑:程序博客网 时间:2024/06/16 17:24
//UVA1627TeamThemUp#include<cstdio>#include<cstring>#include<vector>using namespace std;const int MAXN = 100 + 5;int n, cc;int G[MAXN][MAXN], color[MAXN], diff[MAXN], d[MAXN][2 * MAXN];vector<int> team[MAXN][2];bool dfs(int u, int c) {color[u] = c;team[cc][c - 1].push_back(u);for(int v = 0; v < n; v++) {if(u != v && !(G[u][v] && G[v][u])) {//根据建立连通分量 if(color[v] > 0 && color[u] == color[v]) return false; if(!color[v] && !dfs(v, 3 - c)) return false; }}return true;}bool Build_graph() {memset(color, 0, sizeof(color));cc = 0;for(int i = 0; i < n; i++) {if(!color[i]) {team[cc][0].clear(); team[cc][1].clear();if(!dfs(i, 1)) return false;diff[cc] = team[cc][1].size() - team[cc][0].size();cc++;}}return true;}void print(int ans) {vector<int> team1, team2;int t;for(int i = cc - 1; i >= 0; i--) {if(d[i][ans - diff[i] + n]) {t = 0; ans -= diff[i];}else if(d[i][ans + diff[i] + n]) {t = 1; ans += diff[i];}for(int j = 0; j < team[i][t].size(); j++) team1.push_back(team[i][t][j]);for(int j = 0; j < team[i][t ^ 1].size(); j++) team2.push_back(team[i][t ^ 1][j]);}printf("%d", team2.size());for(int i = 0; i < team2.size(); i++) printf(" %d", team2[i] + 1);printf("\n");printf("%d", team1.size());for(int i = 0; i < team1.size(); i++) printf(" %d", team1[i] + 1);}void dp() {memset(d, 0, sizeof(d)); d[0][0 + n] = 1; for(int i = 0; i < cc; i++) { for(int j = -n; j <= n; j++) if(d[i][j + n]) { d[i + 1][j + diff[i] + n] = 1;//将i+1个连通分量的1组归到最终的A组中 d[i + 1][j - diff[i] + n] = 1;//将i+1个连通分量的1组归到最终的B组中}//两种决策 }for(int ans = 0; ans <= n; ans++) {if(d[cc][ans + n]) { print(ans); return;}else if(d[cc][-ans + n]) {print(-ans); return;}}}int main() { int T, kase = 0;scanf("%d", &T);while(T--) {if(kase++) printf("\n");scanf("%d", &n);int tmp; memset(G, 0, sizeof(G));for(int i = 0; i < n; i++) while(scanf("%d", &tmp) == 1 && tmp) G[i][tmp - 1] = 1;if(n == 1 || !Build_graph()) printf("No solution");else dp();printf("\n");}return 0;}/*253 4 5 01 3 5 02 1 4 5 02 3 5 01 2 3 4 052 3 5 01 4 5 3 01 2 5 01 2 3 04 3 2 1 0*/
阅读全文
0 0
- UVA1627TeamThemUp
- Jquery 子页面触发父级事件、父级获取子级信息
- 机械臂(3)——使用MATLAB建立机械臂模型
- sapui5代码中使用jquey插件flot画图
- 设计模式--观察者
- 解决subline安装后无package control菜单项
- UVA1627TeamThemUp
- python爬虫学习第十六天——今天有点晚,请假一天
- 【leetcode】第20题 Valid Parentheses 题目+解析+代码
- 潘多拉盒子安装aria2命令
- 数据结构-线性表
- Codeforces Round #425 (Div. 2) C
- JavaScript属性值操作
- 个人学习过程:C#篇
- c++限制某个class所能产生对象数量的方法