fork download
  1. #include <stdio.h>
  2. #define fin "graphcoloring.in"
  3. #define SIZE 50
  4.  
  5. int main(int argc, char const *argv[]) {
  6.  
  7. int i, j, n, ok;
  8. int matrix[SIZE][SIZE];
  9. //freopen(fin, "r", stdin);
  10.  
  11. //read the number of the countries
  12. scanf("%d", &n);
  13. int arr_colors[n+1],
  14. color;
  15.  
  16. //read the relation between countries
  17. for(i = 0; i < n; ++i) {
  18.  
  19. for(j = 0; j < n; ++j) {
  20.  
  21. scanf("%d", &matrix[i][j]);
  22.  
  23. }
  24. }
  25.  
  26. //display the relation between countries
  27. for(i = 0; i < n; ++i) {
  28.  
  29. for(j = 0; j < n; ++j) {
  30.  
  31. printf("%d ", matrix[i][j]);
  32. }
  33. printf("\n");
  34. }
  35.  
  36. arr_colors[ 0 ] = 0;
  37.  
  38. for( i = 1; i < n; ++i ) {
  39.  
  40. color = -1;
  41.  
  42. do {
  43. ok = 1;
  44. color++;
  45. for(j = 0; j < i; ++j)
  46. if(matrix[j][i] == 1 && arr_colors[j] == color)
  47. ok = 0;
  48. }
  49.  
  50. while(!ok);
  51.  
  52. arr_colors[i] = color;
  53. }
  54.  
  55. printf("colors:\n");
  56.  
  57. for(i = 0; i < n; ++i) printf("%d ", arr_colors[i]+1);
  58.  
  59. printf("\n");
  60.  
  61. return 0;
  62. }
  63.  
Success #stdin #stdout 0s 5460KB
stdin
7
0 1 1 1 0 0 1
1 0 1 1 0 0 0
1 1 0 1 1 0 1
1 1 1 0 1 0 1
0 0 1 1 0 1 1
0 0 0 0 1 0 1
1 0 1 1 1 1 0
stdout
0 1 1 1 0 0 1 
1 0 1 1 0 0 0 
1 1 0 1 1 0 1 
1 1 1 0 1 0 1 
0 0 1 1 0 1 1 
0 0 0 0 1 0 1 
1 0 1 1 1 1 0 
colors:
1 2 3 4 1 2 5