Advertisement
RaFiN_

max flow solution print

Apr 2nd, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.86 KB | None | 0 0
  1. ///#include <stdio.h>
  2. ///#include <iostream>
  3. ///#include <cstring>
  4. ///#include <cmath>
  5. ///#include <algorithm>
  6. ///#include <string>
  7. ///#include <vector>
  8. ///#include <map>
  9. ///#include <set>
  10. ///#include <queue>
  11. ///#include <cstdlib>
  12. ///#include <limits>
  13. ///#include <iostream>
  14. ///#include <sstream>
  15. ///#include <unordered_set>
  16. ///#include <unordered_map>
  17. ///#include <random>
  18. #include <bits/stdc++.h>
  19. ///#include <bits/stdtr1c++.h>
  20. ///#include <ext/pb_ds/assoc_container.hpp>
  21. ///#include <ext/pb_ds/tree_policy.hpp>
  22. using namespace std;
  23. ///using namespace __gnu_pbds;
  24.  
  25. #define              MAX             500005
  26. #define              EPS             1e-9
  27. #define              ull             unsigned long long
  28. #define              ll              long long
  29. #define              inf             INT_MAX
  30. #define              pi              acos(-1.0)
  31. #define              vi              vector<int>
  32. #define              vl              vector<long long>
  33. #define              pii             pair<int,int>
  34. #define              pll             pair<long long,long long>
  35. #define              mp              make_pair
  36. #define              mem(a, v)       memset(a, v, sizeof (a))
  37. #define              mod             1000000007
  38. #define              all(a)          a.begin(),a.end()
  39. #define              rall(a)         a.rbegin(),a.rend()
  40. #define              ff              first
  41. #define              ss              second
  42. #define              arsort(ar,n)    sort(ar,ar+n);
  43. #define              vsort(v)        sort(v.begin(),v.end())
  44. #define              vrev(v)         reverse(v.begin(),v.end())
  45. #define              arrev(ar,n)     reverse(ar,ar+n)
  46. #define              pb              push_back
  47. #define              popb            pop_back
  48. #define              booster()       ios_base::sync_with_stdio(0); cin.tie(0);
  49. #define              read(f)         freopen(f, "r", stdin)
  50. #define              scani(x)        scanf("%d",&x)
  51. #define              scanl(x)        scanf("%I64d",&x)
  52. #define              scand(x)        scanf("%lf",&x)
  53. #define              scans(x)        scanf("%s",x)
  54. #define              OUT(x)          printf("%d\n",x)
  55. #define              OUTS(x)         printf("%d ",x)
  56. #define              min3(a,b,c)     min(a,min(b,c))
  57. #define              max3(a,b,c)     max(a,max(b,c))
  58. #define              min4(a,b,c,d)   min(min(a,b),min(c,d))
  59. #define              max4(a,b,c,d)   max(max(a,b),max(c,d))
  60. #define              max5(a,b,c,d,e) max(max3(a,b,c),max(d,e))
  61. #define              min5(a,b,c,d,e) min(min3(a,b,c),min(d,e))
  62.  
  63. #define              bitCheck(a,k)     ((bool)(a&(1<<(k))))
  64. #define              bitOff(a,k)       (a&(~(1<<(k))))
  65. #define              bitOn(a,k)        (a|(1<<(k)))
  66. #define              bitFlip(a,k)      (a^(1<<(k)))
  67.  
  68. #define              POPCOUNT           __builtin_popcount
  69. #define              POPCOUNTLL         __builtin_popcountll
  70. #define              RIGHTMOST          __builtin_ctzll
  71. #define              LEFTMOST(x)        (63-__builtin_clzll((x)))
  72.  
  73.  
  74.  
  75. #define scani2(a,b) scani(a) , scani(b)
  76. #define scani3(a,b,c) scani(a), scani(b), scani(c)
  77. #define scani4(a,b,c,d) scani(a), scani(b), scani(c), scani(d)
  78. #define scani5(a,b,c,d,e) scani(a), scani(b), scani(c), scani(d) , scani(e)
  79. #define rnd mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  80.  
  81.  
  82.  
  83. /*********************Ordered Multiset*************************/
  84.  
  85.  
  86. /*** Needs C++11 or C++14 ***/
  87. /// Treap supporting duplicating values in set
  88. /// Maximum value of treap * ADD must fit in long long
  89.  
  90. /*
  91.  
  92. struct Treap{ /// hash = 96814
  93.     int len;
  94.     const int ADD = 1000010;
  95.     const int MAXVAL = 1000000010;
  96.     tr1::unordered_map <long long, int> mp; /// Change to int if only int in treap
  97.     tree<long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update> T;
  98.     Treap(){
  99.         len = 0;
  100.         T.clear(), mp.clear();
  101.     }
  102.     inline void clear(){
  103.         len = 0;
  104.         T.clear(), mp.clear();
  105.     }
  106.     inline void insert(long long x){
  107.         len++, x += MAXVAL;
  108.         int c = mp[x]++;
  109.         T.insert((x * ADD) + c);
  110.     }
  111.     inline void erase(long long x){
  112.         x += MAXVAL;
  113.         int c = mp[x];
  114.         if (c){
  115.             c--, mp[x]--, len--;
  116.             T.erase((x * ADD) + c);
  117.         }
  118.     }
  119.     /// 1-based index, returns the K'th element in the treap, -1 if none exists
  120.     inline long long kth(int k){
  121.         if (k < 1 || k > len) return -1;
  122.         auto it = T.find_by_order(--k);
  123.         return ((*it) / ADD) - MAXVAL;
  124.     }
  125.     /// Count of value < x in treap
  126.     inline int count(long long x){
  127.         x += MAXVAL;
  128.         int c = mp[--x];
  129.         return (T.order_of_key((x * ADD) + c));
  130.     }
  131.     /// Number of elements in treap
  132.     inline int size(){
  133.         return len;
  134.     }
  135. };
  136.  
  137.  
  138.  
  139. typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;///*X.find_by_order();X.order_of_key();
  140. template <class T> inline bool isLeap(T y) {return (y % 400 == 0) || (y % 100 ? y % 4 == 0 : false); }*/
  141. template< class T > inline T gcd(T a, T b) { return (b) == 0 ? (a) : gcd((b), ((a) % (b))); }
  142. template< class T > inline T lcm(T a, T b) { return ((a) / gcd((a), (b)) * (b)); }
  143. template <class T> inline T BigMod(T Base,T power,T M=mod){if(power==0)return 1;if(power&1)return ((Base%M)*(BigMod(Base,power-1,M)%M))%M;else{T y=BigMod(Base,power/2,M)%M;return (y*y)%M;}}
  144. template <class T> inline T ModInv (T A,T M = mod){return BigMod(A,M-2,M);}
  145.  
  146. int fx[] = {-1,+0,+1,+0,+1,+1,-1,-1,+0};
  147. int fy[] = {+0,-1,+0,+1,+1,-1,+1,-1,+0};
  148. int day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  149.  
  150.  
  151. /*********************** let the play begin() *********************************/
  152.  
  153.  
  154. /// ********** FASTER one *******************///
  155. namespace mf
  156. {
  157.     const int MAXN = 10004;
  158.     struct edge {
  159.         int a, b, cap, flow ;
  160.         edge(int _a,int _b,int _c,int _d) {
  161.             a=_a,b=_b,cap=_c,flow=_d;
  162.         }
  163.     } ;
  164.  
  165.     int INF=1500000001;
  166.  
  167.     int n, s, t, d [ MAXN*2 ] , ptr [ MAXN*2 ] , q [ MAXN*2*10 ] ;
  168.     vector < edge > e ;
  169.     vector < int > g [ MAXN * 2 ] ;
  170.  
  171.     void addEdge ( int a, int b, int cap ,int cap2=0) {
  172.         edge e1 =edge( a, b, cap, 0 ) ; /// forward cap
  173.         edge e2 =edge( b, a, cap2 , 0 ) ; /// backward cap change here if needed
  174.         g [ a ] . push_back ( ( int ) e. size ( ) ) ;
  175.         e. push_back ( e1 ) ;
  176.         g [ b ] . push_back ( ( int ) e. size ( ) ) ;
  177.         e. push_back ( e2 ) ;
  178.     }
  179.  
  180.     bool bfs ( ) {
  181.         int qh = 0 , qt = 0 ;
  182.         q [ qt ++ ] = s ;
  183.         memset ( d, -1 , sizeof d ) ;
  184.         d [ s ] = 0 ;
  185.         while ( qh < qt && d [ t ] == - 1 ) {
  186.             int v = q [ qh ++ ] ;
  187.             for ( size_t i = 0 ; i < g [ v ] . size ( ) ; ++ i ) {
  188.                 int id = g [ v ] [ i ] ,
  189.                     to = e [ id ] . b ;
  190.                 if ( d [ to ] == - 1 && e [ id ] . flow < e [ id ] . cap ) {
  191.                     q [ qt ++ ] = to ;
  192.                     d [ to ] = d [ v ] + 1 ;
  193.                 }
  194.             }
  195.         }
  196.         return d [ t ] != -1;
  197.     }
  198.  
  199.     int dfs ( int v, int flow ) {
  200.         if ( ! flow )  return 0 ;
  201.         if ( v == t )  return flow ;
  202.         for ( ; ptr [ v ] < ( int ) g [ v ] . size ( ) ; ++ ptr [ v ] ) {
  203.             int id = g [ v ] [ ptr [ v ] ] ,
  204.                 to = e [ id ] . b ;
  205.             if ( d [ to ] != d [ v ] + 1 )  continue ;
  206.             int pushed = dfs ( to, min ( flow, e [ id ] . cap - e [ id ] . flow ) ) ;
  207.             if ( pushed ) {
  208.                 e [ id ] . flow += pushed ;
  209.                 e [ id ^ 1 ] . flow -= pushed ;
  210.                 return pushed ;
  211.             }
  212.         }
  213.         return 0 ;
  214.     }
  215.  
  216.     ll dinic ( ) {
  217.         ll flow = 0 ;
  218.         for ( ;; ) {
  219.             if ( !bfs () )  break ;
  220.             memset ( ptr, 0 , sizeof ptr ) ;
  221.             while ( int pushed = dfs ( s, INF ) ) {
  222.                 flow += (ll)pushed ;
  223.                 if(pushed == 0)break;
  224.             }
  225.         }
  226.         return flow ;
  227.     }
  228.  
  229.     void init(int src, int dest , int nodes){
  230.         e.clear();
  231.         for(int i = 0;i<=n+n;i++) g[i].clear();
  232.         n = nodes; s = src; t = dest;
  233.     }
  234. };
  235.  
  236. int prime[MAX],p,flag[MAX];
  237. void sieve(int n){
  238.     double z = sqrt(n);
  239.     for(int i = 4;i<=n;i+=2)flag[i] = 1;
  240.     for(int i = 3;i<=z;i+=2){
  241.         if(!flag[i]){
  242.             for(ll j = i*i;j<=n;j+=i+i)flag[j] = 1;
  243.         }
  244.     }
  245.     prime[p++] = 2;
  246.     for(int i = 3;i<=n;i+=2){
  247.         if(!flag[i])prime[p++] = i;
  248.     }
  249. }
  250.  
  251. int nn,arr[MAX];vi v[MAX],ans[MAX];int vis[MAX],cnt;
  252.  
  253. void dfs(int s){
  254.     if(vis[s])return ;
  255.     vis[s] = 1;
  256.     ans[cnt].pb(s);
  257.     for(auto it : v[s]){
  258.         if(!vis[it])dfs(it);
  259.     }
  260. }
  261.  
  262.  
  263.  
  264. int main()
  265. {
  266.     booster()
  267.     ///read("input.txt");
  268.  
  269.     sieve(100000);
  270.  
  271.     cin>>nn;
  272.     for(int i = 0;i<nn;i++){
  273.         cin>>arr[i];
  274.     }
  275.  
  276.     mf::init(0,nn+1,nn+3);
  277.  
  278.     for(int i = 0;i<nn;i++){
  279.         for(int j = i+1;j<nn;j++){
  280.             if(!flag[arr[i]+arr[j]]){
  281.                 if(arr[i]&1)mf::addEdge(i+1,j+1,1);
  282.                 else mf::addEdge(j+1,i+1,1);
  283.             }
  284.         }
  285.     }
  286.     int pnt = 0;
  287.     for(int i = 0;i<nn;i++){
  288.         if(arr[i]&1)mf::addEdge(0,i+1,2),pnt++;
  289.         else mf::addEdge(i+1,nn+1,2);
  290.     }
  291.     int x = mf::dinic();
  292.     ///cout<<x<<endl;
  293.     if(x!=nn||pnt!=nn-pnt)cout<<"Impossible";
  294.     else {
  295.         for(int i = 0;i<nn;i++){
  296.             int sz = mf::g[i+1].size();
  297.             for(int j = 0;j<sz;j++){
  298.                 int indx = mf::g[i+1][j];
  299.                 if(mf::e[indx].flow==1){
  300.                     int a = mf::e[indx].a;
  301.                     int b = mf::e[indx].b;
  302.                     v[a].pb(b);
  303.                     v[b].pb(a);
  304.                 }
  305.             }
  306.         }
  307.  
  308.         for(int i = 1;i<=nn;i++){
  309.             if(!vis[i]){
  310.                 cnt++;
  311.                 dfs(i);
  312.             }
  313.         }
  314.         cout<<cnt<<endl;
  315.         for(int i = 1;i<=cnt;i++){
  316.             cout<<ans[i].size()<<" ";
  317.             for(auto it : ans[i])cout<<it<<" ";
  318.             cout<<endl;
  319.         }
  320.     }
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.     return 0;
  328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement