Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef pair<int,int> ii;
- const int me = (1024*1023), mv = 1024;
- int adj[mv], parent[mv];
- bool marked[mv];
- int ant[me], cap[me], to[me];
- vector<int> ans[mv];
- int ptr = 0, max_flow = 0, s = 0, t = 1021;
- queue<ii> q;
- void add_edge(int u, int v, int w){
- ant[ptr] = adj[u];
- to[ptr] = v;
- cap[ptr] = w;
- adj[u] = ptr++;
- swap(u,v);
- ant[ptr] = adj[u];
- to[ptr] = v;
- cap[ptr] = 0;
- adj[u] = ptr++;
- }
- void init(){
- memset(adj,-1,sizeof(adj));
- for(int i = 0;i<mv;i++) ans[i].clear();
- max_flow = 0;
- ptr = 0;
- }
- bool bfs(){
- memset(marked,false, sizeof(marked));
- memset(parent,-1,sizeof(parent));
- int flow = 0, u = 0, w = 0, e = 0, v =0 , min_flow = 0;
- q.push({s,INT_MAX});
- marked[s] = true;
- while(!q.empty() && !marked[t]){
- u = q.front().first;
- w = q.front().second;
- q.pop();
- e = adj[u];
- while(~e){
- v = to[e];
- if(!marked[v] && cap[e] > 0){
- parent[v] = e;
- min_flow = min(cap[e],w);
- marked[v] = true;
- q.push({v,min_flow});
- if(v==t) {
- flow = min_flow;
- }
- }
- e = ant[e];
- }
- }
- while(!q.empty()) q.pop();
- if(marked[t]){
- e = parent[t];
- while(~e){
- cap[e] -= flow;
- cap[e^1] += flow;
- if(to[e^1] >= 1 && to[e^1] <= 20){
- int i = to[e]-1001;
- //cout << i << " " << to[e^1] << endl;
- ans[i].push_back(to[e^1]);
- }
- e = parent[to[e^1]];
- }
- max_flow += flow;
- }
- return marked[t];
- }
- int main(){
- ios_base::sync_with_stdio(false); cin.tie(0);
- int nk, np, n, v, capProb, totalProb = 0;
- while(cin >> nk >> np && nk != 0 && np != 0){
- init();
- totalProb = 0;
- for(int i = 1;i<=nk;i++){
- cin >> capProb;
- totalProb += capProb;
- add_edge(i+1000,t,capProb);
- }
- for(int i = 1;i<=np;i++){
- cin >> n;
- add_edge(s,i,1);
- while(n--){
- cin >> v;
- add_edge(i,v+1000,INT_MAX);
- }
- }
- while(bfs());
- if(max_flow == totalProb){
- cout << "1" << endl;
- for(int i = 0;i<nk;i++){
- for(int j = 0;j<ans[i].size();j++) cout << ans[i][j] << " ";
- cout << endl;
- }
- }
- else cout << "0" << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement