图的存储和遍历模板

图的存储和遍历模板

八月 01, 2021
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
/*
BlackPink is the Revolution
light up the sky
Blackpink in your area
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<queue>
#include<cctype>
#include<bitset>
#include<vector>
#include<ctime>
#include<map>
#include<set>
using namespace std;
#define rep(i, a, b) for(int i = (a); (i) <= (b); ++i)
#define per(i, a, b) for(int i = (a); (i) >= (b); --i)
#define whlie while
const int N=4e5+10;
typedef long long ll;
typedef pair<int, int> P;

struct edge{
int to, nxt, val;
}e[N<<1];
int cnt, head[N];

inline void add(int u, int v, int w){
e[++cnt] = (edge){v, head[u], w}, head[u] = cnt;
}

inline void add(int u, int v){
e[++cnt] = (edge){v, head[u]}, head[u] = cnt;
}

template <typename T>
inline void read(T &x){
x=0; int f=0; char c = getchar();
for (; !isdigit(c); c = getchar()) f |= (c == '-');
for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
if (f) x = -x;
}

template <typename T>
inline void write(T x, char ch){
if(x<0) putchar('-'), x = -x;
static short st[30], tp = 0;
do st[++tp] = x % 10, x /= 10; while (x);
while (tp) putchar(st[tp--] | 48);
putchar(ch);
}

int n;
int a[N];

inline void dfs(int u, int f) {
for(int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if(v == f) continue;
dfs(v, u);
...
}
}

inline void bfs(int s) {
queue<int> q;
bool vis[N];
q.push(s);
while (!q.empty()) {
int u = q.front(); q.pop();
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if(...) q.push(v);
}
}
}

int main(){
read(n);
return 0;
}