runnig

[sotong] R3 4-color graph coloring

Mar 29th, 2013
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. /* 4 color graph coloring */
  2. /*
  3. output:
  4. 108
  5. 96
  6. 264
  7.  
  8. input:
  9. 3
  10. 4
  11. 0 0 0 1
  12. 0 0 0 1
  13. 0 0 0 1
  14. 1 1 1 0
  15. 5
  16. 0 1 1 1 0
  17. 1 0 0 1 1
  18. 1 0 0 1 0
  19. 1 1 1 0 1
  20. 0 1 0 1 0
  21. 7
  22. 0 1 0 0 1 0 1
  23. 1 0 1 0 1 0 0
  24. 0 1 0 1 1 0 0
  25. 0 0 1 0 1 1 0
  26. 1 1 1 1 0 1 1
  27. 0 0 0 1 1 0 1
  28. 1 0 0 0 1 1 0
  29. */
  30. #include "stdafx.h"
  31.  
  32. #include <iostream>
  33. #include <fstream>
  34. #include <vector>
  35. #include <algorithm>
  36. #include <functional>
  37. #include <queue>
  38. #include <map>
  39. #include <set>
  40. #include <deque>
  41. #include <assert.h>
  42.  
  43. using namespace std;
  44.  
  45. class algorithm
  46. {
  47. public:
  48.     void solution(char *input, char *output);
  49. };
  50.  
  51. typedef std::set<int> set_t;
  52. typedef std::map<int, set_t> graph_t;
  53. typedef std::map<int, int> colormap_t;
  54.  
  55. graph_t G;
  56. set_t V; // visited
  57. colormap_t colormap;
  58.  
  59. typedef unsigned char color_t;
  60. const color_t COLOR1 = (0x1 << 0);
  61. const color_t COLOR2 = (0x1 << 1);
  62. const color_t COLOR3 = (0x1 << 2);
  63. const color_t COLOR4 = (0x1 << 3);
  64. const color_t ALL_COLORS = (COLOR1 | COLOR2 | COLOR3 | COLOR4);
  65.  
  66. int solve(int begin_vertex, int sofar = 1)
  67. {
  68.     set_t::iterator Vit = V.find(begin_vertex);
  69.  
  70.     if(V.end() != Vit) { return 0; }
  71.  
  72.     graph_t::iterator g = G.find(begin_vertex);
  73.     if( g == G.end() || g->second.empty() ) { return 0; }
  74.  
  75.     V.insert(begin_vertex);
  76.  
  77.     set_t & adj_set = g->second;
  78.  
  79.     color_t adj_colors = 0;
  80.  
  81.     std::vector<int> tovisit;
  82.     set_t::iterator adj_it = adj_set.begin();
  83.     for(; adj_it != adj_set.end(); ++adj_it)
  84.     {
  85.         const int adj_vertex = *adj_it;
  86.         if(V.find(adj_vertex) != V.end())
  87.         {
  88.             assert(colormap.find(adj_vertex) != colormap.end());
  89.             adj_colors |= colormap[adj_vertex];
  90.         }
  91.         else
  92.         {
  93.             tovisit.push_back(adj_vertex);
  94.         }
  95.     }
  96.  
  97.     const size_t num_tovisit = tovisit.size();
  98.  
  99.     int ret = 0;
  100.     if(num_tovisit == 0)
  101.     {
  102.         ret += !(adj_colors & COLOR1);
  103.         ret += !(adj_colors & COLOR2);
  104.         ret += !(adj_colors & COLOR3);
  105.         ret += !(adj_colors & COLOR4);
  106.     }
  107.     else
  108.     {
  109.         for(int color = 0; color < 4; ++color)
  110.         {
  111.             const int colorbit = 0x1 << color;
  112.  
  113.             // used color
  114.             if(adj_colors & colorbit) { continue; }
  115.  
  116.             colormap[begin_vertex] = colorbit;
  117.  
  118.             for(size_t v = 0; v < num_tovisit; ++v)
  119.             {
  120.                 ret += solve(tovisit[v], 1);
  121.             }
  122.         }
  123.     }
  124.     Vit = V.find(begin_vertex);
  125.     assert(Vit != V.end());
  126.     V.erase(Vit);
  127.     return ret;
  128. }
  129.  
  130. void algorithm::solution(char *input, char *output)
  131. {
  132.  
  133.     ifstream infile(input);
  134.     ofstream outfile;
  135.     outfile.open(output);
  136.  
  137.     int T;
  138.     infile >> T;
  139.  
  140.     for (int t = 1; t <= T; t++)
  141.     {
  142.         size_t N;
  143.         infile >> N;
  144.  
  145.         G.clear();
  146.  
  147.         for(size_t r = 0; r < N; ++r)
  148.         {
  149.             for(size_t c = 0; c < N; ++c)
  150.             {
  151.                 int v;
  152.                 infile >> v;
  153.                 if(v == 1)
  154.                 {
  155.                     G[r].insert(c);
  156.                     G[c].insert(r);
  157.                 }
  158.             }
  159.         }
  160.         if(!G.empty())
  161.         {
  162.             int begin = G.begin()->first;
  163.             int S = solve(begin);
  164.             outfile << S << '\n';
  165.         }
  166.         else
  167.         {
  168.             outfile << 0 << '\n';
  169.         }
  170.  
  171.     }
  172. }
  173. #if 1
  174. int main(int argc, char * argv[])
  175. {
  176.     algorithm().solution("input.txt", "output.txt");
  177.     return 0;
  178. }
  179. #endif
Advertisement
Add Comment
Please, Sign In to add comment