zoukankan      html  css  js  c++  java
  • CodeForces

    You have been offered a job in a company developing a large social network. Your first task is connected with searching profiles that most probably belong to the same user.

    The social network contains n registered profiles, numbered from 1 to n. Some pairs there are friends (the "friendship" relationship is mutual, that is, if i is friends with j, then j is also friends with i). Let's say that profiles i and j (i ≠ j) are doubles, if for any profile k (k ≠ ik ≠ j) one of the two statements is true: either k is friends with i and j, or k isn't friends with either of them. Also, i and j can be friends or not be friends.

    Your task is to count the number of different unordered pairs (i, j), such that the profiles i and j are doubles. Note that the pairs are unordered, that is, pairs (a, b) and (b, a) are considered identical.

    Input

    The first line contains two space-separated integers n and m (1 ≤ n ≤ 1060 ≤ m ≤ 106), — the number of profiles and the number of pairs of friends, correspondingly.

    Next m lines contains descriptions of pairs of friends in the format "v u", where v and u (1 ≤ v, u ≤ n, v ≠ u) are numbers of profiles that are friends with each other. It is guaranteed that each unordered pair of friends occurs no more than once and no profile is friends with itself.

    Output

    Print the single integer — the number of unordered pairs of profiles that are doubles.

    Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the %I64d specificator.

    Examples

    Input
    3 3
    1 2
    2 3
    1 3
    Output
    3
    Input
    3 0
    Output
    3
    Input
    4 1
    1 3
    Output
    2

    Note

    In the first and second sample any two profiles are doubles.

    In the third sample the doubles are pairs of profiles (1, 3) and (2, 4).

    题意:给定无向图,问有多少对点对,在不考虑它们之间的连边情况下,满足它们的直接连边的情况相同。

    思路:用hash保存每个点的连边情况,然后排序得到hash值,相同的一并处理。

    有两类:第一类是点对间有边,那么我们需要向自己加一条边,那么它们的连边情况是相同的。

    第二类是点对间无边,直接处理即可。

    #include<bits/stdc++.h>
    #define rep(i,a,b) for(int i=a;i<=b;i++)
    #define uint unsigned int
    using namespace std;
    const int maxn=2000010;
    const int seed=1331;
    const int seed2=131;
    uint p[maxn],p2[maxn];long long ans;
    pair<uint,uint>h[maxn];
    int main()
    {
        int N,M,u,v;
        scanf("%d%d",&N,&M); p[0]=1; p2[0]=1;
        rep(i,1,N) p[i]=p[i-1]*seed;
        rep(i,1,N) p2[i]=p2[i-1]*seed2;
        rep(i,1,N) h[i].first=p[i],h[i].second=p2[i];
        rep(i,1,M){
            scanf("%d%d",&u,&v);
            h[u].first+=p[v]; h[v].first+=p[u];
            h[u+N].first+=p[v]; h[v+N].first+=p[u];
            h[u].second+=p2[v]; h[v].second+=p2[u];
            h[u+N].second+=p2[v]; h[v+N].second+=p2[u];
        }
        sort(h+1,h+N+N+1);
        for(int i=1;i<=N+N;i++){
            int j=i;
            while(j+1<=N+N&&h[j+1]==h[i]) j++;
            ans+=(long long)(j-i+1)*(j-i)/2;
            i=j;
        }
        printf("%I64d
    ",ans);
        return 0;
    }
  • 相关阅读:
    基于Haproxy+Keepalived构建高可用负载均衡集群
    基于 Haproxy 构建负载均衡集群
    shell for循环练习题99乘法表
    帮软件同事写的vsftpd服务虚拟用户管理脚本
    sed文件处理练习题
    判断ssh登录密码验证错误超过5次的IP被拉黑
    使用shell中数组功能生成自己的手机号
    利用Crontab设置每个月第一个周六的17:30执行/opt/shell.sh 脚本
    Tomcat 项目代码上线步骤详解
    Jar/War/Ear等包的作用与区别详解
  • 原文地址:https://www.cnblogs.com/hua-dong/p/9514734.html
Copyright © 2011-2022 走看看