-
Notifications
You must be signed in to change notification settings - Fork 0
/
DS3.cpp
113 lines (87 loc) · 1.91 KB
/
DS3.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include<bits/stdc++.h>
using namespace std;
struct Node{
vector<int> data;
vector<int> adj_list;
bool visited;
};
Node* newNode(vector<int> val)
{
Node* tmp = new Node;
tmp->data = val;
tmp->adj_list={};
tmp->data=val;
tmp->visited=false;
return tmp;
}
bool distributedSearch(vector<Node*> &graph, int key, int root)
{
if(graph[root]->visited) return false;
graph[root]->visited=true;
//cout<<"visiting "<<root<<" checked ";
for(auto val:graph[root]->data)
{
//cout<<val<<" ";
if(val==key){
cout<<"found at node "<<root<<endl;
return true;
}
}
//cout<<endl;
// for(auto it:root)
// {
// if(!(it->visited) && distributedSearch(graph,key,it));
// return true;
// }
for(int i=0;i<graph[root]->adj_list.size();i++)
{
if(distributedSearch(graph,key,graph[root]->adj_list[i]))
return true;
}
return false;
}
int main()
{
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int nodeCount;
cin>>nodeCount;
vector<Node*> graph(nodeCount+1,NULL);
int size,val;
for(int i=1;i<=nodeCount;i++)
{
if(graph[i]==NULL)
graph[i] = newNode({});
cin>>size;
for(int j=0;j<size;j++)
{
cin>>val;
graph[i]->data.push_back(val);
}
}
for(int node=1;node<=nodeCount;node++)
{
for(int adjNode=1;adjNode<=nodeCount;adjNode++)
{
if(node==adjNode) continue;
graph[node]->adj_list.push_back(adjNode);
}
}
int key, root;
cin>>key;
root=1;
if(distributedSearch(graph,key,1))
cout<<"Found the value in the distributed network\n";
else
cout<<"Not Found the value in the distributed network\n";
}
//input.txt
/*
5
2 12 32
1 15
2 13 43
2 43 67
3 32 56 34
73
*/