bzoj 3498: PA2009 Cakes【Mischief】

Reference: https://www.cnblogs.com/spfa/p/7495438.html
Why is the adjacency list TTTTTTTLE... can only use vector?
Sort the points according to the point weight from large to small, turn the undirected edge into the directed edge of the top-ranked point and connect it to the bottom-ranked point and record the degree d[u], and use the map to record the connection, so that you can Avoid repeated calculation
and traverse by ranking. Let u be traversed, and scan the adjacent point v of u. If the out-degree of v is less than the square root m, traverse all the adjacent points of v to determine whether it is connected to u; Take the adjacent point v of u, and use map to determine whether the current v is connected to the newly scanned v;
each time the judgment is successful, the point weight of u is added to ans, because the edge is always connected from the point with the larger point weight to the smaller point weight. point, so the value of the judged ternary ring is always the point weight of the current u.
As for why the complexity is right....O (metaphysics)...

#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;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324972157&siteId=291194637