版本比较
代码模版
正文
增加 126 行删除 1 行
112345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313439 行未改动17417517617717817918018118245 行未改动## 树状数组# 搜索### 广搜最短路模板```cpp#include<bits/stdc++.h>using namespace std;#define int long long#define TT int T;cin>>T;while(T--)const int N = 1e6 + 10, M = 1e9 + 7;int n, m;char a[101][101];bool vis[101][101];bool in(int x, int y) { return 1 <= x && x <= n && 1 <= y && y <= m;}int dx[4] = {1, -1, 0, 0};int dy[4] = {0, 0, 1, -1};struct node { int x, y, d; //(x,y) 走了d步};queue<node> q;//队列signed main() { cin >> n >> m; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> a[i][j]; } } q.push(node{1, 1, 1}); vis[1][1] = 1; while (q.size()) { node f = q.front(); //当前任务 q.pop(); //到终点 if (f.x == n && f.y == m) { cout << f.d; return 0; }//做任务 for (int i = 0; i < 4; i++) { int tx = f.x + dx[i]; int ty = f.y + dy[i]; if (in(tx, ty) && !vis[tx][ty] && a[tx][ty] != '#') { vis[tx][ty] = 1; q.push(node{tx, ty, f.d + 1}); } } } return 0;}```
### 并查集```cpp#include<bits/stdc++.h>#define int long longusing namespace std;const int N = 1e6 + 10, M = 5e5 + 10;int n, m;int f[N];int find(int x) { if (x == f[x]) return x; return f[x] = find(f[x]);}signed main() { cin >> n >> m; for (int i = 1; i <= n; i++) { f[i] = i; } while (m --) { int t, x, y; cin >> t >> x >> y; int X = find(x); int Y = find(y); if (t == 1) f[X] = Y; else if (t == 2) { if (X == Y) cout << "Y\n"; else cout << "N\n"; } } return 0;}```### 深搜迷宫类模板```cpp#include<bits/stdc++.h>#define int long longusing namespace std;int n, a[21][21];bool vis[21][21];int sx, sy, ex, ey;int dx[4] = {-1, 1, 0, 0};int dy[4] = {0, 0, -1, 1};bool in(int x, int y) { return 1 <= x && x <= n && 1 <= y && y <= n;}void dfs(int x, int y) { if (x == ex && y == ey) { cout << "YES"; exit(0); } for (int i = 0; i < 4; i++) { int tx = x + dx[i]; int ty = y + dy[i]; if (in(tx, ty) && a[tx][ty] == 0 && vis[tx][ty] == 0) { vis[tx][ty] = 1; dfs(tx, ty); } }}signed main() { cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> a[i][j]; } } cin >> sx >> sy >> ex >> ey; dfs(sx, sy); cout << "NO"; return 0;}```
# 图论### 树状数组
```cpp#include<bits/stdc++.h>#define int long long#define IOS ios::sync_with_stdio(0),cin.tie(nullptr),cout.tie(nullptr);#define memset(a) memset((a), (0), sizeof(a));using namespace std;const int N = 1e6+10, M = 1e2+10, MOD = 1e9+7;39 行未改动 } return 0;}```# 动态规划## 背包### 01背包```cpp#include<bits/stdc++.h>45 行未改动