-
Notifications
You must be signed in to change notification settings - Fork 1
/
BFS similar code
113 lines (103 loc) · 2.99 KB
/
BFS similar code
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
/*
Author: Fuadul Hasan([email protected])
BSMRSTU,Gopalganj
*/
#include<bits/stdc++.h>
using namespace std;
void print(int n) {printf("%d ", n);}
void printl(long long n) {printf("%I64d ", n);}
long long readline(){long long x;scanf("%I64d", &x);return x;}
/*
1.#define gcd(a,b) __gcd(a,b) 2. #define lcm(a,b) (a*b)/gcd(a,b) 3. #define PI acos(-1.0) 4.#define sqr(x) ((x)*(x))
5.#define min3(a,b,c) min(a,min(b,c))6.#define min4(a,b,c,d) min(d,min(a,min(b,c)))7.#define max3(a,b,c) max(a,max(b,c))
8.#define max4(a,b,c,d) max(d,max(a,max(b,c)))
*/
//.............ignore it................//
/*....... scanf.......*/
template <typename T> inline void readline(T &n) {
n = 0; int f = 1; register int ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1;
for (; isdigit(ch); ch = getchar()) n = (n << 3) + (n << 1) + ch - '0';n = n * f;}
template <typename T, typename TT> inline void readline(T &n, TT &m) { readline(n); readline(m); }
template <typename T, typename TT, typename TTT> inline void readline(T &n, TT &m, TTT &l) { readline(n, m); readline(l);}
/*..........error........*/
#define error(args...) {vector<string>_v=split(#args,',');err(_v.begin(),args);cout<<endl;}
vector<string> split(const string &s, char c) {
vector<string>v; stringstream ss(s); string x;
while (getline(ss, x, c))v.emplace_back(x); return move(v);
} void err(vector<string>::iterator it) {}
template<typename T, typename... Args>void err(vector<string>::iterator it, T a, Args...args) {
cout << it->substr((*it)[0] == ' ', it->length()) << " = " << a << " "; err(++it, args...);}
//............ignore it..................//
const int mod = 1e9 + 7;
const int inf = (int)2e9 + 5;
const int N = 1e6 + 5;
const int N1 = 3e5 + 5;
typedef pair<int, int> ii;
typedef std::vector<ii> vii;
#define vi std::vector<int>
#define vll std::vector<ll>
#define all(n) n.begin(),n.end()
#define mpsi std::map<string, int>
#define pb push_back
#define ll long long int
#define nl printf("\n");
#define happy cin.tie(0);
#define coding ios::sync_with_stdio(false);
std::vector<vector<int>>g;
std::vector<bool> visit;
void edge(int a,int b)
{
g[a].push_back(b);
}
/*void bfs(int u)
{
queue<int>q;
q.push(u);
visit[u] = true;
while(!q.empty()){
int x = q.front();
q.pop();
cout<<x<<" ";
for(auto i: g[x]){
cout<<i<<" ";
visit[i]=true;
}
}
}
*/
int solve()
{
//happy coding
//BFS
int n,m;
readline(n,m);
visit.assign(100,false);
g.assign(100,vector<int>());
for(int i=0;i<m;i++){
int a,b;
readline(a,b);
edge(a,b);
}
for(int i=0;i<n;i++){
if(g[i].size()!=0){
cout<<i<<"-> ";
for(auto j: g[i]){
if(j!=0){
if(!visit[j]){
cout<<j<<" ";
visit[j] = true;
}
}
}nl;
}
}
//error()
return 0;
}
int main(){
int test = 1, tc = 0;
//scanf("%d", &test);//(void)getchar();
while (test--)solve();
return 0;
}