题目链接:

http://codeforces.com/problemset/problem/155/E

E. Double Profiles

time limit per test 3 seconds
memory limit per test 256 megabytes
#### 问题描述
> 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 ≠ i, k ≠ 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.
#### 输入
> The first line contains two space-separated integers n and m (1 ≤ n ≤ 106, 0 ≤ 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.
#### 输出
> 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.
#### 样例
> **sample input**
> 4 1
> 1 3
>
> **sample output**
> 2

题意

给你一个n个点m条边的无向图,让你求一些顶点对(无序)(u,v),满足对于任意的k(k!=u,v),k要么到两个点都有边,要么到两个点都没边,对于(u,v)本身可以存在边也可以不存在。

题解

这题如果你想到用hash,思路就会很直观了!

对于顶点u,我们把它的所有相邻的点(不包括它自己)都存起来,hash成一个x进制数ha[u](每个位对应一个节点,用加权的方式加起来)

接下来把顶点对分成两类:

1、顶点对之间没有边相邻:

如果两个顶点u,v它们的ha[u]=ha[v](即它们的相邻顶点集完全一样!),那么它们就是满足的顶点对。

2、顶点对之间没有边相邻:

如果ha[u]+u=ha[v]+v(u会出现在ha[v]里面,v会出现在ha[u]里面),同理,它们也满足条件。

对于有边的,一条一条统计。没边的吧ha数组排个序,扫一遍就可以了。

代码

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
#define X first
#define Y second
#define mp make_pair
using namespace std; const int maxn=1e6+10;
typedef unsigned __int64 LL; const LL P=123;
int n,m; LL pw[maxn];
LL ha[maxn];
void pre(){
memset(ha,0,sizeof(ha));
pw[0]=1;
for(int i=1;i<maxn;i++) pw[i]=pw[i-1]*P;
} int main(){
pre();
scanf("%d%d",&n,&m);
vector<pair<int,int> > egs;
for(int i=0;i<m;i++){
int u,v;
scanf("%d%d",&u,&v);
egs.push_back(mp(u,v));
ha[u]+=pw[v];
ha[v]+=pw[u];
}
LL ans=0;
for(int i=0;i<m;i++){
int u=egs[i].X,v=egs[i].Y;
if(ha[u]+pw[u]==ha[v]+pw[v]) ans++;
}
sort(ha+1,ha+n+1);
for(int i=1;i<n;i++){
if(ha[i]==ha[i+1]){
int ed=i;
while(ed+1<=n&&ha[ed]==ha[ed+1]) ed++;
LL len=ed-i+1;
ans+=len*(len-1)/2;
i=ed;
}
}
printf("%I64d\n",ans);
return 0;
}

Codeforces Round #109 (Div. 2) E. Double Profiles hash的更多相关文章

  1. 贪心 Codeforces Round #109 (Div. 2) B. Combination

    题目传送门 /* 贪心:按照能选的个数和点数降序排序,当条件不符合就break,水题啊! */ #include <cstdio> #include <algorithm> # ...

  2. Codeforces Round #109 (Div. 1) 题解 【ABC】

    A - Hometask 题意:给你一个字符串,然后再给你k个禁止挨在一起的字符串,问你最少删除多少个字符串,使得不会有禁忌的字符串对挨在一起.题目保证每个字符最多出现在一个禁忌中. 题解:由于每个字 ...

  3. codeforces水题100道 第十九题 Codeforces Round #109 (Div. 2) A. I_love_%username% (brute force)

    题目链接:http://www.codeforces.com/problemset/problem/155/A题意:找到当前最大值或者最小值出现的次数.“当前”的意思差不多是a[i]大于所有a[j]( ...

  4. Codeforces Round #109 (Div. 2) A. I_love_%username%【打擂台算法/满足当前数字在已经出现的数字的最大值和最小值之间的个数】

    A. I_love_%username% time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  5. Codeforces Round #215 (Div. 2) D题(离散化+hash)

    D. Sereja ans Anagrams time limit per test 1 second memory limit per test 256 megabytes input standa ...

  6. Codeforces Round #FF (Div. 2) A. DZY Loves Hash

    DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert n numbers, in the ...

  7. Codeforces Round #272 (Div. 2) 题解

    Codeforces Round #272 (Div. 2) A. Dreamoon and Stairs time limit per test 1 second memory limit per ...

  8. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  9. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

随机推荐

  1. 软件工程 speedsnail 冲刺6

    2015-5-10 完成任务:学习了黑马android教学视频13.14.15集,记分功能: 遇到问题: 问题1 android native method not found 解决1 没有解决 明日 ...

  2. [leetcode]_Longest Substring Without Repeating Characters

    问题:求一个字符串中最长不重复子串的长度. 直接思路:以每个字符为出发计算最长不重复子串.TLE.O(n2),HashMap存储字符出现的位置. 代码: public int lengthOfLong ...

  3. HTML5+CSS3前端开发资源整合

    HTML5+CSS3前端开发资源整合   推个广告 个人网站:http://www.51pansou.com HTML5视频下载:HTML5视频 HTML5源码下载:HTML5源码 meta相关: & ...

  4. 让TextView出现跑马灯效果

    只需要在TextView中添加一些属性即可: <?xml version="1.0" encoding="utf-8"?> <LinearLa ...

  5. for xml path('') 引发的数据不完整

    When you read Extensible Markup Language (XML) data from Microsoft SQL Server by using the SqlDataRe ...

  6. 第一节:CLR寄宿

    本系列文章来自 CLR VIA C# .NET FrameWork在Microsoft  Windows平台的顶部运行.这意味着.NET必须用Windows可以理解的技术来构建.首先,所有的托管模块和 ...

  7. spring aop 使用注解方式总结

    spring aop的注解方式:和xml的配置方式略有区别,详细如下: 1.首先还是建立需要的切面类:切面类里面定义好切点配置,以及所有的需要实现的通知方法. /** * */ package com ...

  8. Erlang generic standard behaviours -- gen

    在分析 gen_server (或者是gen_fsm )之前,首先应该弄明白,gen 这个module . -module(gen). -compile({inline,[get_node/1]}). ...

  9. mvc中使用knockoutjs和ajax

    虽然说knockoutjs 官网上写的非常的清楚!但是像我这样的英语呕吐患者,真是虐心啊!今天我写下做个记录,也为那些初次使用的同学给予帮助, 首先我说一下今天我说的内容只是应用不做原理探究,如果没有 ...

  10. php5调用web service (笔者测试成功)

    转自:http://www.cnblogs.com/smallmuda/archive/2010/10/12/1848700.html 感谢作者分享 工作中需要用php调用web service接口, ...