ZOJ 1462 Team Them Up! (二分图+路径保存DP)
2014-08-03 23:10
337 查看
Team Them Up!
Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge
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
For simplicity, all persons are assigned a unique integer identifier from 1 to N.
The first line in the input file contains a single integer number N (2 <= N <= 100) - the total number 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 ith person knows. The list is terminated by 0.
Output
If the solution to the problem does not exist, then write a single message "No solution" (without quotes) to the output file. Otherwise write a solution on two lines. On the first line of the output file write the number of persons in the first team, followed
by the identifiers of persons in the first team, placing one space before each identifier. On the second line describe the second team in the same way. You may write teams and identifiers of persons in a team in any order.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank
line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Sample Input
2
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
Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge
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
For simplicity, all persons are assigned a unique integer identifier from 1 to N.
The first line in the input file contains a single integer number N (2 <= N <= 100) - the total number 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 ith person knows. The list is terminated by 0.
Output
If the solution to the problem does not exist, then write a single message "No solution" (without quotes) to the output file. Otherwise write a solution on two lines. On the first line of the output file write the number of persons in the first team, followed
by the identifiers of persons in the first team, placing one space before each identifier. On the second line describe the second team in the same way. You may write teams and identifiers of persons in a team in any order.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank
line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Sample Input
2
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
# include <iostream> # include <string.h> # include <fstream> # include <stdio.h> # include <cmath> using namespace std; int n,m; bool mp[105][105]; int color[105]; bool vis[105]; int lt[2][105]; int p[105][105]; int dp[105][105]; int pp; const int inf = 1<<29; struct po{ int x,c; }; po route[105][105]; bool dfs(int ii,int c){ vis[ii] = true; bool fg ; color[ii] = c; lt[c%2][pp]++; for(int i=1;i<=n;++i){ if(mp[ii][i]){ if(!vis[i]){ fg = dfs(i,(c%2 == 0?c+1:c-1)); if(fg == false) return false; } else{ if(color[ii] == color[i]) return false; } } } return true; } int main() { //ifstream cin("1.txt"); int T; int temp; int ttt=0; while(cin>>T){ ttt=0; while(T--){ cin>>n; if(ttt++) cout<<"\n"; memset(mp,false,sizeof(mp)); memset(vis,false,sizeof(vis)); memset(dp,0,sizeof(dp)); memset(color,-1,sizeof(color)); memset(lt,0,sizeof(lt)); for(int i=1;i<=n;++i){ //mp[i][i] = false; while(cin>>temp&&temp){ mp[i][temp] = true; } } for(int i=1;i<=n;++i){ //构图 for(int j=i+1;j<=n;++j){ if(!(mp[i][j]&&mp[j][i])){ mp[i][j] = mp[j][i] = true; } else{ mp[i][j] = mp[j][i] = false; } } } int pos = 0; bool fg = true; pp=0; for(int i=1;i<=n;++i){ //着色 if(!vis[i]){ fg = dfs(i,pos); if(fg == false) break; pos+=2; pp++; } } if(fg == false){ cout<<"No solution\n"; } else{ dp[0][0] = 1; int num = 0; for(int i=0;i<pp;++i){ //dp+路径保存 for(int j=0;j<=num;++j){ int k=num-j; if(dp[j][k]){ route[j+lt[0][i]][k+lt[1][i]].c = i*2; dp[j+lt[0][i]][k+lt[1][i]] = 1; route[j+lt[1][i]][k+lt[0][i]].c = i*2+1; dp[j+lt[1][i]][k+lt[0][i]] = 1; } } num+=lt[0][i] + lt[1][i]; } int posi,posj; int res = inf; for(int i=1;i<=n;++i){ if(dp[i][n-i]&&res>abs(i-n+i)){ res = abs(i-n+i); posi = i;posj = n-i; } } cout<<posi; memset(vis,false,sizeof(vis)); int p1 = posi,p2 = posj; int cc = route[p1][p2].c; int tp = pp-1; while(1){ for(int i=1;i<=n;++i){ if(color[i] == cc){ vis[i] = true; cout<<" "<<i; } } if(cc%2==0){ p1-=lt[0][tp]; p2-=lt[1][tp]; tp--; } else{ p1-=lt[1][tp]; p2-=lt[0][tp]; tp--; } cc = route[p1][p2].c; if(p1==0&&p2==0) break; } cout<<endl; cout<<n-posi; for(int i=1;i<=n;++i){ if(!vis[i]) cout<<" "<<i; } cout<<endl; } } } return 0; }
相关文章推荐
- ZOJ_1462 Team Them Up! 求完连通分量后,再进行背包,之后背包路径回溯
- 【POJ 1112】Team Them Up!(二分图染色+DP)
- [ZOJ1462][POJ1112] Team Them Up!
- POJ 1112 Team Them Up!【二分图染色+DP】
- poj 1112 Team Them Up! 二分图染色+dp
- poj1112 Team Them Up!(二分图染色+dp)
- Team them up! UVA - 1627(dp+二分图+打印路径+连通分量)
- UVA - 1627 Team them up!——二分图染色+01背包
- POJ 1112 Team Them Up! 二分图判定+01背包
- POJ 1112 Team Them Up! DP
- uva1627 - Team them up!【dp】
- 100道动态规划——16 UVA 1627 Team them up! 二分图,背包,不过关键还是学会转化
- poj 1112 Team Them Up! (补图+dp)
- UVa 1627 Team them up! (01背包+二分图)
- POJ 1112 Team Them Up! (二分图染色+连通分量+DP)
- POJ1112 Team Them Up![二分图染色 补图 01背包]
- UVa1627 - Team them (二分图染色+dp)
- Uva 1627 Team them up!(dp+二分图染色)
- UVA 1627 Team them up!——dp
- uva 1627 Team them up! 二分图 + 连通分量 + 背包