Brine
A certain person trying to make his way in the universe.
Index
Glossary
所以什麼是圖?
邊
的路徑Graph Storage
int main() {
int vertexCount, edgeCount;
cin >> vertexCount >> edgeCount;
vector< vector<bool> > graph;
graph.assign(vertexCount, vector<bool>(vertexCount));
int u, v;
for (int i = 0; i < edgeCount; i++) {
cin >> u >> v;
graph[u][v] = graph[v][u] = 1;
}
}
空間複雜度?
int main() {
int vertexCount, edgeCount;
cin >> vertexCount >> edgeCount;
vector< vector<int> > graph(vertexCount);
int u, v;
for (int i = 0; i < edgeCount; i++) {
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
}
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
cin >> u >> v;
graph[u].push_back(v);
cin >> u >> v;
graph[u][v] = 1;
graph[v][u] = 1;
cin >> u >> v;
graph[u][v] = 1;
會用 pair<int, int> 是因為有內建比較函式
typedef pair<int, int> Edge;
int main() {
int vertexCount, edgeCount;
cin >> vertexCount >> edgeCount;
vector< vector<Edge> > graph(vertexCount);
int u, v, w; // w is for weight
for (int i = 0; i < edgeCount; i++) {
cin >> u >> v >> w;
graph[u].push_back({w, v});
graph[v].push_back({w, u});
}
}
p.first 和 p.second 不好打?
用 priority_queue 會有問題,但很難寫
struct E {
int to;
int w;
};
int main() {
priority_queue<E, vector<E>, function<bool(E, E)> > pq(
[](const E& a, const E& b) {
return (a.w != b.w ? a.w < b.w : a.to < b.to);
}
);
}
項目 | 鄰接矩陣 | 鄰接串列 |
---|---|---|
空間複雜度 | ||
插入邊 | ||
查詢邊 | ||
枚舉邊 |
Other Methods
#include <iostream>
using namespace std;
int main() {
int height, length;
cin >> height >> length;
vector<string> graph(height);
for (auto& s: graph) cin >> s;
}
int main() {
int height, length;
cin >> height >> length;
vector<vector<int>> graph(height + 2,
vector<int>(length + 2, -1));
for (int i = 1; i <= height; i++) {
for (int j = 1; j <= length; j++) {
cin >> graph[i][j];
if (graph[i][j]) graph[i][j] = -1;
}
}
}
#include <bits/stdc++.h>
using namespace std;
int main() {
int vertexCount, edgeCount;
cin >> vertexCount >> edgeCount;
vector< pair<int, int> > edgeList(edgeCount);
for (auto& [u, v]: edgeList) {
cin >> u >> v;
}
}
Graph Traversal
我們現在有某個點突然有很多的水的一張圖,而我們現在想要計算出這張圖中的點何時會被水淹到,該怎麼做呢?
我們現在有某個點突然有很多的水的一張圖,而我們現在想要計算出這張圖中的點何時會被水淹到,該怎麼做呢?
我們現在有某個點突然有很多的水的一張圖,而我們現在想要計算出這張圖中的點何時會被水淹到,該怎麼做呢?
我們現在有某個點突然有很多的水的一張圖,而我們現在想要計算出這張圖中的點何時會被水淹到,該怎麼做呢?
我們現在有某個點突然有很多的水的一張圖,而我們現在想要計算出這張圖中的點何時會被水淹到,該怎麼做呢?
我們現在有某個點突然有很多的水的一張圖,而我們現在想要計算出這張圖中的點何時會被水淹到,該怎麼做呢?
vector<int> bfs(int source, vector< vector<int> >& graph) {
queue<int> q;
q.push(source);
vector<int> distance(graph.size(), INT32_MAX);
distance[source] = 0;
while (!q.empty()) { // while (q.size())
int& current = q.front();
for (auto& n: graph[current]) {
if (distance[n] != INT32_MAX) continue;
distance[n] = distance[current] + 1;
q.push(n);
}
q.pop();
}
distance[source] = 0;
return distance;
}
四個方向的
while (!q.empty()) { // while (q.size())
auto& [x, y] = q.front();
// queue< pair<int, int> > q;
if (graph[x][y + 1] == INT32_MAX) {
graph[x][y + 1] = graph[x][y] + 1;
q.push({x, y + 1});
}
if (graph[x][y - 1] == INT32_MAX) {
graph[x][y - 1] = graph[x][y] + 1;
q.push({x, y - 1});
}
if (graph[x + 1][y] == INT32_MAX) {
graph[x + 1][y] = graph[x][y] + 1;
q.push({x + 1, y});
}
if (graph[x - 1][y] == INT32_MAX) {
graph[x - 1][y] = graph[x][y] + 1;
q.push({x - 1, y});
}
q.pop();
}
const int dx[] = {1,-1, 0, 0};
const int dy[] = {0, 0, 1,-1};
while (!q.empty()) {
auto& [x, y] = q.front();
for (int i = 0; i < 4; i++) {
auto& nextPosition = graph[x + dx[i]][y + dy[i]];
if (nextPosition == INT32_MAX) {
nextPosition = graph[x][y] + 1;
q.push({x + dx[i], y + dy[i]});
}
}
q.pop();
}
void dfs(int current, vector< vector<int> >& graph, vector<bool>& visited) {
visited[current] = 1;
for (auto& n: graph[current]) {
if (visited[n]) continue;
dfs(n, graph, visited);
}
}
int main() {
int vC, eC;
cin >> vC >> eC;
vector< vector<int> > graph(vC);
int u, v;
for (int i = 0; i < eC; i++) {
cin >> u >> v;
graph[u].push_back(v);
}
vector<bool> visited(vC);
dfs(0, graph, visited);
}
vector<bool> visited(vC);
function<void(int)> dfs = [&](int current) {
visited[current] = 1;
for (auto& n: graph[current]) {
if (!visited[n]) dfs(n);
}
};
vector< pair<int, int> > tree(vC);
int leftChild, rightChild;
for (int i = 0; i < vC; i++) {
cin >> leftChild >> rightChild;
tree[i] = {leftChild, rightChild};
}
vector<int> preorder, inorder, postorder;
function<void(int)> dfs = [&](int current) {
auto [l, r] = tree[current];
preorder.push_back(current);
if (l >= 0) dfs(l);
inorder.push_back(current);
if (r >= 0) dfs(r);
postorder.push_back(current);
};
By Brine
圖論[0]