1182. Team Them Up!
Time limit: 1.0 second Memory limit: 64 MB
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 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 A
ij
(1 ≤ A
ij
≤ N, A
ij
≠ i) separated by spaces. The list represents identifiers of persons that i
th
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). Otherwise write a solution on two lines. On the first line 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.
Sample
input | output |
---|---|
5 2 3 5 0 1 4 5 3 0 1 2 5 0 1 2 3 0 4 3 2 1 0 |
3 1 3 5 2 2 4 |
Problem Author: Vladimir Kotov, Roman Elizarov
Problem Source: 2001-2002 ACM Northeastern European Regional Programming Contest
*****************************************************************************************************
Complement FIG && && DFS algorithm knapsack problem f [] [] ,,, coloring of the program are explained
*****************************************************************************************************
1 / * The main question is to complement FIGS claim 2 and dfs communication request blocks, each block is divided into two portions communicating X [] [], Y [] []; . 3 then with 01 backpack and obtaining the minimum mark 4 final output the results, noted here x [i] [0], y [i] [0] is the number of i-th block in two communicating parts . 5 V [i] array is used to mark the point i belongs to which group. . 6 * / . 7 #include <the iostream> . 8 #include < String > . 9 #include <CString> 10 #include <cstdio> . 11 #include <the cmath> 12 is #include <algorithm> 13 is the using namespace STD; 14 const int MAXN = 110 ; 15 int E [1001][1001]; 16 int v[1001],x[1001][1001],y[1001][1001]; 17 int f[1001][1001]; 18 int n,m,j,i,p; 19 int tmp; 20 int absw(int x) 21 { 22 return (x<0)?(-x):x; 23 } 24 void init() 25 { 26 cin>>n; 27 memset(e,0,sizeof(e)); 28 for(i=0;i<n;i++)//建图 29 { 30 while(cin>>tmp&&tmp) 31 { 32 e[i][tmp-1]=1; 33 } 34 } 35 for(i=0;i<n;i++)//求补图 36 for(j=0;j<i;j++) 37 { 38 if(e[i][j]==1&&e[j][i]==1) 39 e[i][j]=e[j][i]=0; 40 else 41 e[i][j]=e[j][i]=1; 42 } 43 } 44 bool dfs(int k,int p,int xx)//染色、求连通块 45 { 46 for(int it=0;it<n;it++) 47 if(it!=k&&e[it][k]==1) 48 { 49 if(v[it]==-1) 50 { 51 if(xx==1) 52 y[p][++y[p][0]]=it; 53 else 54 x[p][++x[p][0]]=it; 55 v[it]=xx; 56 if(!dfs(it,p,1-xx))return false; 57 } 58 else 59 if(v[k]==v[it])return false; 60 61 } 62 return true; 63 } 64 bool solve() 65 { 66 int ans,g1; 67 p=0; 68 memset(v,-1,sizeof(v)); 69 for(i=0;i<n;i++) 70 { 71 if(v[i]==-1) 72 { 73 v[i]=0;x[p][0 ] = . 1 ; X [P] [ . 1 ] = I; Y [P] [ 0 ] = 0 ; 74 IF (DFS (I, P,! . 1 )) return to false ; 75 P ++; // P interconnecting blocks 76 } 77 } 78 Memset (F, 255 , the sizeof (F)); 79 F [ 0 ] [X [ 0 ] [ 0 ] -Y [ 0 ] [ 0 ] + MAXN] = 0 ; 80 F [ 0] [Y [ 0 ] [ 0 ] the -X-[ 0 ] [ 0 ] + MAXN] = . 1 ; 81 for (I = . 1 I ++;; I <P) // before the knapsack problem, f [i] [j] denotes the i-th communication block fetch when the difference is j x (0) or Y (. 1); 82 for (J = . 1 ; J <= MAXN * 2 ; J ++ ) 83 { 84 iF (F [I- . 1 ] [J] ! = - . 1 ) 85 F [I] [J + X [I] [ 0 ] -Y [I] [ 0 ]] = 0 ; 86 IF (F [I- . 1 !] [J] = - . 1) 87 f[i][j+y[i][0]-x[i][0]]=1; 88 } 89 ans=-1; 90 for(j=0;j<=maxn*2;j++) 91 if(f[p-1][j]!=-1) 92 if(ans==-1||absw(ans-maxn)>absw(j-maxn)) 93 ans=j;//最小差值 94 g1=0; 95 for(i=p-1; I> = 0 ; i--) // knapsack problem looking forward from the 96 IF (F [I] [ANS] == 0 ) 97 { 98 G1 + = X [I] [ 0 ]; 99 for (J = . 1 ; J <= X [I] [ 0 ]; J ++) V [X [I] [J]] = . 1 ; 100 for (J = . 1 ; J <= Y [I] [ 0 ]; J ++) V [Y [I] [J]] = 2 ; 101 ANS - = (X [I] [ 0 ] -Y [I] [ 0 ]); 102 } 103 the else 104 { 105 G1 + = Y [I] [ 0 ]; 106 for (J = . 1 ; J <= X [I] [ 0 ]; J ++ ) 107 V [X [I] [J]] = 2 ; 108 for (J = . 1 ; J <= Y [I] [ 0 ]; J ++ ) 109 V [Y [I] [J]] = . 1 ; 110 ANS - = (Y [I] [ 0 ] the -X-[I] [ 0 ]); 111 } 112 // outputs the calculation result 113 COUT << << G1 ' ' ; 114 for (I =0;i<n;i++) 115 if(v[i]==1) 116 cout<<i+1<<' '; 117 cout<<endl; 118 cout<<n-g1<<' '; 119 for(i=0;i<n;i++) 120 if(v[i]==2) 121 cout<<i+1<<' '; 122 cout<<endl; 123 return true; 124 125 } 126 int main() 127 { 128 init(); 129 if(!solve()) 130 cout<<"No solution"<<endl; 131 }
adhere to! ! ! ! !
Reproduced in: https: //www.cnblogs.com/sdau--codeants/p/3306909.html