参考:https://www.cnblogs.com/spfa/p/7495438.html

为什么邻接表会TTTTTTTLE啊...只能用vector?

把点按照点权从大到小排序,把无向边变成排名靠前的点连向排名靠后的点的有向边并记录出度d[u],用map记录一下联通,这样可以避免重复计算

按排名遍历,设遍历到u点,扫u的邻接点v,如果v的出度小于根号m,就遍历v的所有邻接点,判断是否与u相连即可;否则,再次枚举u的邻接点v,用map判断当前v是否与新扫描的v相连;

每次判断成功都在ans里加上u的点权,因为边总是从点权大的点连向点权小的点,所以判断出的三元环的价值总是当前u的点权

至于为什么复杂度是对的.......O(玄学)吧......

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<cmath>
#include<vector>
using namespace std;
const int N=100005;
int n,m,d[N],cnt,h[N],lk[N],rk[N];
map<int,int>mp[N];
vector<int>g[N];
struct qwe
{
int ne,to;
}e[N<<2];
struct dian
{
int id,v;
}a[N];
bool cmp(const dian &a,const dian &b)
{
return a.v>b.v;
}
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
void add(int u,int v)
{
d[u]++;
mp[u][v]=1;
g[u].push_back(v);
}
int main()
{
n=read(),m=read();
for(int i=1;i<=n;i++)
a[i].v=read(),a[i].id=i;
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n;i++)
rk[a[i].id]=i;
for(int i=1;i<=m;i++)
{
int x=read(),y=read();
if(rk[x]<rk[y])
add(x,y);
else
add(y,x);
}
long long ans=0;
int bs=sqrt(m)+1;
for(int k=1;k<=n;k++)
{
int u=a[k].id,v=a[k].v;;
for(int i=0,len=g[u].size();i<len;i++)
lk[g[u][i]]=u;
for(int i=0,len=g[u].size();i<len;i++)
{
int y=g[u][i];
if(d[y]>bs)
{
for(int j=0;j<len;j++)
if(mp[y][g[u][j]])
ans+=v;
}
else
{
for(int j=0,le=g[y].size();j<le;j++)
if(lk[g[y][j]]==u)
ans+=v;
}
}
}
printf("%lld\n",ans);
return 0;
}

bzoj 3498: PA2009 Cakes【瞎搞】的更多相关文章

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

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

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

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

  3. BZOJ 3498 PA2009 Cakes

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

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

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

  5. BZOJ 4236: JOIOJI map瞎搞

    分别记录J,O,I,的个数 cnt[char][i] 表示处理到第i位,char的个数 显然当且仅当 cnt[J][i] - cnt[O][i] == cnt[J][j-1] - cnt[O][j-1 ...

  6. bzoj 2456: mode【瞎搞】

    这题加个#include都会MLE-- 神思路,每个数抵消宇哥和它不同的数,最后剩下的就是众数 #include<cstdio> int n,la,x,tot; int main() { ...

  7. URAL 1203. Scientific Conference(瞎搞)

    题目链接 本来觉得这不是经典的贪心吗..果断水一次,wa了,看了看discuss,发现貌似不好水,土土的DP了一下,复杂度很高了,又T了...然后想想单调队列,二分什么的...不好往上加,直接搞了标记 ...

  8. Codeforces Gym 100610 Problem H. Horrible Truth 瞎搞

    Problem H. Horrible Truth Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1006 ...

  9. B. Salty Fish Go! -期望题(瞎搞题)

    链接:https://www.nowcoder.com/acm/contest/104/B来源:牛客网 题意:A few days ago, WRD was playing a small game ...

随机推荐

  1. shit layui & select & re-render & bug

    shit layui https://www.layui.com/doc/modules/form.html#onselect https://www.layui.com/doc/element/fo ...

  2. hdu 3038带权并查集

    #include<stdio.h> #include<string.h> #define N  200100 struct node { int x,count; }pre[N ...

  3. PatentTips – EMC Virtual File System

    BACKGROUND OF THE INVENTION 1. Field of the Invention The present invention generally relates to net ...

  4. Validate Binary Search Tree(DFS)

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  5. How to force immediate stop of threads in Jmeter servers如何在jmeter执行完,立即停止jmeter

    https://stackoverflow.com/questions/38900315/how-to-force-immediate-stop-of-threads-in-jmeter-server ...

  6. pycharm内存不足时如何修改设置?

    Help->Find Action->(type "VM Options")->(Click)"Edit Custom VM Options" ...

  7. CentOS 7加强安全性:

    CentOS 7加强安全性:1. 更改 root 密码************************************************************************* ...

  8. [Unity3D]Unity3D游戏开发之从Unity3D到Eclipse

    ---------------------------------------------------------------------------------------------------- ...

  9. 扩展GCD 中国剩余定理(CRT) 乘法逆元模版

    extend_gcd: 已知 a,b (a>=0,b>=0) 求一组解 (x,y) 使得 (x,y)满足 gcd(a,b) = ax+by 以下代码中d = gcd(a,b).顺便求出gc ...

  10. KLT 光流

    一 光流 光流的概念是Gibson在1950年首先提出来的.它是空间运动物体在观察成像平面上的像素运动的瞬时速度,是利用图像序列中像素在时间域上的变化以及相邻帧之间的相关性来找到上一帧跟当前帧之间存在 ...