-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAAAA.cpp
More file actions
89 lines (78 loc) · 1.37 KB
/
AAAA.cpp
File metadata and controls
89 lines (78 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
ll MOD = 1e9 + 7;
vector<int> tree[101];
bool goal=false;
void dfid(int src,int dest,string &path,int curlvl,int lvl,int par)
{
if(path!="")
path=path+" -> ";
path+='0'+src;
if(src==dest)
{
goal=true;
return;
}
if(lvl==curlvl)
{
return;
}
for(auto x:tree[src])
{
if(x==par)
continue;
dfid(x,dest,path,curlvl+1,lvl,src);
if(goal)
break;
}
return;
}
void solve()
{
cout<<"Enter number of nodes in tree"<<endl;
int n;
cin>>n;
cout<<"Enter edges of tree"<<endl;
for(int i=0;i<n-1;i++)
{
int a,b;
cin>>a>>b;
tree[a].push_back(b);
tree[b].push_back(a);
}
cout<<"Enter source and destination"<<endl;
int src,dest;
cin>>src>>dest;
cout<<"Enter maximum depth of tree"<<endl;
int maxdepth;
cin>>maxdepth;
for(int cur_lvl=1;cur_lvl<=maxdepth;cur_lvl+=1)
{
string path="";
dfid(src,dest,path,1,cur_lvl,-1);
if(goal)
{
cout<<"The required path is : "<<path<<endl;
return;
}
}
cout<<"Path not found"<<endl;
return;
}
int main()
{
// #ifndef ONLINE_JUDGE
// freopen("in.txt", "r", stdin);
// freopen("opts.txt", "w", stdout);
// #endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t = 1;
// cin >> t;
while (t-- )
{
solve();
}
return 0;
}