There are a lot of tips telling us that some fruits must not be eaten with some other fruits, or we might get ourselves in serious trouble. For example, bananas can not be eaten with cantaloupe (哈密瓜), otherwise it will lead to kidney deficiency (肾虚).
Now you are given a long list of such tips, and a big basket of fruits. You are supposed to pick up those fruits so that it is safe to eat any of them.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers: N, the number of tips, and M, the number of fruits in the basket. both numbers are no more than 100.
Then two blocks follow. The first block contains N pairs of fruits which must not be eaten together, each pair occupies a line and there is no duplicated tips; and the second one contains M fruits together with their prices, again each pair in a line. To make it simple, each fruit is represented by a 3-digit ID number. A price is a positive integer which is no more than 1000. All the numbers in a line are separated by spaces.
Output Specification:
For each case, first print in a line the maximum number of safe fruits. Then in the next line list all the safe fruits, in increasing order of their ID's. The ID's must be separated by exactly one space, and there must be no extra space at the end of the line. Finally in the third line print the total price of the above fruits. Since there may be many different solutions, you are supposed to output the one with a maximum number of safe fruits. In case there is a tie, output the one with the lowest total price. It is guaranteed that such a solution is unique.
Sample Input:
16 20
001 002
003 004
004 005
005 006
006 007
007 008
008 003
009 010
009 011
009 012
009 013
010 014
011 015
012 016
012 017
013 018
020 99
019 99
018 4
017 2
016 3
015 6
014 5
013 1
012 1
011 1
010 1
009 10
008 1
007 2
006 5
005 3
004 4
003 6
002 1
001 2
Sample Output:
12
002 004 006 008 009 014 015 016 017 018 019 020
239
1 #include<bits/stdc++.h> 2 using namespace std; 3 struct node 4 { 5 set<int> s; 6 int p; 7 int dp; 8 bool v; 9 } no; 10 11 map<int,node> m; 12 vector<set<int> > v; 13 vector<int> vb,vr,vn; 14 int mnum,msum; 15 16 void dfs(set<int>::iterator its,int num,int sum,int vi,int vj) 17 { 18 if(its==v[vi].end()) 19 { 20 if(num>mnum||(num==mnum&&sum<msum)) 21 { 22 vr=vb; 23 mnum=num; 24 msum=sum; 25 } 26 return; 27 } 28 else if(num+v[vi].size()-vj<mnum) 29 return; 30 else if(num+m[*its].dp<mnum&&m[*its].dp>0) 31 return; 32 else 33 { 34 int kk,k=*its; 35 if(!vb.empty()) 36 kk=vb.back(); 37 38 if(!m[k].v) 39 { 40 set<int>::iterator itss; 41 42 vector<int> vl; 43 for(itss=m[k].s.begin();itss!=m[k].s.end();++itss) 44 if(!m[*itss].v) 45 { 46 m[*itss].v=true; 47 vl.push_back(*itss); 48 } 49 50 vb.push_back(k); 51 ++its; 52 dfs(its,num+1,sum+m[k].p,vi,vj+1); 53 --its; 54 55 if(vb.size()>1) 56 { 57 if(m[k].dp+1>m[kk].dp) 58 m[kk].dp=m[k].dp+1; 59 } 60 61 vb.pop_back(); 62 for(int i=0;i<vl.size();i++) 63 m[vl[i]].v=false; 64 } 65 ++its; 66 dfs(its,num,sum,vi,vj+1); 67 --its; 68 69 if(vb.size()>1) 70 { 71 if(m[k].dp+1>m[kk].dp) 72 m[kk].dp=m[k].dp+1; 73 } 74 } 75 } 76 77 void bfs() 78 { 79 queue<int> q; 80 map<int,node>::iterator itm; 81 set<int>::iterator its; 82 for(itm=m.begin();itm!=m.end();++itm) 83 { 84 if((itm->second).v) 85 { 86 (itm->second).v=false; 87 int vs=v.size(); 88 set<int> sv; 89 v.push_back(sv); 90 v[vs].insert(itm->first); 91 92 93 q.push(itm->first); 94 for(;!q.empty();q.pop()) 95 { 96 int k=q.front(); 97 for(its=m[k].s.begin();its!=m[k].s.end();++its) 98 { 99 if(m[*its].v) 100 { 101 m[*its].v=false; 102 v[vs].insert(*its); 103 q.push(*its); 104 } 105 } 106 } 107 } 108 } 109 } 110 111 int main() 112 { 113 // ios::sync_with_stdio(false); 114 // freopen("data.txt","r",stdin); 115 int n,k,c1,c2,x,p1,p2,c=0,t=0; 116 scanf("%d %d",&n,&k); 117 mnum=0,msum=0; 118 for(;n--;) 119 { 120 scanf("%d %d",&c1,&c2); 121 m[c1].s.insert(c2); 122 m[c2].s.insert(c1); 123 } 124 for(;k--;) 125 { 126 scanf("%d %d",&c1,&c2); 127 m[c1].p=c2; 128 m[c1].v=true; 129 m[c1].dp=0; 130 } 131 132 bfs(); 133 134 int mmn=0,mms=0; 135 for(int i=0;i<v.size();i++) 136 { 137 mnum=0,msum=0; 138 dfs(v[i].begin(),0,0,i,0); 139 mmn+=mnum; 140 mms+=msum; 141 for(int i=0;i<vr.size();i++) 142 vn.push_back(vr[i]); 143 } 144 sort(vn.begin(),vn.end()); 145 printf("%d %03d",mmn,vn[0]); 146 for(int i=1;i<vn.size();i++) 147 printf(" %03d",vn[i]); 148 printf(" %d",mms); 149 return 0; 150 }