Advertisement
Guest User

blabla

a guest
Mar 18th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef pair<int,int> ii;
  6.  
  7. const int me = (1024*1023), mv = 1024;
  8.  
  9. int adj[mv], parent[mv];
  10. bool marked[mv];
  11. int ant[me], cap[me], to[me];
  12.  
  13. vector<int> ans[mv];
  14.  
  15.  
  16. int ptr = 0, max_flow = 0, s = 0, t = 1021;
  17. queue<ii> q;
  18.  
  19. void add_edge(int u, int v, int w){
  20.     ant[ptr] = adj[u];
  21.     to[ptr] = v;
  22.     cap[ptr] = w;
  23.     adj[u] = ptr++;
  24.     swap(u,v);
  25.     ant[ptr] = adj[u];
  26.     to[ptr] = v;
  27.     cap[ptr] = 0;
  28.     adj[u] = ptr++;
  29. }
  30.  
  31. void init(){
  32.     memset(adj,-1,sizeof(adj));
  33.     for(int i = 0;i<mv;i++) ans[i].clear();
  34.     max_flow = 0;
  35.     ptr = 0;
  36. }
  37.  
  38. bool bfs(){
  39.     memset(marked,false, sizeof(marked));
  40.     memset(parent,-1,sizeof(parent));
  41.     int flow = 0, u = 0, w = 0, e = 0, v =0 , min_flow = 0;
  42.     q.push({s,INT_MAX});
  43.     marked[s] = true;
  44.  
  45.     while(!q.empty() && !marked[t]){
  46.         u = q.front().first;
  47.         w = q.front().second;
  48.         q.pop();
  49.         e = adj[u];
  50.         while(~e){
  51.             v = to[e];
  52.             if(!marked[v] && cap[e] > 0){
  53.                 parent[v] = e;
  54.                 min_flow = min(cap[e],w);
  55.                 marked[v] = true;
  56.                 q.push({v,min_flow});
  57.                 if(v==t) {
  58.                     flow = min_flow;
  59.                 }
  60.             }
  61.             e = ant[e];
  62.         }
  63.     }
  64.     while(!q.empty()) q.pop();
  65.    
  66.     if(marked[t]){
  67.         e = parent[t];
  68.  
  69.         while(~e){
  70.             cap[e] -= flow;
  71.             cap[e^1] += flow;
  72.  
  73.                 if(to[e^1] >= 1 && to[e^1] <= 20){
  74.  
  75.                 int i = to[e]-1001;
  76.                 //cout << i << " " << to[e^1] << endl;
  77.                 ans[i].push_back(to[e^1]);
  78.             }
  79.  
  80.             e = parent[to[e^1]];
  81.         }
  82.        
  83.         max_flow += flow;
  84.     }
  85.     return marked[t];
  86. }
  87.  
  88. int main(){
  89.     ios_base::sync_with_stdio(false); cin.tie(0);
  90.    
  91.     int nk, np, n, v, capProb, totalProb = 0;
  92.  
  93.     while(cin >> nk >> np && nk != 0 && np != 0){
  94.         init();
  95.         totalProb = 0;
  96.         for(int i = 1;i<=nk;i++){
  97.             cin >> capProb;
  98.             totalProb += capProb;
  99.             add_edge(i+1000,t,capProb);
  100.         }
  101.  
  102.         for(int i = 1;i<=np;i++){
  103.             cin >> n;
  104.             add_edge(s,i,1);
  105.             while(n--){
  106.                 cin >> v;
  107.                 add_edge(i,v+1000,INT_MAX);
  108.             }
  109.         }
  110.  
  111.         while(bfs());
  112.         if(max_flow == totalProb){
  113.             cout << "1" << endl;
  114.             for(int i = 0;i<nk;i++){
  115.                 for(int j = 0;j<ans[i].size();j++) cout << ans[i][j] << " ";
  116.                 cout << endl;
  117.             }
  118.         }
  119.         else cout << "0" << endl;
  120.  
  121.        
  122.     }
  123.    
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement