Escape
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 10382 Accepted Submission(s): 2485
Problem Description
2012 If this is the end of the world how to do? I do not know how. But now scientists have found that some stars, who can live, but some people do not fit to live some of the planet. Now scientists want your help, is to determine what all of people can live in these planets.
Input
More set of test data, the beginning of each data is n (1 <= n <= 100000), m (1 <= m <= 10) n indicate there n people on the earth, m representatives m planet, planet and people labels are from 0. Here are n lines, each line represents a suitable living conditions of people, each row has m digits, the ith digits is 1, said that a person is fit to live in the ith-planet, or is 0 for this person is not suitable for living in the ith planet.
The last line has m digits, the ith digit ai indicates the ith planet can contain ai people most..
0 <= ai <= 100000
The last line has m digits, the ith digit ai indicates the ith planet can contain ai people most..
0 <= ai <= 100000
Output
Determine whether all people can live up to these stars
If you can output YES, otherwise output NO.
If you can output YES, otherwise output NO.
Sample Input
1 1
1
1
2 2
1 0
1 0
1 1
Sample Output
YES
NO
Source
Recommend
lcy
题意:
给你n个人m个星球,和第i个人能否适应第j个星球,1为适应,0为不适应。问你全部人能不能去星球上。
矩阵建边,跑一下二分图多重匹配。如果这个人无法去任意星球,直接break。
普通版:1560ms
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #include <algorithm> 6 #include <cmath> 7 #include <vector> 8 #include <queue> 9 #include <map> 10 #include <stack> 11 #include <set> 12 using namespace std; 13 typedef long long LL; 14 #define ms(a, b) memset(a, b, sizeof(a)) 15 #define pb push_back 16 #define mp make_pair 17 const LL INF = 0x7fffffff; 18 const int inf = 0x3f3f3f3f; 19 const int mod = 1e9+7; 20 const int maxn = 100000+10; 21 const int maxm = 10+10; 22 int n, m, uN, vN; 23 int g[maxn][maxm]; 24 int linker[maxm][maxn]; 25 bool used[maxm]; 26 int num[maxm]; 27 bool dfs(int u) 28 { 29 for(int v = 0;v<vN;v++) 30 if(g[u][v] && !used[v]){ 31 used[v] = true; 32 if(linker[v][0]<num[v]){ 33 linker[v][++linker[v][0]] = u; 34 return true; 35 } 36 for(int i = 1;i<=num[0];i++) 37 if(dfs(linker[v][i])){ 38 linker[v][i] = u; 39 return true; 40 } 41 } 42 return false; 43 } 44 int hungary() 45 { 46 int res = 0; 47 for(int i = 0;i<vN;i++){ 48 linker[i][0] = 0; 49 } 50 for(int u = 0;u<uN;u++){ 51 ms(used, false); 52 if(dfs(u)) res++; 53 else return res; 54 } 55 return res; 56 } 57 void init() { 58 ms(g, 0); 59 } 60 void solve() { 61 for(int i = 0;i<n;i++){ 62 for(int j = 0;j<m;j++){ 63 int x; 64 scanf("%d", &x); 65 if(x==1){ 66 g[i][j] = 1; 67 } 68 else{ 69 g[i][j] = 0; 70 } 71 } 72 } 73 for(int i = 0;i<m;i++) 74 scanf("%d", &num[i]); 75 vN = m, uN = n; 76 int ans = hungary(); 77 // printf("%d\n", ans); 78 if(ans==n){ 79 printf("YES\n"); 80 } 81 else{ 82 printf("NO\n"); 83 } 84 } 85 int main() { 86 #ifdef LOCAL 87 freopen("input.txt", "r", stdin); 88 // freopen("output.txt", "w", stdout); 89 #endif 90 while(~scanf("%d%d", &n, &m)){ 91 init(); 92 solve(); 93 } 94 return 0; 95 }
fread版:249ms
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #include <algorithm> 6 #include <cmath> 7 #include <vector> 8 #include <queue> 9 #include <map> 10 #include <stack> 11 #include <set> 12 using namespace std; 13 typedef long long LL; 14 #define ms(a, b) memset(a, b, sizeof(a)) 15 #define pb push_back 16 #define mp make_pair 17 const LL INF = 0x7fffffff; 18 const int inf = 0x3f3f3f3f; 19 const int mod = 1e9+7; 20 const int maxn = 100000+10; 21 const int maxm = 10+10; 22 //输入挂 23 const int MAXBUF = 10000; 24 char buf[MAXBUF], *ps = buf, *pe = buf+1; 25 inline void rnext() 26 { 27 if(++ps == pe) 28 pe = (ps = buf)+fread(buf,sizeof(char),sizeof(buf)/sizeof(char),stdin); 29 } 30 template <class T> 31 inline bool in(T &ans) 32 { 33 ans = 0; 34 T f = 1; 35 if(ps == pe) return false;//EOF 36 do{ 37 rnext(); 38 if('-' == *ps) f = -1; 39 }while(!isdigit(*ps) && ps != pe); 40 if(ps == pe) return false;//EOF 41 do 42 { 43 ans = (ans<<1)+(ans<<3)+*ps-48; 44 rnext(); 45 }while(isdigit(*ps) && ps != pe); 46 ans *= f; 47 return true; 48 } 49 const int MAXOUT=10000; 50 char bufout[MAXOUT], outtmp[50],*pout = bufout, *pend = bufout+MAXOUT; 51 inline void write() 52 { 53 fwrite(bufout,sizeof(char),pout-bufout,stdout); 54 pout = bufout; 55 } 56 inline void out_char(char c){ *(pout++) = c;if(pout == pend) write();} 57 inline void out_str(char *s) 58 { 59 while(*s) 60 { 61 *(pout++) = *(s++); 62 if(pout == pend) write(); 63 } 64 } 65 template <class T> 66 inline void out_int(T x) { 67 if(!x) 68 { 69 out_char('0'); 70 return; 71 } 72 if(x < 0) x = -x,out_char('-'); 73 int len = 0; 74 while(x) 75 { 76 outtmp[len++] = x%10+48; 77 x /= 10; 78 } 79 outtmp[len] = 0; 80 for(int i = 0, j = len-1; i < j; i++,j--) swap(outtmp[i],outtmp[j]); 81 out_str(outtmp); 82 } 83 //end 84 int n, m, uN, vN; 85 int g[maxn][maxm]; 86 int linker[maxm][maxn]; 87 bool used[maxm]; 88 int num[maxm]; 89 bool dfs(int u) 90 { 91 for(int v = 0;v<vN;v++) 92 if(g[u][v] && !used[v]){ 93 used[v] = true; 94 if(linker[v][0]<num[v]){ 95 linker[v][++linker[v][0]] = u; 96 return true; 97 } 98 for(int i = 1;i<=num[0];i++) 99 if(dfs(linker[v][i])){ 100 linker[v][i] = u; 101 return true; 102 } 103 } 104 return false; 105 } 106 int hungary() 107 { 108 int res = 0; 109 for(int i = 0;i<vN;i++){ 110 linker[i][0] = 0; 111 } 112 for(int u = 0;u<uN;u++){ 113 ms(used, false); 114 if(dfs(u)) res++; 115 else return res; 116 } 117 return res; 118 } 119 void init() { 120 ms(g, 0); 121 } 122 void solve() { 123 int x; 124 for(int i = 0;i<n;i++){ 125 for(int j = 0;j<m;j++){ 126 in(x); 127 if(x==1){ 128 g[i][j] = 1; 129 } 130 else{ 131 g[i][j] = 0; 132 } 133 } 134 } 135 for(int i = 0;i<m;i++) 136 in(num[i]); 137 vN = m, uN = n; 138 int ans = hungary(); 139 if(ans==n){ 140 out_str("YES");out_char('\n'); 141 } 142 else{ 143 out_str("NO");out_char('\n'); 144 } 145 } 146 int main() { 147 #ifdef LOCAL 148 freopen("input.txt", "r", stdin); 149 // freopen("output.txt", "w", stdout); 150 #endif 151 while(in(n)&&in(m)){ 152 init(); 153 solve(); 154 } 155 write(); 156 return 0; 157 }