10.17 枚举中间|图论

lc2867

先标记出非质数,再构建树的结构

(DFS)统计每个由非质数组成的连通块大小

计算满足恰好含一个质数节点的合法路径数量。

const int MX = 1e5;

bool np[MX + 1]; // 质数=false 非质数=true

int init = []() {

np[1] = true;

for (int i = 2; i * i <= MX; i++) {

if (!np[i]) {

for (int j = i * i; j <= MX; j += i) {

np[j] = true;

}

}

}

return 0;

}();

class Solution {

public:

long long countPaths(int n, vector<vector<int>> &edges) {

vector<vector<int>> g(n + 1);

for (auto &e: edges) {

int x = e[0], y = e[1];

g[x].push_back(y);

g[y].push_back(x);

}

vector<int> size(n + 1);

vector<int> nodes;

function<void(int, int)> dfs = [&](int x, int fa) {

nodes.push_back(x);

for (int y: g[x]) {

if (y != fa && np[y]) {

dfs(y, x);

}

}

};

long long ans = 0;

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

if (np[x]) continue; // 跳过非质数

int sum = 0;

for (int y: g[x]) { // 质数 x 把这棵树分成了若干个连通块

if (!np[y]) continue;

if (size[y] == 0) { // 尚未计算过

nodes.clear();

dfs(y, -1); // 遍历 y 所在连通块,在不经过质数的前提下,统计有多少个非质数

for (int z: nodes) {

size[z] = nodes.size();

}

}

// 这 size[y] 个非质数与之前遍历到的 sum 个非质数,两两之间的路径只包含质数 x

ans += (long long) size[y] * sum;

sum += size[y];

}

ans += sum; // 从 x 出发的路径

}

return ans;

}

};

lc2242

先构建图,把每个点连接的点按分数排序并保留前3个

if (vs.size() > 3)

++nth_element(vs.begin(), vs.begin() + 3, vs.end());++

vs.resize(3);

然后枚举中间边(x-y) a-x-y-b

找边两端点各自连接的点中合适的(除环),计算能组成的最大得分

if (a != y && b != x && a != b)

++ans = max(ans, -score_a + scores[x] + scores[y] - score_b);++

返回最大得分(没有符合的就返回-1)

class Solution {

public:

int maximumScore(vector<int> &scores, vector<vector<int>> &edges)

{

int n = scores.size();

vector<vector<pair<int, int>>> g(n);

for (auto &e : edges) {

int x = e[0], y = e[1];

g[x].emplace_back(-scores[y], y);

g[y].emplace_back(-scores[x], x);

}

for (auto &vs : g)

if (vs.size() > 3) {

++nth_element(vs.begin(), vs.begin() + 3, vs.end());++

vs.resize(3);

}

int ans = -1;

for (auto &e : edges) {

int x = e[0], y = e[1];

for (auto &[score_a, a] : g[x])

for (auto &[score_b, b] : g[y])

if (a != y && b != x && a != b)

++ans = max(ans, -score_a + scores[x] + scores[y] - score_b);++

}

return ans;

}

};


为了对图中每个节点,只保留与它相连的、分数最大的前三个节点

if (vs.size() > 3)

++nth_element(vs.begin(), vs.begin() + 3, vs.end());++

vs.resize(3);

  • 首先遍历图 g 中的每个节点的邻接列表 vs 。

  • 如果邻接列表的长度超过3,就使用 nth_element 函数,++把第3大的元素放到 vs.begin() + 3 的位置,这样前3个元素就是最大的三个。++

  • 然后通过 vs.resize(3) ,将邻接列表的长度调整为3,从而只保留分数最大的前三个相邻节点


思考枚举点还是边

将问题放小,假设三个点怎么办

三个点则枚举中间点,再枚举两边最优

优化,++贪心每次选最大的权值
则一个点最多往外连三个点
++

写的时候可以借助提供的边一下子确定两个点,灵活运用点和边的关系

(这样就不用写四个循环和一堆判断了)

lc2874

预处理+单调性

class Solution {

public:

long long maximumTripletValue(vector<int>& nums)

{

//dan diao xin

int n=nums.size();

vector<int> max_left(n,0);

vector<int> max_right(n,0);

long long ret=LONG_MIN;

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

{

max_left[i]=max(max_left[i-1],nums[i-1]);

}

for(int i=n-2;i>0;i--)

{

max_right[i]=max(max_right[i+1],nums[i+1]);

}

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

{

ret=max(ret,(long long)(max_left[i]-nums[i])*max_right[i]);

}

return ret<0?0:ret;

}

};

lc2909

枚举j,记忆mn i &预处理mn k

class Solution {

public:

int minimumSum(vector<int>& nums)

{

int n=nums.size();

int mnl=nums[0];

vector<int> pmn(n,INT_MAX);

pmn[n-1]=nums[n-1];

for(int a=n-2;a>=0;a--)

{

pmn[a]=min(pmn[a+1],nums[a+1]);

//not include 0

}

int ret=INT_MAX;

for(int j=1;j<n;j++)

{

if(mnl<nums[j] && pmn[j]<nums[j])

{

ret=min(mnl+nums[j]+pmn[j],ret);

}

mnl=min(mnl,nums[j]);

}

return ret==INT_MAX?-1:ret;

}

};

lc447

hash

遍历每个点,计算它与其他点的距离

用hash统计各距离出现次数

再根据次数算出能组成的回旋镖数量并累加,最终得到所有回旋镖的总数

class Solution {

public:

int numberOfBoomerangs(vector<vector<int>>& points) {

int ans = 0;

unordered_map<int, int> cnt;

for (auto& p1 : points) {

cnt.clear();

for (auto& p2 : points) {

int d2 = (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]);

++ans += cnt[d2]++ * 2;++

}

}

return ans;

}

};

相关推荐
让我们一起加油好吗3 小时前
【基础算法】01BFS
数据结构·c++·算法·bfs·01bfs
孤狼灬笑3 小时前
机器学习十大经典算法解析与对比
人工智能·算法·机器学习
靠近彗星5 小时前
3.1 栈
数据结构·算法
sulikey5 小时前
一文彻底理解:如何判断单链表是否成环(含原理推导与环入口推算)
c++·算法·leetcode·链表·floyd·快慢指针·floyd判圈算法
Swift社区5 小时前
LeetCode 402 - 移掉 K 位数字
算法·leetcode·职场和发展
_码力全开_5 小时前
P1005 [NOIP 2007 提高组] 矩阵取数游戏
java·c语言·c++·python·算法·矩阵·go
墨染点香6 小时前
LeetCode 刷题【124. 二叉树中的最大路径和、125. 验证回文串】
算法·leetcode·职场和发展