fork download
  1. #include <iostream>
  2. #define FIN "graphcoloring.in"
  3. #define FOUT "graphcoloring.out"
  4. #define DIM 100
  5.  
  6. using namespace std;
  7.  
  8. int matrix[ DIM ][ DIM ],
  9. colors [ DIM ],
  10. countries, color, ok;
  11.  
  12. int main(int argc, char const *argv[]) {
  13.  
  14. //freopen(FIN, "r", stdin);
  15.  
  16. cin>>countries;
  17.  
  18. for(int i = 0; i < countries; ++i) {
  19. for(int j = 0; j < countries; ++j) {
  20. cin>>matrix[i][j];
  21. }
  22. }
  23.  
  24. for(int i = 0; i < countries; ++i) {
  25. for(int j = 0; j < countries; ++j) {
  26. cout<<matrix[i][j]<<" ";
  27. }
  28. cout<<endl;
  29. }
  30.  
  31. colors[0] = 0;
  32.  
  33.  
  34. for(int i = 1; i < countries; ++i) {
  35.  
  36. color = -1;
  37.  
  38. do {
  39.  
  40. color++;
  41.  
  42. ok = 1;
  43.  
  44. for(int k = 0; k < i && ok; ++k) {
  45.  
  46. if(matrix[ k ][ i ] == 1 && colors[ k ] == color) ok = 0;
  47.  
  48. }
  49.  
  50. } while(!ok);
  51.  
  52. colors[i] = color;
  53.  
  54. }
  55.  
  56. for(int i = 0; i < countries; ++i) cout<<colors[i]+1<<" ";
  57.  
  58. return 0;
  59. }
Success #stdin #stdout 0.01s 5516KB
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 
1 2 3 4 1 2 5