Codeforces Round #290 (Div. 2) E. Fox And Dinner network stream

Source: Internet
Author: User

Codeforces Round #290 (Div. 2) E. Fox And Dinner network stream
E. Fox And Dinner time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

Fox Ciel is maid in a party in Prime Kingdom. There areNFoxes there (include Fox Ciel). The I-th fox isAIYears old.

They will have dinner around some round tables. You want to distribute foxes such that:

  1. Each fox is sitting at some table.
  2. Each table has at least 3 foxes sitting around it.
  3. The sum of ages of any two adjacent foxes around each table shocould be a prime number.

    IfKFoxesF1,F2 ,...,FKAre sitting around table in clockwise order, then for 1 limit ≤ limitILimit ≤ limitKSkip-commit 1:FIAndFITables + tables 1 are adjacent, andF1 andFKAre also adjacent.

    If it is possible to distribute the foxes in the desired manner, find out a way to do that.

    Input

    The first line contains single integerN(3 cores ≤ CoresNLimit ≤0000200): the number of foxes in this party.

    The second line containsNIntegersAI(2 cores ≤ CoresAILimit ≤ limit 104 ).

    Output

    If it is impossible to do this, output Impossible.

    Otherwise, in the first line output an integerM(): The number of tables.

    Then outputMLines, each line shocould start with an integerK-=-The number of foxes around that table, and thenKNumbers-indices of fox sitting around that table in clockwise order.

    If there are several possible arrangements, output any of them.

    Sample test (s) input
    43 4 8 9
    Output
    14 1 2 4 3
    Input
    52 2 2 2 2
    Output
    Impossible
    Input
    122 3 4 5 6 7 8 9 10 11 12 13
    Output
    112 1 2 3 6 5 12 9 8 7 10 11 4
    Input
    242 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
    Output
    36 1 2 3 6 5 410 7 8 9 12 15 14 13 16 11 108 17 18 23 22 19 20 21 24
    Note

    In example 1, they can sit around one table, their ages are: 3-8-9-4, adjacent sums are: 11, 17, 13 and 7, all those integers are primes.

    In example 2, it is not possible: the sum of 2 + 2 = 4 is not a prime number.

    The question is to divide n numbers into several tables. The sum of numbers on a table is the prime number, and the numbers are connected at the beginning and end.

    According to the meaning of the question, if each number is greater than or equal to 2, then the prime number indicates that one of the two numbers is an odd number and the other is an even number. Then, if the number of the odd number is equal, the odd number is divided into two heaps, create a bipartite graph. connect all the odd numbers at the start point. The weight value is 2. connect all the even numbers at the end point. The weight value is 2. The sum of the odd and even numbers is prime, connect an edge of 1 and find the maximum stream. If the final result is an edge with a value of 1 starting from the starting point, it means that only two people can be connected at most, each singularity even has two sides connected. Use dfs to find this path. If there are more than three connection points, output the answer! Here we use the EK algorithm bfs to find the maximum stream!

     

    # Define INF9000000000 # define EPS (double) 1e-9 # define mod1_00007 # define PI3.14159265358979 //******************************* **************************************** * ******/# endif # define N 205 # define M 100005 # define maxn 205 # define MOD merge limit 000007int n, pri [N], ansNum; bool vis [N], prime [M]; vector
       
        
    Ans [N]; struct Edge {int from, to, cap, flow; Edge (int u, int v, int c, int f): from (u ), to (v), cap (c), flow (f) {}}; struct EdmondsKarp {int n, m; vector
        
         
    Edges; // returns the double vector of the edge storage.
         
          
    G [maxn]; // an adjacent table, in the figure int a [maxn]; // an improved int p [maxn] From the start point to the I. // void init (int n) {FI (n) G [I]. clear (); edges. clear ();} void AddEdge (int from, int to, int cap) {edges. push_back (Edge (from, to, cap, 0); edges. push_back (Edge (to, from, 0, 0); // reverse m = edges. size (); G [from]. push_back (m-2); G [to]. push_back m-1);} int Maxflow (int s, int t) {int flow = 0; for (;) {memset (a, 0, sizeof (a); queue
          
           
    Q; Q. push (s); a [s] = INF; while (! Q. empty () {int x = Q. front (); Q. pop (); FI (G [x]. size () {Edge & e = edges [G [x] [I]; if (! A [e. to] & e. cap> e. flow) {p [e. to] = G [x] [I]; a [e. to] = min (a [x], e. cap-e. flow); Q. push (e. to) ;}} if (a [t]) break;} if (! A [t]) break; for (int u = t; u! = S; u = edges [p [u]. from) {edges [p [u]. flow + = a [t]; edges [p [u] ^ 1]. flow-= a [t];} flow + = a [t];} return flow ;}}; EdmondsKarp Ek; void InitPrime () {memset (prime, true, sizeof (prime); prime [0] = prime [1] = false; for (int I = 2; I
           
            
    Maximum stream using the Dinic Algorithm
            
    # Define INF9000000000 # define EPS (double) 1e-9 # define mod1_00007 # define PI3.14159265358979 //******************************* **************************************** * ******/# endif # define N 205 # define M 100005 # define maxn 205 # define MOD merge limit 000007int n, pri [N], ansNum; bool vis [N], prime [M]; vector
             
              
    Ans [N]; struct Edge {int from, to, cap, flow; Edge (int u, int v, int c, int f): from (u ), to (v), cap (c), flow (f) {}}; struct EdmondsKarp {int n, m; vector
              
               
    Edges; // returns the double vector of the edge storage.
               
                
    G [maxn]; // an adjacent table, in the figure int a [maxn]; // an improved int p [maxn] From the start point to the I. // void init (int n) {FI (n) G [I]. clear (); edges. clear ();} void AddEdge (int from, int to, int cap) {edges. push_back (Edge (from, to, cap, 0); edges. push_back (Edge (to, from, 0, 0); // reverse m = edges. size (); G [from]. push_back (m-2); G [to]. push_back m-1);} int Maxflow (int s, int t) {int flow = 0; for (;) {memset (a, 0, sizeof (a); queue
                
                 
    Q; Q. push (s); a [s] = INF; while (! Q. empty () {int x = Q. front (); Q. pop (); FI (G [x]. size () {Edge & e = edges [G [x] [I]; if (! A [e. to] & e. cap> e. flow) {p [e. to] = G [x] [I]; a [e. to] = min (a [x], e. cap-e. flow); Q. push (e. to) ;}} if (a [t]) break;} if (! A [t]) break; for (int u = t; u! = S; u = edges [p [u]. from) {edges [p [u]. flow + = a [t]; edges [p [u] ^ 1]. flow-= a [t];} flow + = a [t];} return flow ;}}; struct Dinic {int n, m, s, t; vector
                 
                  
    Edges; // returns the double vector of the edge storage.
                  
                    G [maxn]; // an adjacent table, in the figure bool vis [maxn]; // use int d [maxn] For BFS; // The distance from the start point to I: int cur [maxn]; // Current Arc subscript void init (int n) {FI (n) G [I]. clear (); edges. clear ();} void AddEdge (int from, int to, int cap) {edges. push_back (Edge (from, to, cap, 0); edges. push_back (Edge (to, from, 0, 0); // reverse m = edges. size (); G [from]. push_back (m-2); G [to]. push_back m-1);} bool BFS () {memset (vis, 0, sizeof (vis); queue
                   
                     Q; Q. push (s); d [s] = 0; vis [s] = 1; while (! Q. empty () {int x = Q. front (); Q. pop (); for (int I = 0; I
                    
                      E. flow) {vis [e. to] = 1; d [e. to] = d [x] + 1; Q. push (e. to) ;}}} return vis [t];} int DFS (int x, int a) {if (x = t | a = 0) return; int flow = 0, f; for (int I = cur [x]; I
                     
                       0) {e. flow + = f; edges [G [x] [I] ^ 1]. flow-= f; flow + = f; a-= f; if (a = 0) break;} return flow;} int Maxflow (int s, int t) {this-> s = s; this-> t = t; int flow = 0; while (BFS () {memset (cur, 0, sizeof (cur )); flow + = DFS (s, INF);} return flow ;}}; // EdmondsKarp Ek; Dinic Ek; void InitPrime () {memset (prime, true, sizeof (prime); prime [0] = prime [1] = false; for (int I = 2; I
                      
                       

     

     

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.