[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? 把点按照点权从大到小排序,把无向边变成排名靠 ...
随机推荐
- SpringBoot集成Apache Shiro
笔者因为项目转型的原因,对Apache Shiro安全框架做了一点研究工作,故想写点东西以便将来查阅.之所以选择Shiro也是看了很多人的推荐,号称功能丰富强大,而且易于使用.实践下来的确如大多数人所 ...
- Python-常见面试题-持续更新
1.请你简要介绍一下Python的生成器是什么 答:Python生成器是一个返回可以迭代对象的函数,可以被用作控制循环的迭代行为. 生成器类似于返回值为数组的一个函数,这个函数可以接受参数,可以被调用 ...
- 论一类每次修改log个结点更新的线段树标记方法
楼房重建(BZOJ2957) 多次询问一个区间中大于区间内这个数之前所有数的数的数量. 每个线段树结点维护该节点的答案c和区间内最大值m.假设有函数get(x,cm)=结点x中答案>cm的长度. ...
- UVA 10820 欧拉函数模板题
这道题就是一道简单的欧拉函数模板题,需要注意的是,当(1,1)时只有一个,其他的都有一对.应该对欧拉函数做预处理,显然不会超时. #include<iostream> #include&l ...
- uva11300 分金币(中位数)
来源:https://vjudge.net/problem/UVA-11300 题意: 有n个人围成一圈,每个人有一定数量的金币,每次只能挪动一个位置,求挪动的最少金币使他们平分金币 题解: 蓝书p6 ...
- 最全的Django入门及常用配置
Django 常用配置 Django 安装 pipx install django x 为python解释器版本2 or 3 如果你想安装指定版本的django,使用pip install djang ...
- Linux之hosts文件
一.序言: 今天同事部署环境遇到问题, 原因1:修改了主机名,在/etc/hosts文件中加了3台集群的ip和主机名,但是将默认的前两行也改了,没注意看改了哪里, 现象: 1.zookeeper单台可 ...
- Requires: libc.so.6(GLIBC_2.14)(64bit)
centos6 - CentOS 6 - libc.so.6(GLIBC_2.14)(64bit) is needed by - Server Faulthttps://serverfault.com ...
- Jenkins redeploy artifacts
jenkins redeploy artifacts 按钮 - 开源中国https://www.oschina.net/question/3045293_2247829 Jenkins 构建失败后通过 ...
- 前端开发之jQuery库
使用jquery开发的时候,如果我们不想使用自己的jquery文件,那么可以引用现成的地址.方便日常开发使用 jquery-2.0以上版本 (注!不再支持IE 6/7/8) jquery-2.0.0百 ...