• 阿里巴巴专场——第322场周赛题解


     

    目录

    模拟法:6253.回环句

    排序后模拟:6254. 划分技能点相等的团队

    BFS:6255. 两个城市间路径的最小分数

    BFS:6256. 将节点分成尽可能多的组


    模拟法:6253.回环句

     

    这道题直接按照题目的意思暴力模拟即可:

    1. class Solution {
    2. public:
    3. bool isCircularSentence(string sentence) {
    4. if (sentence[0] != sentence[sentence.size() - 1]) {
    5. return false;
    6. }
    7. for (int i = 0; i < sentence.size(); i++) {
    8. if (sentence[i] == ' ') {
    9. if (sentence[i - 1] != sentence[i + 1]) {
    10. return false;
    11. }
    12. }
    13. }
    14. return true;
    15. }
    16. };

    排序后模拟:6254. 划分技能点相等的团队

    先排序后,判断第i个和第n-i个加起来的和是否相等,如果不相等,直接返回-1结束。然后把这些加和,返回结果即可。

    1. class Solution {
    2. public:
    3. long long dividePlayers(vector<int>& skill) {
    4. sort(skill.begin(), skill.end());
    5. long long ans = 0;
    6. long long temp = skill[0] + skill[skill.size() - 1];
    7. for (int i = 0; i < skill.size() / 2; i++) {
    8. if (skill[i] + skill[skill.size() - i - 1] != temp) {
    9. return -1;
    10. } else {
    11. ans += skill[i] * skill[skill.size() - i - 1];
    12. }
    13. }
    14. return ans;
    15. }
    16. };

    BFS:6255. 两个城市间路径的最小分数

    1. class Solution {
    2. public:
    3. int minScore(int n, vector<vector<int>>& roads) {
    4. vector<vector<pair<int, int>>> arr(n);
    5. for (const auto& r : roads) {
    6. int u = r[0] - 1, v = r[1] - 1, d = r[2];
    7. arr[u].emplace_back(v, d);
    8. arr[v].emplace_back(u, d);
    9. }
    10. int ret = INT_MAX;
    11. vector<int> visit(n);
    12. queue<int> q;
    13. q.push(0);
    14. while (!q.empty()) {
    15. int f = q.front();
    16. q.pop();
    17. if (visit[f] != 0) {
    18. continue;
    19. }
    20. visit[f] = 1;
    21. for (auto [next, d] : arr[f]) {
    22. q.push(next);
    23. ret = min(ret, d);
    24. }
    25. }
    26. return ret;
    27. }
    28. };

    BFS:6256. 将节点分成尽可能多的组

     贴一个大佬的回答吧:

    1. class Solution {
    2. public:
    3. int magnificentSets(int n, vector<vector<int>> &edges) {
    4. vector<vector<int>> g(n);
    5. for (auto &e : edges) {
    6. int x = e[0] - 1, y = e[1] - 1;
    7. g[x].push_back(y);
    8. g[y].push_back(x);
    9. }
    10. int time[n], clock = 0; // time 充当 vis 数组的作用(避免在 BFS 内部重复创建 vis 数组)
    11. memset(time, 0, sizeof(time));
    12. auto bfs = [&](int start) -> int { // 返回从 start 出发的最大深度
    13. int depth = 0;
    14. time[start] = ++clock;
    15. vector<int> q = {start};
    16. while (!q.empty()) {
    17. vector<int> nxt;
    18. for (int x : q)
    19. for (int y : g[x])
    20. if (time[y] != clock) { // 没有在同一次 BFS 中访问过
    21. time[y] = clock;
    22. nxt.push_back(y);
    23. }
    24. q = move(nxt);
    25. ++depth;
    26. }
    27. return depth;
    28. };
    29. int8_t color[n]; memset(color, 0, sizeof(color));
    30. vector<int> nodes;
    31. function<bool(int, int8_t)> is_bipartite = [&](int x, int8_t c) -> bool { // 二分图判定,原理见视频讲解
    32. nodes.push_back(x);
    33. color[x] = c;
    34. for (int y : g[x])
    35. if (color[y] == c || color[y] == 0 && !is_bipartite(y, -c))
    36. return false;
    37. return true;
    38. };
    39. int ans = 0;
    40. for (int i = 0; i < n; ++i) {
    41. if (color[i]) continue;
    42. nodes.clear();
    43. if (!is_bipartite(i, 1)) return -1; // 如果不是二分图(有奇环),则无法分组
    44. // 否则一定可以分组
    45. int max_depth = 0;
    46. for (int x : nodes) // 枚举连通块的每个点,作为起点 BFS,求最大深度
    47. max_depth = max(max_depth, bfs(x));
    48. ans += max_depth;
    49. }
    50. return ans;
    51. }
    52. };

    https://leetcode.cn/problems/divide-nodes-into-the-maximum-number-of-groups/solution/mei-ju-qi-dian-pao-bfs-by-endlesscheng-s5bu/

     

  • 相关阅读:
    Linux网络和系统管理
    软考高级系统架构设计师系列案例考点专题四:嵌入式系统
    Day09-尚品汇-路由传递参数结合会话存储
    信息系统综合测试与管理
    【C++】超详细typedef用法和实例,看完不信你不会
    day09_面向对象_多态_static
    [计算机网络实验] TCP协议
    2022鹏城杯web
    unable to boot the simulator. domain nsposixerrordomain code 4
    基于单片机停车场环境监测系统仿真设计
  • 原文地址:https://blog.csdn.net/qq_41895747/article/details/128170250