[BZOJ 3498] [PA 2009] Cakes
Description
\(n\) 个点 \(m\) 条边,每个点有一个点权 \(a_i\)。
对于任意一个三元环 \((i,j,k)(i<j<k)\),它的贡献为 \(\max(a_i,a_j,a_k)\),求所有三元环的贡献和。
Input
The first line of the standard input contains two integers n and m (1<=N<=100000,1<=M<=250000) separated by a single space and denoting the number of confectioners at the convention and the number of pairs of them that like each other. The participants of the convention are numbered from 1 to N, The second line contains n integers pi (1<=Pi<=1000000) separated by single spaces and denoting the requirements of respective confectioners for flour (in decagrams). The following m lines contain data about pairs of contestants that like each other. Each of these lines contains two integers ai and bi (1<=ai,bi<=n Ai<>Bi) separated by a single space. They denote that confectioners ai and bi like each other. We assume that all pairs of participants of the convention apart from the ones listed in the input would not like to bake cakes together. Each pair of confectioners appears at most once in the input.
Output
The first and only line of the standard output should contain a single integer - the quantity of flour that will be used by all teams in total, in decagrams.
Sample Input
5 7
1 5 3 4 2
1 2
2 3
5 2
4 3
3 1
1 4
5 1
Sample Output
14
Explanation of the example. The following three-person teams: (1,2,3),(1,2,5) and (1,3,4)require 5, 5 and 4 decagrams of flour to bake the cakes. In total 5+5+4=14 decagrams of flour are required.
HINT
\(n\le100000,m\le250000\)
Solution
〖一〗
将节点按照度数 \(\le \sqrt m\) 和 \(> \sqrt m\) 分为两类,由于只有 \(m\) 条边,因此度数 \(> \sqrt m\) 的点的个数为 \(O(\sqrt m)\)。
对于存在度数 \(\le\sqrt m\) 的点的三元环,枚举度数 \(\le\sqrt m\) 的点,再枚举它的两条边,判断这两条边指向的点是否有边相连,这里枚举第一条边相当于枚举图中的所有边,第二条边是度数复杂度,因此时间复杂度为 \(O(m\sqrt m)\)。
对于点的度数均 \(> \sqrt m\) 的三元环,三重循环枚举度数 \(> \sqrt m\) 的点,复杂度 \(O((\sqrt m)^3)=O(m\sqrt m)\)。
判断两个点之间是否有边相连用 \(map<pair,bool>\) 判断就行了。
〖二〗
还有一个黑科技做法,将每条边定向,由度数大的点指向度数小的点,如果度数相同就由编号小的点指向编号大的点,显然这样得到的图是不存在环的。
枚举每个点 \(i\),再枚举它的出边,将出边指向的点 \(j\) 打上标记,再枚举点 \(j\) 的出边,如果此时出边指向的点 \(k\) 被打了标记,那么 \(i,j,k\) 就组成了一个三元环。这样每个三元环只会被统计一次。
考虑证明时间复杂度。如果一个点的出度 \(\le \sqrt m\),指向它的点最多有 \(n\) 个,复杂度为 \(O(n\sqrt m)\);如果一个点的出度 \(>\sqrt m\),指向它的点的出度一定比它大,最多有 \(\sqrt m\) 个,复杂度为 \(O(m\sqrt m)\)。
〖三〗
HDU 6184 Counting Stars 是求 \(V=(A,B,C,D),E=(AB,BC,CD,DA,AC)\) 的子图个数。
设 \(cnt[i]\) 表示第 \(i\) 条边在多少个三元环中出现过,求三元环的时候顺便统计出来即可,最后枚举每条边,将 \(\large\binom{cnt[i]}{2}\) 计入答案。
Code
#include <cstdio>
#include <algorithm>
const int N = 100005, M = 250005;
struct Pair { int u, v; } p[M];
struct Edge { int v, nxt; } e[M];
int a[N], d[N], head[N], tot, f[N], q[N];
long long ans;
int read() {
int x = 0; char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
return x;
}
void adde(int u, int v) {
e[++tot].nxt = head[u], head[u] = tot, e[tot].v = v;
}
int main() {
int n = read(), m = read();
for (int i = 1; i <= n; ++i) a[i] = read();
for (int i = 1; i <= m; ++i) p[i].u = read(), p[i].v = read(), ++d[p[i].u], ++d[p[i].v];
for (int i = 1; i <= m; ++i)
if (d[p[i].u] == d[p[i].v]) p[i].u < p[i].v ? adde(p[i].u, p[i].v) : adde(p[i].v, p[i].u);
else d[p[i].u] > d[p[i].v] ? adde(p[i].u, p[i].v) : adde(p[i].v, p[i].u);
for (int i = 1; i <= n; ++i) {
int t = 0;
for (int j = head[i]; j; j = e[j].nxt) f[e[j].v] = i, q[++t] = e[j].v;
for (int j = 1; j <= t; ++j)
for (int k = head[q[j]]; k; k = e[k].nxt)
if (f[e[k].v] == i) ans += std::max(a[i], std::max(a[q[j]], a[e[k].v]));
}
printf("%lld\n", ans);
return 0;
}
[BZOJ 3498] [PA 2009] Cakes的更多相关文章
- BZOJ 3498: PA2009 Cakes 一类经典的三元环计数问题
首先引入一个最常见的经典三元环问题. #include <bits/stdc++.h> using namespace std; const int maxn = 100005; vect ...
- [BZOJ 1879][SDOI 2009]Bill的挑战 题解(状压DP)
[BZOJ 1879][SDOI 2009]Bill的挑战 Description Solution 1.考虑状压的方式. 方案1:如果我们把每一个字符串压起来,用一个布尔数组表示与每一个字母的匹配关 ...
- [BZOJ 1563] [NOI 2009] 诗人小G(决策单调性)
[BZOJ 1563] [NOI 2009] 诗人小G(决策单调性) 题面 一首诗包含了若干个句子,对于一些连续的短句,可以将它们用空格隔开并放在一行中,注意一行中可以放的句子数目是没有限制的.小 G ...
- [BZOJ 1412][ZJOI 2009] 狼和羊的故事
题目大意 有一个 (n times m) 的网格,每一个格子上是羊.狼.空地中的一种,羊和狼可以走上空地.现要在格子边上建立围栏,求把狼羊分离的最少围栏数. (1 leqslant n, ; m le ...
- BZOJ 3498 PA2009 Cakes(三元环处理)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3498 [题目大意] N个点m条边,每个点有一个点权a. 对于任意一个三元环(j,j,k ...
- BZOJ 3498 PA2009 Cakes
本题BZOJ权限题,但在bzojch上可以看题面. 题意: N个点m条无向边,每个点有一个点权a. 对于任意一个三元环(i,j,k)(i<j<k),它的贡献为max(ai,aj,ak) 求 ...
- BZOJ.3498.[PA2009]Cakes(三元环 枚举)
题目链接 感觉我可能学的假的(复杂度没问题,但是常数巨大). 一个比较真的说明见这儿:https://czyhe.me/blog/algorithm/3-mem-ring/3-mem-ring/. \ ...
- Bzoj 3498 Cakes(三元环)
题面(权限题就不放题面了) 题解 三元环模板题,按题意模拟即可. #include <cstdio> #include <cstring> #include <vecto ...
- bzoj 3498: PA2009 Cakes【瞎搞】
参考:https://www.cnblogs.com/spfa/p/7495438.html 为什么邻接表会TTTTTTTLE啊...只能用vector? 把点按照点权从大到小排序,把无向边变成排名靠 ...
随机推荐
- ML.NET 示例:聚类之客户细分
写在前面 准备近期将微软的machinelearning-samples翻译成中文,水平有限,如有错漏,请大家多多指正. 如果有朋友对此感兴趣,可以加入我:https://github.com/fei ...
- Xamarin.Forms 3.0的新特性
近期因为工作关系开始使用Xamarin,翻译了两篇国外的介绍3.0新特性的文章,供大家参考. 第一篇文章来自Xamarin官网,原文地址:https://blog.xamarin.com/xamari ...
- 朱晔的互联网架构实践心得S1E3:相辅相成的存储五件套
朱晔的互联网架构实践心得S1E3:相辅相成的存储五件套 [下载本文PDF进行阅读] 这里所说的五件套是指关系型数据库.索引型数据库.时序型数据库.文档型数据库和缓存型数据库. 上图显示了一套读写服务搭 ...
- 什么是CLOS架构?
Clos架构,诞生于1952年,是由一位叫Charles Clos的人提出的,所以它并不是一个新的概念. 这个架构主要描述了一种多级电路交换网络的结构.Clos最大的优点就是对Crossbar结构的改 ...
- koa-router
为了处理URL,我们需要引入koa-router这个middleware,让它负责处理URL映射. 我们把上一节的hello-koa工程复制一份,重命名为url-koa. 先在package.json ...
- CF每日一练 Codeforces Round #520 (Div. 2)
比赛过程总结:过程中有事就玩手机了,后面打的状态不是很好,A题理解错题意,表明了内心不在状态,B题想法和思路都是完全正确的,但是并没有写出来,因为自己代码能力不强,思路不是特别清晰,把代码后面写乱了, ...
- Python—包介绍
包(Package) 当你的模块文件越来越多,就需要对模块文件进行划分,比如把负责跟数据库交互的都放一个文件夹,把与页面交互相关的放一个文件夹, . └── my_proj ├── crm #代码目录 ...
- Vue使用的一些实例
1.实现歌曲的点击切换. <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- ES7的新特性
ES7的新特性 ES7 特性: 1.Array.prototype.includes2.Exponentiation Operator(求幂运算) 一,Array.prototype.includes ...
- WPF中任务栏只显示主窗口
我们在用WPF开发的时候,常常会遇到在主窗口打开的情况下,去显示子窗口,而此时任务栏同时显示主窗口与子窗口.这样看起来很不美观.所以在弹出子窗口之前,设置它的几个相应属性,便不会出现这种问题了. // ...