Advertisement
Pabon_SEC

Graph Coloring

May 4th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. bool graph[110][110];
  6.  
  7. bool color[110],ans[110];
  8.  
  9. int node,edge,curr;
  10.  
  11. bool yes(int n)
  12. {
  13.     for(int i=1; i<=node; i++)
  14.     {
  15.         if(graph[n][i] && color[i])
  16.         {
  17.             return false;
  18.         }
  19.     }
  20.  
  21.     return true;
  22. }
  23.  
  24. void dfs(int n,int mx)
  25. {
  26.     if(n==node+1)
  27.     {
  28.         if(curr<mx)
  29.         {
  30.             curr = mx;
  31.  
  32.             memcpy(ans,color,sizeof(color));
  33.         }
  34.  
  35.         return;
  36.     }
  37.  
  38.     if(yes(n))
  39.     {
  40.         color[n] = true;
  41.  
  42.         dfs(n+1,mx+1);
  43.  
  44.         color[n] = false;
  45.     }
  46.  
  47.     dfs(n+1,mx);
  48. }
  49.  
  50. int main()
  51. {
  52.     int test,i,u,v;
  53.  
  54.     scanf("%d",&test);
  55.  
  56.     while(test--)
  57.     {
  58.         scanf("%d%d",&node,&edge);
  59.  
  60.         for(i=1; i<=edge; i++)
  61.         {
  62.             scanf("%d%d",&u,&v);
  63.  
  64.             graph[u][v] = graph[v][u] = true;
  65.         }
  66.  
  67.         curr = 0;
  68.  
  69.         dfs(1,0);
  70.  
  71.         printf("%d\n",curr);
  72.  
  73.         for(i=1;i<=node;i++)
  74.         {
  75.             if(ans[i])
  76.             {
  77.                 printf("%d",i);
  78.  
  79.                 break;
  80.             }
  81.         }
  82.  
  83.         for(i=i+1;i<=node;i++)
  84.         {
  85.             if(ans[i])
  86.             {
  87.                 printf(" %d",i);
  88.             }
  89.         }
  90.  
  91.         puts("");
  92.  
  93.         memset(color,false,sizeof(color));
  94.  
  95.         memset(graph,false,sizeof(graph));
  96.  
  97.     }
  98.  
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement