-
Notifications
You must be signed in to change notification settings - Fork 66
/
07-Valid Bfs.cpp
48 lines (47 loc) · 1.01 KB
/
07-Valid Bfs.cpp
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
#include<bits/stdc++.h>
using namespace std;
bool validBfs(int n, vector<int> sequence , vector<vector<int>> edges)
{
int x,y,val,val1;
vector<vector<int>>v(n+1);
vector<int>visit(n+1);
for(int i=0;i<n-1;i++)
{
x=edges[i][0],y=edges[i][1];
v[x].push_back(y);
v[y].push_back(x);
}
queue<int>q;
q.push(1);
visit[1]=1;
y=1;
int flag=0;
while(q.size())
{
x=q.front();
q.pop();
set<int>s;
for(int i=0;i<v[x].size();i++)
{
val=v[x][i];
if(visit[val]==0)
{
visit[val]=1;
s.insert(val);
}
}
val1=0;
while(val1<s.size())
{
if(s.find(sequence[y])==s.end())
{
flag=1;
break;
}
q.push(sequence[y]);
y++;
val1++;
}
}
return flag==1? false: true;
}