-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgraph adj list.cpp
More file actions
109 lines (91 loc) · 1.64 KB
/
graph adj list.cpp
File metadata and controls
109 lines (91 loc) · 1.64 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include<bits/stdc++.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
int main() {
freopen("input.txt", "r", stdin);
freopen("op.txt", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin>>t;
while(t--)
{
int v,e;
cin>>v>>e;
vector<int> gp[v];
for(int i=0; i<e; i++)
{
int a,b;
cin>>a>>b;
gp[a].push_back(b);
gp[b].push_back(a);
}
for(int j=0; j<v; j++)
{
cout<<j<<"";
for(auto z: gp[j])
cout<<"-> "<<z<<"";
cout<<"\n";
}
}
return 0;
}
// bfs
vector <int> bfs(vector<int> g[], int N) {
queue<int> q;
vector<int> ans;
q.push(0);
bool vis[N+1] = {false};
while(!q.empty())
{
int n = q.front();
q.pop();
ans.push_back(n);
for(auto si: g[n])
if(!vis[si])
{
vis[si] = true;
q.push(si);
}
}
return ans;
//dfs
vector<int> dfs(vector<int> g[], int N){
bool vis[N+1] = {false};
vector<int> ans;
stack<int> s;
s.push(0);
ans.push(0);
vis[0] = true;
int i=0;
while(!s.empty())
{
int i = s.pop();
for(auto ch : g[i])
{
if(!vis[ch])
}
}
}
// dfs
bool vis[N+1] = {false};
vector<int> adj;
vecotr<int> ans;
vector<int> dfs(vector<int> adj[], int i, vector< int> ans )
{
vis[i] = true;
ans.push_back(i);
for(auto it : adj[i])
{
if(!vis[it])
dfs(adj,it,ans);
}
return ans;
}
for(int i=0; i<N; i++)
{
if(!vis[i])
dfs(adj, i, ans);
}
//dfs