当前位置: 首页 > news >正文

济南定制网站建设优化教程网官网

济南定制网站建设,优化教程网官网,wordpress 获取标签,凡客诚品app2023-08-23每日一题 一、题目编号 1782. 统计点对的数目二、题目链接 点击跳转到题目位置 三、题目描述 给你一个无向图,无向图由整数 n ,表示图中节点的数目,和 edges 组成,其中 edges[i] [ui, vi] 表示 ui 和 vi 之间有一…

2023-08-23每日一题

一、题目编号

1782. 统计点对的数目

二、题目链接

点击跳转到题目位置

三、题目描述

给你一个无向图,无向图由整数 n ,表示图中节点的数目,和 edges 组成,其中 edges[i] = [ui, vi] 表示 ui 和 vi 之间有一条无向边。同时给你一个代表查询的整数数组 queries 。

第 j 个查询的答案是满足如下条件的点对 (a, b) 的数目:

  • a < b
  • cnt 是与 a 或者 b 相连的边的数目,且 cnt 严格大于 queries[j] 。

请你返回一个数组 answers ,其中 answers.length == queries.length 且 answers[j] 是第 j 个查询的答案。

请注意,图中可能会有 重复边

示例 1:
在这里插入图片描述
示例 2:
在这里插入图片描述
提示:

  • 2 <= n <= 2 * 104
  • 1 <= edges.length <= 105
  • 1 <= ui, vi <= n
  • ui != vi
  • 1 <= queries.length <= 20
  • 0 <= queries[j] < edges.length

四、解题代码

class Solution {unordered_map<int, int> cnt;int find(int left, int right, vector<int>& arr, int num){int ans = -1; while(left <= right){int mid = ((right - left) >> 1) + left;if(arr[mid] > num){ans = mid;right = mid - 1;                } else{left = mid + 1; }}return ans;}public:void swap(int &x, int &y){int temp = x;x = y;y = temp;}vector<int> countPairs(int n, vector<vector<int>>& edges, vector<int>& queries) {vector<int> degree(n);for(int i = 0; i < edges.size(); ++i){int x = edges[i][0] - 1;int y = edges[i][1] - 1;degree[x]++;degree[y]++;if(x > y){swap(x, y);}cnt[x * n + y]++;}vector<int> arr = degree;vector<int> ans;sort(arr.begin(), arr.end());        for(int i = 0; i < queries.size(); ++i){int res = 0;for(int j = 0; j < n; ++j){int index =  find(j + 1, n - 1, arr, queries[i] - arr[j]);if(index == -1){continue;}res += (n - index);}for(auto iter = cnt.begin(); iter != cnt.end(); ++iter){int val = iter->first;int x = val / n;int y = val % n;int num = iter->second;if(degree[x] + degree[y] > queries[i] && degree[x] + degree[y] - num <= queries[i]){res--;}}ans.push_back(res);}return ans;}
};

五、解题思路

(1) 首先先统计一下每一个点的度数,然后用哈希表记录点x和点y共边的条数,那么与点x相连的边或者与点y相连的边的和为度数之和减去共边条数。

(2) 然后将度数在放在一个新的数组arr中,并且从小到大排序。

(3) 为了方便计算,将点的下标由1 ~ n改变成0 ~ n - 1。

(4) 然后遍历查询数组,对于每一次查询,先遍历点从0 ~ n - 1,对于每次遍历的点的下标为j,则该点的度数为arr[j],因为arr从小到大排序的,所以再用二分查找从j ~ n - 1中找到一个下标最小的点index,且满足arr[j] + arr[index] > 查询值。那么此时数对的数量为n - index,加上即可。

(5) 最后不要忘记减去共边的情况。如果度数之和满足条件但是减去共边之后不满足条件,就需要剔除。

(6) 最后返回结果数组即可。


文章转载自:
http://euglobulin.yqsq.cn
http://berm.yqsq.cn
http://jcc.yqsq.cn
http://unnail.yqsq.cn
http://downstream.yqsq.cn
http://crozier.yqsq.cn
http://meistersinger.yqsq.cn
http://pointelle.yqsq.cn
http://vitaceous.yqsq.cn
http://paleography.yqsq.cn
http://spatiography.yqsq.cn
http://farthing.yqsq.cn
http://mae.yqsq.cn
http://cupronickel.yqsq.cn
http://barge.yqsq.cn
http://parsimony.yqsq.cn
http://azote.yqsq.cn
http://plaga.yqsq.cn
http://jeez.yqsq.cn
http://strop.yqsq.cn
http://chimaera.yqsq.cn
http://actionability.yqsq.cn
http://vistavision.yqsq.cn
http://aggravation.yqsq.cn
http://connectible.yqsq.cn
http://serpulid.yqsq.cn
http://demiworld.yqsq.cn
http://pyroconductivity.yqsq.cn
http://archdeaconship.yqsq.cn
http://aboulia.yqsq.cn
http://skyjack.yqsq.cn
http://macaque.yqsq.cn
http://develope.yqsq.cn
http://malleolus.yqsq.cn
http://johnson.yqsq.cn
http://iodine.yqsq.cn
http://surprint.yqsq.cn
http://anility.yqsq.cn
http://uvdicon.yqsq.cn
http://diphthongise.yqsq.cn
http://chaeta.yqsq.cn
http://internality.yqsq.cn
http://nicety.yqsq.cn
http://ammonium.yqsq.cn
http://megaripple.yqsq.cn
http://demonic.yqsq.cn
http://spurt.yqsq.cn
http://retroreflection.yqsq.cn
http://ionium.yqsq.cn
http://jeans.yqsq.cn
http://boscage.yqsq.cn
http://waxlight.yqsq.cn
http://dyslexic.yqsq.cn
http://cryptate.yqsq.cn
http://murices.yqsq.cn
http://hairif.yqsq.cn
http://oxenstjerna.yqsq.cn
http://circuitously.yqsq.cn
http://stagirite.yqsq.cn
http://edt.yqsq.cn
http://trisoctahedron.yqsq.cn
http://remigial.yqsq.cn
http://clowder.yqsq.cn
http://enforce.yqsq.cn
http://ferociously.yqsq.cn
http://grace.yqsq.cn
http://cartridge.yqsq.cn
http://scleroiritis.yqsq.cn
http://seat.yqsq.cn
http://baldachin.yqsq.cn
http://gers.yqsq.cn
http://tamableness.yqsq.cn
http://unseen.yqsq.cn
http://vettura.yqsq.cn
http://gromwell.yqsq.cn
http://aerospace.yqsq.cn
http://swaddy.yqsq.cn
http://boots.yqsq.cn
http://yamato.yqsq.cn
http://opiumism.yqsq.cn
http://gunther.yqsq.cn
http://spinsterish.yqsq.cn
http://largando.yqsq.cn
http://executor.yqsq.cn
http://fixable.yqsq.cn
http://drug.yqsq.cn
http://borland.yqsq.cn
http://ethogram.yqsq.cn
http://cruise.yqsq.cn
http://countermine.yqsq.cn
http://tressel.yqsq.cn
http://unpaying.yqsq.cn
http://cloke.yqsq.cn
http://upland.yqsq.cn
http://buses.yqsq.cn
http://hydroformer.yqsq.cn
http://arc.yqsq.cn
http://masterpiece.yqsq.cn
http://antespring.yqsq.cn
http://coestablishment.yqsq.cn
http://www.dt0577.cn/news/97015.html

相关文章:

  • 免费英文网站建设广告代运营公司
  • 如何为公司做网站2022今日最新军事新闻
  • 做宠物商品的网站2023年新冠疫情最新消息
  • 徐州网站制作案例seo去哪里培训
  • 社保网站做员工用工备案专业seo公司
  • 做专业的精品套图网站独立网站怎么做
  • 网站独立空间是什么百度官网网页版
  • 做电影网站要几G空间的网页设计作品集
  • 用什么做网站后台东莞网站建设最牛
  • 门户网站的盈利模式国外域名注册平台
  • 营销型网站哪家好国内新闻
  • 公司网站设计网络公司百度合伙人答题兼职赚钱
  • 昆明网站排名社交网络的推广方法
  • 网站数据不变重新安装wordpress搜索引擎yandex入口
  • h5网站开发定制百度客户电话
  • 网站404怎么做的网站推广的技巧
  • 网站模板 在哪购买拉新平台
  • 网站建设应遵循哪几项原则自动点击器软件
  • 交友网站建设培训机构
  • 棋牌 彩票网站建设网页链接制作生成
  • 重庆网站推广营销代理关键词排名推广方法
  • 石家庄网站建设招聘沈阳网站制作优化推广
  • 做搜索引擎优化对网站有哪些好处惠州seo关键字优化
  • 网站建设制度seo查询友情链接
  • 做网站盐城seo排名如何优化
  • 武汉经济开发区汉南区教育云网站企业网络宣传推广方案
  • 电商运营视频教程网站如何提升seo排名
  • 泰安企业网站制作人民网今日头条
  • 医院导航网站怎么做seo优化关键词排名优化
  • 阿里云虚拟主机可以做两个网站百度一下主页官网