hdu6184 (过题全靠抖

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tree__water/article/details/77867768
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+5;
int vis[N];
vector<int> q[N],low,up;
unordered_set<ll> mp;
unordered_map<ll,int> cnt;
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int lim=sqrt(1.0*m);
        mp.clear();low.clear();up.clear();cnt.clear();
        for(int i=1;i<=n;i++)
            q[i].clear(),vis[i]=0;
        for(int i=0;i<m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            q[u].push_back(v);
            q[v].push_back(u);
            mp.insert(1ll*u*n+v);
            mp.insert(1ll*v*n+u);
        }
        for(int i=1;i<=n;i++)
        {
            if(q[i].size()<=lim)
                low.push_back(i);
            else up.push_back(i);
        }
        for(int i=0;i<low.size();i++)
        {
            int u=low[i];
            vis[u]=1;
            for(int j=0;j<q[u].size();j++)
            {
                int v=q[u][j];
                if(vis[v])continue;
                for(int k=j+1;k<q[u].size();k++)
                {
                    int vv=q[u][k];
                    if(!vis[vv]&&mp.count(1ll*v*n+vv))
                        cnt[v<vv?1ll*v*n+vv:1ll*vv*n+v]++,cnt[u<vv?1ll*u*n+vv:1ll*vv*n+u]++,cnt[u<v?1ll*u*n+v:1ll*v*n+u]++;
                }
            }
        }
        for(int i=0;i<up.size();i++)
        {
            for(int j=i+1;j<up.size();j++)
            {
                int u=up[i],v=up[j];
                if(!mp.count(1ll*u*n+v))continue;
                for(int k=j+1;k<up.size();k++)
                {
                    int vv=up[k];
                    if(mp.count(1ll*v*n+vv)&&mp.count(1ll*u*n+vv))
                        cnt[v<vv?1ll*v*n+vv:1ll*vv*n+v]++,cnt[u<vv?1ll*u*n+vv:1ll*vv*n+u]++,cnt[u<v?1ll*u*n+v:1ll*v*n+u]++;}
            }
        }
        ll ans=0;
        for(unordered_map<ll,int> ::iterator i=cnt.begin();i!=cnt.end();i++)
            ans+=i->second*(i->second-1)/2;
        printf("%lld\n",ans);
    }
}
展开阅读全文

没有更多推荐了,返回首页