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的更多相关文章

  1. BZOJ 3498: PA2009 Cakes 一类经典的三元环计数问题

    首先引入一个最常见的经典三元环问题. #include <bits/stdc++.h> using namespace std; const int maxn = 100005; vect ...

  2. [BZOJ 1879][SDOI 2009]Bill的挑战 题解(状压DP)

    [BZOJ 1879][SDOI 2009]Bill的挑战 Description Solution 1.考虑状压的方式. 方案1:如果我们把每一个字符串压起来,用一个布尔数组表示与每一个字母的匹配关 ...

  3. [BZOJ 1563] [NOI 2009] 诗人小G(决策单调性)

    [BZOJ 1563] [NOI 2009] 诗人小G(决策单调性) 题面 一首诗包含了若干个句子,对于一些连续的短句,可以将它们用空格隔开并放在一行中,注意一行中可以放的句子数目是没有限制的.小 G ...

  4. [BZOJ 1412][ZJOI 2009] 狼和羊的故事

    题目大意 有一个 (n times m) 的网格,每一个格子上是羊.狼.空地中的一种,羊和狼可以走上空地.现要在格子边上建立围栏,求把狼羊分离的最少围栏数. (1 leqslant n, ; m le ...

  5. BZOJ 3498 PA2009 Cakes(三元环处理)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3498 [题目大意] N个点m条边,每个点有一个点权a. 对于任意一个三元环(j,j,k ...

  6. BZOJ 3498 PA2009 Cakes

    本题BZOJ权限题,但在bzojch上可以看题面. 题意: N个点m条无向边,每个点有一个点权a. 对于任意一个三元环(i,j,k)(i<j<k),它的贡献为max(ai,aj,ak) 求 ...

  7. BZOJ.3498.[PA2009]Cakes(三元环 枚举)

    题目链接 感觉我可能学的假的(复杂度没问题,但是常数巨大). 一个比较真的说明见这儿:https://czyhe.me/blog/algorithm/3-mem-ring/3-mem-ring/. \ ...

  8. Bzoj 3498 Cakes(三元环)

    题面(权限题就不放题面了) 题解 三元环模板题,按题意模拟即可. #include <cstdio> #include <cstring> #include <vecto ...

  9. bzoj 3498: PA2009 Cakes【瞎搞】

    参考:https://www.cnblogs.com/spfa/p/7495438.html 为什么邻接表会TTTTTTTLE啊...只能用vector? 把点按照点权从大到小排序,把无向边变成排名靠 ...

随机推荐

  1. Luogu P3321 [SDOI2015]序列统计

    一道不错的多项式好题.还涉及了一些数论内容. 首先我们看到题目是求乘积模\(m\)的方案数,考虑到这种方案数我们一般都可以用生成函数来做. 但显然卷积的下标有加(FFT,NTT等)有位运算(FWT)但 ...

  2. Java Statement PK PrepareStatement

    PreparedStatement是用来执行SQL查询语句的API之一,Java提供了 Statement.PreparedStatement 和 CallableStatement三种方式来执行查询 ...

  3. CAS5.X 集群配置 初版

    基础版 cas-overlay  pom.xml <?xml version="1.0" encoding="UTF-8"?> <projec ...

  4. FineUIMvc随笔(7)扩展通知对话框(显示多个不重叠)

    声明:FineUIMvc(基础版)是免费软件,本系列文章适用于基础版. 这篇文章我们将改造 FineUIMvc 默认的通知对话框,使得同时显示多个也不会重叠.并提前出一个公共的JS文件,供大家使用. ...

  5. Nodejs mongoose 详解

    前言 Mongoose 是在nodejs环境下,对mongodb进行便捷操作的对象模型工具.本文介绍解(翻)密(译)Mongoose插件. Schema 开始我们就要讲到Schema,一个Schema ...

  6. .net core CKEditor 图片上传

    最近在玩 asp.net core,不想用UEditor,想使用CKEditor.故需要图片上传功能. 废话不多说,先上效果图: CKEditor 前端代码: <text id="co ...

  7. NLP之——Word2Vec详解

    2013年,Google开源了一款用于词向量计算的工具--word2vec,引起了工业界和学术界的关注.首先,word2vec可以在百万数量级的词典和上亿的数据集上进行高效地训练:其次,该工具得到的训 ...

  8. Django 中的 cookie 和 session

    一.cookie 由于HTTP协议是无状态的,而服务器端的业务必须是要有状态的.Cookie诞生的最初目的是为了存储web中的状态信息,以方便服务器端使用.比如判断用户是否是第一次访问网站.目前最新的 ...

  9. haoop笔记

    : //:什么是hadoop? hadoop是解决大数据问题的一整套技术方案 :hadoop的组成? 核心框架 分布式文件系统 分布式计算框架 分布式资源分配框架 hadoop对象存储 机器计算 :h ...

  10. PV、TPS、QPS计算公式(转)

    英文解释: PV=page viewTPS=transactions per secondQPS=queries per secondRPS=requests per second RPS=并发数/平 ...