当前位置:网站首页>Self study understanding of [chain forward star]
Self study understanding of [chain forward star]
2022-07-29 06:28:00 【C2024XSC249】
Let's take the following figure as an example :

According to the order of edge construction , We can create a sequence number for these edges , Such as :
1 2 <---1 Number
2 3 <---2 Number
3 4 <---3 Number
1 3 <---4 Number
4 1 <---5 Number
1 5 <---6 Number
4 5 <---7 Number
according to 《 Algorithm competition 》 Ideas given in , We can write a linked list :
1 -> 5 -> 3 -> 2
2 -> 3
3 -> 4
4 -> 5 -> 1
5
such , We can observe from back to front , Maintain a head[i] Array , Indicates that the arc head in the input edge is i The number of the last side of , namely :
head[1] = 6
head[2] = 2
head[3] = 3
head[4] = 7
head[5] = 0( That is, there is no arc head 5 The edge of )
We can also maintain it while saving to[i] Array , That is to say i The arc tail of the edge . This is too easy , Don't say .
Finally, we maintain next[i] Array , We set the first i The arc head of the edge is x, be next[i] Said in the first i The last arc before the edge is x Number of the side of , namely :
next[1] = 0
next[2] = 0
next[3] = 0
next[4] = 1
next[5] = 0
next[6] = 4
next[7] = 5
After getting these three arrays , We can realize the edge adding operation .
void AddEdge (int u, int v, int x) {
to[++ cnt] = v;
w[cnt] = x;
next[cnt] = head[u];
head[u] = cnt;
}
When we want to enumerate the adjacency points of this point , Just use for Just loop through it .
for (int i = head[x]; i; i = next[i]) {
//...
}
Example : Adjacency table stores weighted undirected graph
Code
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN = 1000 + 5;
int n, m, u, p, x, cnt, to[MAXN], head[MAXN], w[MAXN], next[MAXN];
struct node {
int x, w;
bool operator < (const node x) const {
if (w == x.w) {
return this->x < x.x;
} else {
return w < x.w;
}
}
};
vector <node> v;
void AddEdge (int u, int v, int x) {
to[++ cnt] = v;
w[cnt] = x;
next[cnt] = head[u];
head[u] = cnt;
}
int main () {
scanf ("%d %d", &n, &m);
for (int i = 1; i <= m; i ++) {
scanf ("%d %d %d", &u, &p, &x);
AddEdge (u, p, x);
AddEdge (p, u, x);
}
for (int i = 1; i <= n; i ++) {
v.clear();
for (int j = head[i]; j; j = next[j]) {
v.push_back((node) {
to[j], w[j]});
}
sort (v.begin(), v.end());
for (int i = 0; i < v.size(); i ++) {
printf ("%d ", v[i].x);
}
printf ("\n");
}
return 0;
}
边栏推荐
- 官方教程 Redshift 05 AOVs
- EtherCAT主站掉线后,如何保证目标系统免受故障影响?
- Learning notes of bit operation
- 【Leetcode刷题】数组2——二分查找
- Traditional model predictive control trajectory tracking - circular trajectory (function package has been updated)
- Vivado IP核之浮点数开方 Floating-point
- Linked list -------------------------- tail insertion method
- Access、Hybrid和Trunk三种模式的理解
- [leetcode skimming] array 2 - binary search
- c语言问题
猜你喜欢

Navicat for Oracle Cannot create oci environment

STP生成树原理及选举规则举例

LeetCode #26.删除有序数组中的重复项

Official tutorial redshift 09 camera

V-ray 5 ACEScg 工作流程设置
![[leetcode brush questions] array 3 - divide and conquer](/img/76/bc3d9ba0b84578e17bf30195bda5d1.png)
[leetcode brush questions] array 3 - divide and conquer

Maya aces workflow configuration (Arnold and redshift map configuration specification - restore the correct effect of the map under the SP aces process) PS restore the rendered map under the aces proc

Dynamic planning summary

Leetcode - Tips

虹科方案 | 在数字化的变电站中低成本实现无缝集成的独特解决方案
随机推荐
Leetcode 83. delete duplicate elements in the sorting linked list
Encapsulation - Super keyword
Abstract encapsulation inheritance polymorphism
Dynamic planning summary
进程与线程
文件系统一
Navicat for Oracle Cannot create oci environment
Learning notes of bit operation
Overview and summary of GI engine in redshift 024, the official tutorial
Abstract classes and interfaces
c语言问题
Operating system interview questions
MySQL interview questions
摊余成本最牛例子
Sliding window leetcode 76. minimum covering substring (hard) 76.76. minimumwindow substring (hard)
官方教程 Redshift 07 Instances and Proxy
[beauty of software engineering - column notes] 14 | project management tools: all management problems should be considered whether they can be solved by tools
Computer network interview questions
Vivado IP核之浮点数开方 Floating-point
Leetcode notes 452. minimum number of arrows to burst balloons (medium) 452. detonate balloons with the minimum number of arrows (medium)