Advertisement
Guest User

Untitled

a guest
May 20th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int ml = 1000000000, cnt = 0;
  7.  
  8. void dfs(vector<bool> used, vector< vector<int> > edg, int st, int cur, vector<int> path){
  9.  
  10. path.push_back(cur);
  11.  
  12. used[cur] = true;
  13. for (int i = 0; i < edg[cur].size(); i++){
  14.  
  15. if (edg[cur][i] == st){
  16.  
  17. if (path.size() < ml){
  18. ml = path.size();
  19. cnt = 1;
  20. }
  21. else if (path.size() == ml)
  22. cnt += 1;
  23. }
  24. //cout << used[edg[cur][i]];
  25. if (path.size() < ml && not used[edg[cur][i]]) {
  26. dfs(used, edg, st, edg[cur][i], path);
  27. }
  28. }
  29. int x = path[-1];
  30. path.pop_back();
  31. //used[x] = 0;
  32. return;
  33. }
  34.  
  35.  
  36. int main() {
  37. freopen("cycle.in", "r", stdin);
  38. freopen("cycle.out", "w", stdout);
  39. int n, x;
  40. cin >> n >> x;
  41. vector <vector<int>> edg;
  42. vector<bool> used;
  43. for (int i = 0; i < n; i++){
  44. used.push_back(false);
  45. string s;
  46. cin >> s;
  47. vector <int> a;
  48. for (int j = 0; j < n; j++) {
  49. if (s[j] == '1') {
  50. a.push_back(j);
  51. }
  52. }
  53. edg.push_back(a);
  54.  
  55. }
  56.  
  57.  
  58. vector <int> p;
  59. dfs(used, edg, x - 1, x - 1, p);
  60.  
  61. if (cnt == 0)
  62. cout << -1;
  63. else
  64. cout << ml << " " << cnt;
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement