Total Submissions:1228 Solved:139
Time Limit: 3000 mSec
Problem Description
Your task is to divide a number of persons into two teams, in such a way, that:
• everyone belongs to one of the teams;
• every team has at least one member;
• every person in the team knows every other person in his team;
• teams are as close in their sizes as possible.
This task may have many solutions. You are to find and output any solution, or to report that the solution does not exist.
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs. For simplicity, all persons are assigned a unique integer identifier from 1 to N. Thefirstlineintheinputfilecontainsasingleintegernumber N (2 ≤ N ≤ 100) —thetotalnumber of persons to divide into teams, followed by N lines — one line per person in ascending order of their identifiers. Each line contains the list of distinct numbers Aij (1 ≤ Aij ≤ N, Aij ̸= i) separated by spaces. The list represents identifiers of persons that i-th person knows. The list is terminated by ‘0’.
Output
Sample Input
5
3 4 5 0
1 3 5 0
2 1 4 5 0
2 3 5 0
1 2 3 4 0
5
2 3 5 0
1 4 5 3 0
1 2 5 0
1 2 3 0
4 3 2 1 0
Sample Output
No solution
3 1 3 5
2 2 4
题解:有一阵子没写博客了,感到很内疚,这个东西还是要坚持。这个题用的东西比较杂,但每一部分都不算难,首先是二分图的判断,染色法dfs很好做,之后就是一个01背包的变形,不过做法似乎和背包没什么关系,数据小,比较暴力的方式就能过,最后是输出解,用的是lrj之前讲的方法。
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 const int maxn = 100 + 5; 6 7 int n, tot, belong[maxn]; 8 bool gra[maxn][maxn]; 9 vector<int> team[maxn][2]; 10 int delta[maxn]; 11 12 bool dfs(int u,int flag) { 13 belong[u] = flag; 14 team[tot][flag - 1].push_back(u); 15 for (int v = 1; v <= n; v++) { 16 if (gra[u][v] && u != v) { 17 if (belong[v] == flag) return false; 18 if (!belong[v] && !dfs(v, 3 - flag)) return false; 19 } 20 } 21 return true; 22 } 23 24 bool build_graph() { 25 memset(belong, 0, sizeof(belong)); 26 for (int u = 1; u <= n; u++) { 27 if (!belong[u]) { 28 tot++; 29 team[tot][0].clear(); 30 team[tot][1].clear(); 31 if (!dfs(u, 1)) return false; 32 } 33 } 34 return true; 35 } 36 37 bool dp[maxn][maxn << 2]; 38 vector<int> ans, ans1; 39 40 void DP() { 41 for (int i = 1; i <= tot; i++) { 42 delta[i] = team[i][0].size() - team[i][1].size(); 43 //printf("(%d-%d) = %d ", team[i][0].size(), team[i][1].size(), delta[i]); 44 } 45 memset(dp, false, sizeof(dp)); 46 dp[0][0 + n] = true; 47 for (int i = 0; i <= tot; i++) { 48 for (int j = -n; j <= n; j++) { 49 if (dp[i][j + n]) dp[i + 1][j + n + delta[i + 1]] = dp[i + 1][j + n - delta[i + 1]] = true; 50 } 51 } 52 53 int res = 0; 54 for (int j = 0; j <= n; j++) { 55 if (dp[tot][n + j]) { 56 res = n + j; 57 break; 58 } 59 if (dp[tot][n - j]) { 60 res = n - j; 61 break; 62 } 63 } 64 ans.clear(), ans1.clear(); 65 for (int i = tot; i >= 1; i--) { 66 if (dp[i - 1][res - delta[i]]) { 67 for (int j = 0; j < (int)team[i][0].size(); j++) { 68 ans.push_back(team[i][0][j]); 69 } 70 for (int j = 0; j < (int)team[i][1].size(); j++) { 71 ans1.push_back(team[i][1][j]); 72 } 73 res -= delta[i]; 74 } 75 else if (dp[i - 1][res + delta[i]]) { 76 for (int j = 0; j < (int)team[i][0].size(); j++) { 77 ans1.push_back(team[i][0][j]); 78 } 79 for (int j = 0; j < (int)team[i][1].size(); j++) { 80 ans.push_back(team[i][1][j]); 81 } 82 res += delta[i]; 83 } 84 } 85 printf("%d", ans.size()); 86 for (int i = 0; i < (int)ans.size(); i++) printf(" %d", ans[i]); 87 printf(" "); 88 printf("%d", ans1.size()); 89 for (int i = 0; i < (int)ans1.size(); i++) printf(" %d", ans1[i]); 90 printf(" "); 91 } 92 93 int main() 94 { 95 //freopen("input.txt", "r", stdin); 96 int iCase; 97 scanf("%d", &iCase); 98 while (iCase--) { 99 tot = 0; 100 memset(gra, false, sizeof(gra)); 101 scanf("%d", &n); 102 int v; 103 for (int u = 1; u <= n; u++) { 104 while (true) { 105 scanf("%d", &v); 106 if (v == 0) break; 107 gra[u][v] = true; 108 } 109 } 110 for (int u = 1; u <= n; u++) { 111 for (int v = 1; v < u; v++) { 112 if (!gra[u][v] || !gra[v][u]) gra[u][v] = gra[v][u] = true; 113 else gra[u][v] = gra[v][u] = false; 114 } 115 } 116 117 if (n == 1 || !build_graph()) printf("No solution "); 118 else DP(); 119 120 if (iCase) printf(" "); 121 } 122 return 0; 123 }