Advertisement
PedalaVasile

graf1

May 6th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream fin("graf1.in");
  6. ofstream fout("graf1.out");
  7.  
  8. int n, m, start, stop;
  9.  
  10. vector < int > lis[7501];
  11. vector < int > rez;
  12. bitset < 7501 > viz;
  13. vector < int > ap;
  14.  
  15. int lMin = INT_MAX;
  16.  
  17. void dfs(int l, int nod, int antnod, int cate = 0)
  18. {
  19.     viz[nod] = 1;
  20.     rez.push_back(nod);
  21.     cout << antnod << ' ' <<  nod << ' ' << l << '\n';
  22.  
  23.     if(nod == stop)
  24.     {
  25.         cout << cate << '\n';
  26.         if(l < lMin)
  27.         {
  28.             ap.clear();
  29.             for(int el : rez)
  30.                 ap[el]++;
  31.  
  32.             lMin = l;
  33.         }
  34.         else if(l == lMin)
  35.         {
  36.             for(int el : rez)
  37.                 ap[el]++;
  38.         }
  39.  
  40.         return;
  41.     }
  42.  
  43.     if(l >= lMin)
  44.         return;
  45.  
  46.     for(int vecin : lis[nod])
  47.     {
  48.         if(!viz[vecin])
  49.         {
  50.             dfs(l + 1, vecin, nod, cate + 1);
  51.             rez.pop_back();
  52.         }
  53.     }
  54.     viz[nod] = 0;
  55. }
  56.  
  57. int main()
  58. {
  59.     ap.resize(7501);
  60.  
  61.     fin >> n >> m >> start >> stop;
  62.  
  63.     int a, b;
  64.  
  65.     while(m--)
  66.     {
  67.         fin >> a >> b;
  68.         lis[a].push_back(b);
  69.         lis[b].push_back(a);
  70.     }
  71.  
  72.     dfs(1, start, 0);
  73.  
  74.     int c = 0;
  75.  
  76.     for(int i = 1; i <= n; i++)
  77.         fout << i << ' ' << ap[i] << '\n', c++;
  78.  
  79.     fout << c;
  80.  
  81.     fin.close();
  82.     fout.close();
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement