Escape
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 13028 Accepted Submission(s): 3264
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
裸?毕竟是多校的题,能裸吗。。。有点bishu。。
看到n和m的范围相差这么大,没点想法吗
把选择相同的人缩为一个点,顺便统计个数,就是裸了,
注意用c++交。。。。
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <bitset> #define rap(i, a, n) for(int i=a; i<=n; i++) #define rep(i, a, n) for(int i=a; i<n; i++) #define lap(i, a, n) for(int i=n; i>=a; i--) #define lep(i, a, n) for(int i=n; i>a; i--) #define rd(a) scanf("%d", &a) #define rlld(a) scanf("%lld", &a) #define rc(a) scanf("%c", &a) #define rs(a) scanf("%s", a) #define pd(a) printf("%d ", a); #define plld(a) printf("%lld ", a); #define pc(a) printf("%c ", a); #define ps(a) printf("%s ", a); #define MOD 2018 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 1e6 + 10, INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff; int n, m, s, t, cnt; int head[maxn], d[maxn], cur[maxn]; map<string, int> se; vector<string> g; string str; struct node { int u, v, c, next; }Node[maxn]; void add_(int u, int v, int c) { Node[cnt].u = u; Node[cnt].v = v; Node[cnt].c = c; Node[cnt].next = head[u]; head[u] = cnt++; } void add(int u, int v, int c) { add_(u, v, c); add_(v, u, 0); } bool bfs() { queue<int> Q; mem(d, 0); Q.push(s); d[s] = 1; while(!Q.empty()) { int u = Q.front(); Q.pop(); for(int i = head[u]; i != -1; i = Node[i].next) { node e = Node[i]; if(!d[e.v] && e.c > 0) { d[e.v] = d[u] + 1; Q.push(e.v); if(e.v == t) return 1; } } } return d[t] != 0; } int dfs(int u, int cap) { int ret = 0; if(u == t || cap == 0) return cap; for(int &i = cur[u]; i != -1; i = Node[i].next) { node e = Node[i]; if(d[e.v] == d[u] + 1 && e.c > 0) { int V = dfs(e.v, min(e.c, cap)); Node[i].c -= V; Node[i ^ 1].c += V; ret += V; cap -= V; if(cap == 0) break; } } if(cap > 0) d[u] = -1; return ret; } int Dinic() { int ret = 0; while(bfs()) { memcpy(cur, head, sizeof(head)); ret += dfs(s, INF); } return ret; } void init() { mem(head, -1); se.clear(); g.clear(); cnt = 0; } int main() { while(~scanf("%d%d", &n, &m)) { getchar(); init(); int ans = 0; rap(i, 1, n) { getline(cin, str); if(!se[str]) { ++ans; g.push_back(str); int len = str.size(); for(int j = 0; j < len; j++) { if(str[j] == '1') add(10 + ans, (j + 2) / 2, INF); } } se[str]++; } s = 0, t = 10 + ans + 1; int max_flow = 0; for(int i = 0; i < g.size(); i++) { add(s, 10 + i + 1, se[g[i]]); max_flow += se[g[i]]; } int tmp; for(int i = 1; i <= m; i++) { rd(tmp); add(i, t, tmp); } if(max_flow == Dinic()) { printf("YES "); } else printf("NO "); } return 0; }