[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? 把点按照点权从大到小排序,把无向边变成排名靠 ...
随机推荐
- .NET Core 中的路径问题
NET Core 应用程序相对于以前的.NET Framework 应用程序在启动运行的方式上有一定的差异,今天就来谈一谈这个获取应用程序启动路径的问题. 1.工作路径 WorkingDirector ...
- Java核心数据结构(List,Map,Set)原理与使用技巧
JDK提供了一组主要的数据结构实现,如List.Map.Set等常用数据结构.这些数据都继承自 java.util.Collection 接口,并位于 java.util 包内. 1.List接口 最 ...
- SpringBoot整合Mybatis使用注解或XML的方式开发
2018-6-4 补充mybatis-spring-boot注解的使用 1.导包 只需要再导入mysql+mybatis两个包 <dependency> <groupId>or ...
- 2小时学会Spring Boot(IDE:eclipse)
一:安装STS插件 官网下载:点此下载STS 注意:STS版本必须与eclipse版本对应 安装教程:http://blog.csdn.net/cryhelyxx/article/details/53 ...
- redis-trib.rb命令详解
redis-trib.rb是官方提供的Redis Cluster的管理工具,无需额外下载,默认位于源码包的src目录下,但因该工具是用ruby开发的,所以需要准备相关的依赖环境. 准备redis-tr ...
- Rollup处理并打包JS文件项目实例
关于Rollup rollup是一款用来es6模块打包代码的构建工具(支持css和js打包).当我们使用ES6模块编写应用或者库时,它可以打包成一个单独文件提供浏览器和Node.js来使用. 它的优点 ...
- 网络编程-C/S架构
什么是C/S架构 C指的是client(客户端软件),S指的是Server(服务端软件),本章的重点就是教大家写一个C/S架构的软件,实现服务端软件与客户端软件基于网络通信. 计算机基础知识 作为应用 ...
- Python—反射
反射 1 什么是反射 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省).这一概念的提出很快引发了计算机科学领域关于应用反射性的研究.它 ...
- 【转】ubuntu 双机热备
1.关于软件安装 sudo apt-get install libssl-dev sudo apt-get install openssl sudo apt-get install libpopt-d ...
- PEP 8 python编程规范
一 代码编排 缩进.4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格. 每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换行点要在操作符的后边敲回车. 类和to ...