Codeforces 154C Double Profiles
2016-07-20 19:01
281 查看
题意:
有n个点,m条边,问有多少个点对满足除它们之外的其他所有的点对这两个点的连接情况是一样的。
思路:
对于每个合要求的点对来说,它们的连接情况是一样的,也就是连接的点是一样的。那么符合要求的点对只有两种情况,1是对称,2是3个点的环。我们只需要给每个点一个特定的值,若两个点相连,那么两边都加上对方的那个值,最后判断一下有几个值一样的,然后加上从里面选2个的方法数。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef __int64 LL;
const int MAX=1e6+5;
LL ha[MAX],hb[MAX],va[MAX];
int n,m;
void init(){
LL mod=233333;
va[1]=1;
for(int i=2;i<=n;i++){
va[i]=va[i-1]*mod;
}
}
int main(){
scanf("%d%d",&n,&m);
init();
memset(ha,0,sizeof(ha));
memset(hb,0,sizeof(hb));
while(m--){
int u,v;
scanf("%d%d",&u,&v);
ha[u]+=va[v];
ha[v]+=va[u];
}
for(int i=1;i<=n;i++){
hb[i]=ha[i]+va[i];
}
LL ans=0;
sort(ha+1,ha+1+n);
sort(hb+1,hb+1+n);
LL now=ha[1],cnt=1;
for(int i=2;i<=n;i++){
if(now==ha[i]) cnt++;
else{
ans+=(cnt*(cnt-1))/2;
now=ha[i];
cnt=1;
}
}
ans+=(cnt*(cnt-1))/2;
now=hb[1],cnt=1;
for(int i=2;i<=n;i++){
if(now==hb[i]) cnt++;
else{
ans+=(cnt*(cnt-1))/2;
now=hb[i];
cnt=1;
}
}
ans+=(cnt*(cnt-1))/2;
printf("%I64d\n",ans);
return 0;
}
有n个点,m条边,问有多少个点对满足除它们之外的其他所有的点对这两个点的连接情况是一样的。
思路:
对于每个合要求的点对来说,它们的连接情况是一样的,也就是连接的点是一样的。那么符合要求的点对只有两种情况,1是对称,2是3个点的环。我们只需要给每个点一个特定的值,若两个点相连,那么两边都加上对方的那个值,最后判断一下有几个值一样的,然后加上从里面选2个的方法数。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef __int64 LL;
const int MAX=1e6+5;
LL ha[MAX],hb[MAX],va[MAX];
int n,m;
void init(){
LL mod=233333;
va[1]=1;
for(int i=2;i<=n;i++){
va[i]=va[i-1]*mod;
}
}
int main(){
scanf("%d%d",&n,&m);
init();
memset(ha,0,sizeof(ha));
memset(hb,0,sizeof(hb));
while(m--){
int u,v;
scanf("%d%d",&u,&v);
ha[u]+=va[v];
ha[v]+=va[u];
}
for(int i=1;i<=n;i++){
hb[i]=ha[i]+va[i];
}
LL ans=0;
sort(ha+1,ha+1+n);
sort(hb+1,hb+1+n);
LL now=ha[1],cnt=1;
for(int i=2;i<=n;i++){
if(now==ha[i]) cnt++;
else{
ans+=(cnt*(cnt-1))/2;
now=ha[i];
cnt=1;
}
}
ans+=(cnt*(cnt-1))/2;
now=hb[1],cnt=1;
for(int i=2;i<=n;i++){
if(now==hb[i]) cnt++;
else{
ans+=(cnt*(cnt-1))/2;
now=hb[i];
cnt=1;
}
}
ans+=(cnt*(cnt-1))/2;
printf("%I64d\n",ans);
return 0;
}
相关文章推荐
- 编译Memcached测试客户端连接【web级ubuntu速学课程】
- NYOJ 1112 求次数 (map)
- 函数指针的运用于程序示例
- 大数据学习-hadoop -第三课附加- hadoop RPC源码分析
- HDU 2080 夹角有多大II(余弦公式)
- Yarn中几个专用名称
- Qt qml listview刷新
- HDU1753 大明A+B(带小数的大数加法)
- Unity 中gameObject.SetActive()方法的使用
- [Leetcode]216. Combination Sum III
- Java GC 日志分析秒懂!
- codeforces水题100道 第二十二题 Codeforces Beta Round #89 (Div. 2) A. String Task (strings)
- 自创控件Topbar
- String 类的基本用法
- 棋盘问题
- 外部css样式改变,刷新E网页没改变
- flashback table to before drop外键约束无法恢复的原因分析
- Linux下非root用户能创建新文件,却不能拷贝文件的问题
- poj2438 Children's Dining(哈密尔顿回路)
- memcached存储大数据的问题