Advertisement
YauhenMardan

Untitled

Mar 29th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #include <fstream>
  2. #include <queue>
  3. int main(int argc, const char * argv[]) {
  4. std::ifstream fin("input.txt");
  5. std::ofstream fout("output.txt");
  6.  
  7. int n;
  8. fin>>n;
  9. //init and read adj matrix
  10. int* mark=new int[n];
  11. int* path=new int[n];
  12. bool** adj=new bool*[n];
  13. for(int i=0;i<n;i++){
  14. adj[i]=new bool[n];
  15. }
  16. bool* visit=new bool[n];
  17. std::queue<int> fifo;
  18. for(int i=0;i<n;i++){
  19. path[i]=0;
  20. }
  21. for(int i=0;i<n;i++){
  22. for(int j=0;j<n;j++){
  23. fin>>adj[i][j];
  24. if(adj[i][j]){
  25. path[j]=i+1;
  26. }
  27. }
  28. }
  29. for(int i=0;i<n;i++){
  30. visit[i]=false;
  31. }
  32. int e;
  33. //bfs
  34.  
  35. for(int i=0;i<n;i++){
  36. if(!visit[i]){
  37. fifo.push(i);
  38. visit[i]=true;
  39. mark[i]=1;
  40. int m=mark[i];
  41. while (!fifo.empty()) {
  42. e=fifo.front();
  43. fifo.pop();
  44. for(int i=0;i<n;i++){
  45. if(adj[e][i]&&!visit[i]){
  46. // if(!fifo.empty()){
  47. mark[i]=m+1;
  48. m++;
  49. // }
  50. visit[i]=true;
  51. fifo.push(i);
  52. }
  53. }
  54. }
  55. }
  56. }
  57. for (int i=0; i<n; i++) {
  58. fout<<mark[i]<<" ";
  59. }
  60. fout<<"\n";
  61. return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement