zoukankan      html  css  js  c++  java
  • BZOJ3498PA2009 Cakes——三元环

    题目描述

    N个点m条边,每个点有一个点权a。
    对于任意一个三元环(j,j,k)(i<j<k),它的贡献
    为max(ai,aj,ak) 
    求所有三元环的贡献和。
    N<100000,,m<250000。

    输入

    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. 

    输出

    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. 

    样例输入

    5 7
    1 5 3 4 2
    1 2
    2 3
    5 2
    4 3
    3 1
    1 4
    5 1

    样例输出

    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.
     
     
    三元环模板题,做法参见三元环讲解
    #include<cmath>
    #include<vector>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define ll long long
    using namespace std;
    int v[100010];
    int head[250010];
    int to[500010];
    int next[500010];
    int vis[100010];
    int s[100010];
    int n,m;
    int x,y;
    int tot;
    ll ans;
    vector<int>q[100010];
    void add(int x,int y)
    {
        tot++;
        next[tot]=head[x];
        head[x]=tot;
        to[tot]=y;
        s[x]++;
    }
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&v[i]);
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&x,&y);
            add(x,y);
            add(y,x);
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=head[i];j;j=next[j])
            {
                if(s[i]>s[to[j]]||(s[i]==s[to[j]]&&i<to[j]))
                {
                    q[i].push_back(to[j]);
                }
            }
        }
        for(int now=1;now<=n;now++)
        {
            int len=q[now].size();
            for(int i=0;i<len;i++)
            {
                vis[q[now][i]]=now;
            }
            for(int i=0;i<len;i++)
            {
                int point=q[now][i];
                int size=q[point].size();
                for(int j=0;j<size;j++)
                {
                    if(vis[q[point][j]]==now)
                    {
                        ans+=max(v[now],max(v[point],v[q[point][j]]));
                    }
                }
            }
        }
        printf("%lld",ans);
    }
  • 相关阅读:
    【原】ios打包ipa的四种实用方法(.app转.ipa)
    【原】Mac下统计任意文件夹中代码行数的工具——cloc
    【原+转】用CMake代替makefile进行跨平台交叉编译
    【原】iOS设计模式之:建造者模式Builder Pattern,用于改进初始化参数
    【原】Github系列之一:一起做仿天气类应用中的实时模糊效果LiveBlur
    【原】iOS:一种直接修改frame的某个属性的方法
    【原】iOS优秀开源项目总结
    【原】你真的懂iOS的autorelease吗?
    【原】iOS容易造成循环引用的三种场景,就在你我身边!
    Failure [DELETE_FAILED_INTERNAL_ERROR]之后rm apk卸载
  • 原文地址:https://www.cnblogs.com/Khada-Jhin/p/10129383.html
Copyright © 2011-2022 走看看