跳到正文

个人记录

笔记7.16.2

用户 1583220约 8 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 8 分钟

版本

  1. v1当前

1.图论 :

1.顶点(Vertex)‌:图的基本元素,也称为节点 ‌

2.边(Edge)‌:连接两个顶点的线,可分为: 有向边(带方向)无向边(无方向)

3.权值(Weight)‌:边上的数值

表示方法

1.邻接矩阵

C++
const int MAX_V = 100;  // 最大顶点数

int adjMatrix[MAX_V][MAX_V];  // 二维数组表示

// 初始化(以无向图为例)

memset(adjMatrix, 0, sizeof(adjMatrix));

// 添加边(顶点a和b相连)

adjMatrix[a][b] = 1;  // 无权图用1表示连接

adjMatrix[b][a] = 1;  // 无向图需要对称设置

// 带权图

adjMatrix[a][b] = weight;

2.邻接表

C++
#include <vector>

using namespace std;

vector<int> adjList[MAX_V];  // 无权图

vector<pair<int, int>> weightedAdjList[MAX_V];  // 带权图(first是顶点,second是权值)

// 添加边(无向图示例)

adjList[a].push_back(b);

adjList[b].push_back(a);

// 添加带权边

weightedAdjList[a].emplace_back(b, weight);

weightedAdjList[b].emplace_back(a, weight);

3.边列表

C++
struct Edge {

    int u, v, weight;

    bool operator<(const Edge& other) const {

        return weight < other.weight;

    }

};

vector<Edge> edges;

常用的架构

1.深度优先搜索

C++
vector<bool> visited(MAX_V, false);

void dfs(int v) {

    visited[v] = true;

    for (int neighbor : adjList[v]) {

        if (!visited[neighbor]) {

            dfs(neighbor);

        }

    }

}

2.广度优先搜索

C++
void bfs(int start) {

    queue<int> q;

    vector<bool> visited(MAX_V, false);

    q.push(start);

    visited[start] = true;


    while (!q.empty()) {

        int v = q.front();

        q.pop();

        for (int neighbor : adjList[v]) {

            if (!visited[neighbor]) {

                visited[neighbor] = true;

                q.push(neighbor);

            }

        }

    }

}

其他比较特殊的类型

1.有向无环图:可进行拓扑排序 ‌ 2.二分图‌:可用着色法检测 ‌ 3.树‌:一种特殊的无向连通无环图

作业

P3916 100分 思:从编号最大的节点n开始倒序遍历对每个节点i执行DFS标记所有能到达i的节点的答案为i

C++
#include <iostream>

#include <vector>

using namespace std;

const int N = 1e5 + 10;

vector<int> g[N];

int ans[N];

void dfs(int u, int max_id) {

    if (ans[u]) return;

    ans[u] = max_id;

    for (int v : g[u]) dfs(v, max_id);

}

int main() {

    int n, m;

    cin >> n >> m;

    while (m--) {

        int u, v;

        cin >> u >> v;

        g[v].push_back(u);

    }

    for (int i = n; i >= 1; i--) dfs(i, i);

    for (int i = 1; i <= n; i++) cout << ans[i] << " ";

    return 0;

}

SP1437 0分 (没有思路)

P1807 100分

C++
#include<bits/stdc++.h>

using namespace std;

int n,m,in[1505];

vector<int>g[1505];

vector<int>d[1505];

queue<int>q;

int v[1505];

int main()

{

  cin>>n>>m;

  for(int i=1;i<=m;i++)

  {

    int ff,tt,dd;

    cin>>ff>>tt>>dd;

    g[ff].push_back(tt);

    d[ff].push_back(dd);

    in[tt]++;

  }

  for(int i=2;i<=n;i++)

  {

    v[i]=-1e9;

    if(!in[i]) q.push(i);

  }

  while(!q.empty())

  {

    int x=q.front();

    q.pop();

    for(int i=0;i<g[x].size();i++)

      if(!--in[g[x][i]]) q.push(g[x][i]);

  }

  q.push(1);

  while(!q.empty())

  {

    int x=q.front();

    q.pop();

    for(int i=0;i<g[x].size();i++)

    {

      if(v[g[x][i]]<v[x]+d[x][i]) v[g[x][i]]=v[x]+d[x][i];

      if(!--in[g[x][i]]) q.push(g[x][i]);

    }

  }

  if(v[n]==-1e9) cout<<"-1";

  else cout<<v[n];

  return 0;

}

P1395 100分

C++
#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

const int N = 50005;

vector<int> edge[N];

int n, center, min_dis = 0;

int max_sub[N], size[N];

void find_center(int u, int fa) {

    size[u] = 1;

    max_sub[u] = 0;

    for (int v : edge[u]) {

        if (v == fa) continue;

        find_center(v, u);

        size[u] += size[v];

        max_sub[u] = max(max_sub[u], size[v]);

    }

    max_sub[u] = max(max_sub[u], n - size[u]);

    if (max_sub[u] < max_sub[center] ||

        (max_sub[u] == max_sub[center] && u < center)) {

        center = u;

    }

}

void calc_dis(int u, int fa, int depth) {

    min_dis += depth;

    for (int v : edge[u]) {

        if (v == fa) continue;

        calc_dis(v, u, depth + 1);

    }


}

int main() {

    cin >> n;

    for (int i = 1; i < n; i++) {

        int u, v;

        cin >> u >> v;

        edge[u].push_back(v);

        edge[v].push_back(u);

    }

    max_sub[0] = n + 1;

    find_center(1, 0);

    calc_dis(center, 0, 0);

    cout << center << " " << min_dis << endl;

    return 0;

}
原文