knightL

Coloring

Oct 17th, 2025
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <map>
  5. using namespace std;
  6.  
  7. typedef long long ll;
  8.  
  9. const int N = 10;
  10. const int M = 5;
  11. const int START = 3;
  12.  
  13. int maxSize;
  14.  
  15. struct Component
  16. {
  17.     int p[N];
  18.     int sz[N];
  19. };
  20.  
  21. struct State
  22. {
  23.     Component c[M];
  24.     int adj[N][N];
  25. };
  26.  
  27. void init_component(Component* r)
  28. {
  29.     for(int i=0;i<N;i++)
  30.     {
  31.         r->p[i] = i;
  32.         r->sz[i] = 1;
  33.     }
  34. }
  35.  
  36. void init_state(State* state)
  37. {
  38.     for(int i=0;i<M;i++)
  39.         init_component(&state->c[i]);
  40.     for(int i=0;i<N;i++)
  41.         for(int j=0;j<N;j++)
  42.             state->adj[i][j] = -1;
  43. }
  44.  
  45. void component_merge(Component* comp, int a, int b)
  46. {
  47.     int ca = comp->p[a];
  48.     int cb = comp->p[b];
  49.  
  50.     if( ca != cb )
  51.     {
  52.         for(int i=0;i<N;i++)
  53.             if ( comp->p[i] == cb )
  54.             {
  55.                 comp->sz[cb]--;
  56.                 comp->p[i] = ca;
  57.                 comp->sz[ca]++;
  58.             }
  59.     }
  60.  
  61. }
  62.  
  63. int stateCount;
  64.  
  65. bool solve(State state, int colorTop)
  66. {
  67.     stateCount++;
  68.     for(int i=0;i<M;i++)
  69.         for(int j=0;j<N;j++)
  70.             if ( state.c[i].sz[j] > maxSize )
  71.                 return false;
  72.  
  73.     bool changes = true;
  74.     while(changes)
  75.     {
  76.         changes = false;
  77.         for(int i=0;i<N;i++)
  78.             for(int j=i+1;j<N;j++)
  79.                 for(int k=0;k<colorTop;k++)
  80.                     if(state.c[k].p[i] == state.c[k].p[j] && state.adj[i][j] == -1)
  81.                     {
  82.                         state.adj[j][i] = state.adj[i][j] = k;
  83.                         changes = true;
  84.                     }
  85.     }
  86.  
  87.     int best = -1;
  88.     int bestI = -1, bestJ = -1;
  89.  
  90.     for(int i=0;i<N;i++)
  91.         for(int j=i+1;j<N;j++)
  92.             if(state.adj[i][j] == -1)
  93.             {
  94.                 int ways = 0;
  95.                 for(int k=0;k<colorTop;k++)
  96.                 {
  97.                     int iid = state.c[k].p[i];
  98.                     int jid = state.c[k].p[j];
  99.  
  100.                     if(state.c[k].sz[iid] + state.c[k].sz[jid] <= maxSize)
  101.                         ways++;
  102.                 }
  103.                 if( best == -1 || ways < best )
  104.                 {
  105.                     best = ways;
  106.                     bestI = i;
  107.                     bestJ = j;
  108.                 }
  109.             }
  110.  
  111.     if ( best == -1 )
  112.     {
  113.         for(int i=0;i<N;i++)
  114.         {
  115.             for(int j=0;j<N;j++)
  116.                 printf("%d ", state.adj[i][j]);
  117.             puts("");
  118.         }
  119.  
  120.         for(int k = 0 ; k < M; k++)
  121.         {
  122.             int ecnt = 0;
  123.             for(int i=0;i<N;i++)
  124.                 for(int j=i+1;j<N;j++)
  125.                     if(state.adj[i][j] == k)
  126.                         ecnt++;
  127.             printf("%d %d\n", N, ecnt);
  128.             for(int i=0;i<N;i++)
  129.                 for(int j=i+1;j<N;j++)
  130.                     if(state.adj[i][j] == k)
  131.                         printf("%d %d\n", i+1, j+1);
  132.             puts("====");
  133.         }
  134.  
  135.         return true;
  136.     }
  137.  
  138.     for(int k=0;k<colorTop;k++)
  139.     {
  140.         State s2 = state;
  141.         int i, j;
  142.         i = bestI;
  143.         j = bestJ;
  144.         s2.adj[i][j] = s2.adj[j][i] = k;
  145.         component_merge(&s2.c[k], i, j);
  146.  
  147.         if( solve( s2, ( k == colorTop-1 && colorTop < M ) ? colorTop+1 : colorTop ) )
  148.             return true;
  149.  
  150.         s2.adj[i][j] = s2.adj[j][i] = -1;
  151.     }
  152.  
  153.     return false;
  154. }
  155.  
  156. int main()
  157. {
  158.     State state;
  159.     init_state(&state);
  160.  
  161.     state.adj[0][1] = state.adj[0][2] = 0;
  162.     component_merge(&state.c[0], 0, 1);
  163.     component_merge(&state.c[0], 0, 2);
  164.  
  165.     for(maxSize = START; ; maxSize++)
  166.     {
  167.         stateCount = 0;
  168.         printf("maxSize = %d\n", maxSize);
  169.         if ( solve(state, 2) )
  170.         {
  171.             printf("size = %d\n", maxSize);
  172.             break;
  173.         }
  174.         printf("State Count = %d\n", stateCount);
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment