代码随想录算法训练营第五十六天 | 图论part06

108. 冗余连接

cpp 复制代码
#include <iostream>
#include <vector>

using namespace std;

void init(vector<int> &father) {
    for (int i = 0; i < father.size(); ++i) {
        father[i] = i;
    }
}

int find(vector<int>& father, int u) {
    if (father[u] == u) return u;
    return father[u] = find(father, father[u]);
}

int isSame(vector<int>& father, int u, int v) {
    u = find(father, u);
    v = find(father, v);
    return u == v;
}

void join(vector<int>& father, int u, int v) {
    u = find(father, u);
    v = find(father, v);
    if (u == v) return;
    father[v] = u;
}
int main()
{
    int n, u, v, resultu, resultv;
    cin >> n;
    vector<int> father(n + 1, 0);
    init(father);
    for (int i = 0; i < n; ++i) {
        cin >> u >> v;
        if (isSame(father, u, v)) {
            resultu = u;
            resultv = v;
        }
        else
            join(father, u, v);
    }
    cout << resultu << " " << resultv << endl;
    return 0;
}

109. 冗余连接II

关键是要能够分出两种情况:

  1. 是不是存在入度为2的点,然后判断哪一条边去掉,还是连通的。在这里也有两种情况,一种是直接删除最后一个边,一种是会形成环。
  2. 是不是存在环
cpp 复制代码
#include <iostream>
#include <vector>

using namespace std;

void init(vector<int>& father) {
    for (int i = 0; i < father.size(); ++i) {
        father[i] = i;
    }
}

int find(vector<int>& father, int u) {
    if (father[u] == u) return u;
    return father[u] = find(father, father[u]);
}

int isSame(vector<int>& father, int u, int v) {
    u = find(father, u);
    v = find(father, v);
    return u == v;
}

void join(vector<int>& father, int u, int v) {
    u = find(father, u);
    v = find(father, v);
    if (u == v) return;
    father[v] = u;
}

bool isTreeAfterRemove(vector<int>& father, const vector<vector<int>>& edges, int deleteEdge) {
    init(father);
    for (int i = 0; i < edges.size(); ++i) {
        if (i == deleteEdge) continue;
        if (isSame(father, edges[i][0], edges[i][1]))
            return false;
        join(father, edges[i][0], edges[i][1]);
    }
    return true;
}

void getRemoveEdge(vector<int>& father, const vector<vector<int>>& edges) {
    init(father);
    for (vector<int> pair : edges) {
        if (isSame(father, pair[0], pair[1])) {
            cout << pair[0] << " " << pair[1] << endl;
            return;
        }
        join(father, pair[0], pair[1]);
    }
}

int main()
{
    int n, u, v;
    cin >> n;
    vector<int> father(n + 1, 0);
    vector<vector<int>> edges;
    vector<int> inDegrees(n + 1, 0);
    for (int i = 0; i < n; ++i) {
        cin >> u >> v;
        inDegrees[v]++;
        edges.emplace_back(vector<int>{u, v});
    }

    vector<int> vec;
    for (int i = n - 1; i >= 0; --i) {
        if (inDegrees[edges[i][1]] == 2)
            vec.push_back(i);
    }
    if (vec.size() > 0) {
        if (isTreeAfterRemove(father, edges, vec[0])) {
            cout << edges[vec[0]][0] << " " << edges[vec[0]][1] << endl;
        }
        else
            cout << edges[vec[1]][0] << " " << edges[vec[1]][1] << endl;
        return 0;
    }
    getRemoveEdge(father, edges);
    return 0;
}
相关推荐
程序员JerrySUN11 分钟前
BitBake 执行流程深度解析:从理论到实践
linux·开发语言·嵌入式硬件·算法·架构
王老师青少年编程1 小时前
gesp(二级)(16)洛谷:B4037:[GESP202409 二级] 小杨的 N 字矩阵
数据结构·c++·算法·gesp·csp·信奥赛
robin_suli1 小时前
动态规划子序列问题系列一>等差序列划分II
算法·动态规划
cxylay2 小时前
自适应滤波算法分类及详细介绍
算法·分类·自适应滤波算法·自适应滤波·主动噪声控制·anc
茶猫_2 小时前
力扣面试题 - 40 迷路的机器人 C语言解法
c语言·数据结构·算法·leetcode·机器人·深度优先
轻浮j2 小时前
Sentinel底层原理以及使用算法
java·算法·sentinel
Abelard_3 小时前
LeetCode--347.前k个高频元素(使用优先队列解决)
java·算法·leetcode
小猪写代码3 小时前
C语言:递归函数(新增)
算法·c#
点云SLAM3 小时前
C++创建文件夹和文件夹下相关操作
开发语言·c++·算法
heeheeai3 小时前
kotlin 函数作为参数
java·算法·kotlin