∆ (triangle)
2.1 题目描述
给定一个无自环重边的无向图,求这个图的三元环1的个数以及补图2的三元环个数。
2.2 输入格式
第一行 2 个数 n, m ,分别表示图的点数、边数。
接下来 m 行,每行两个数 u, v ,表示一条连接 u, v 的无向边。
2.3 输出格式
一行两个数,依次表示原图的三元环个数以及补图的三元环的个数。
2.4 样例输入
5 5
1 2
1 3
2 3
2 4
3 4
2.5样例输出
2 1
2.6数据范围
对于 30% 的数据:n ≤ 100
对于 60% 的数据:m ≤ 500
对于 100% 的数据:n ≤ 10^5 , m ≤ 10^5
2.7评分方式
如果你两个数均输出正确,得 10 分。
否则如果两个数中任意一个正确或者两个数的和正确,得 6 分。 否则不得分。
注:
1大小为 3的环。即一个无序三元组 (x, y, z) 使得任意两点之间都有边
2一条连接(u, v)(u = v) 的边,如果在原图中出现了,那么在补图中不会出现,否则一定会在补图中出现。
题解:
题目中说两个数的和正确可以得分,是不是说明先求和是一个突破口呐?
对于一个完全图,三元环的数量是C(n,3),少了一些边,就少了一些三元环,少的三元环应该有至少一条边在原图中,至少一条边在补图中。
减少的三元环数量为:sigma(d[i]*(n-1-d[i]))/2 (d[i]为度数)
一开始我想不通为什么是除以2,然后我画了两个图,就发现了答案,每个三元环可以被两个点找到。
剩下的就是求原图中的三元环,有一个神奇的算法,和分段暴力有一丢丢类似吧。
将所有点分成两类:d[i]<sqrt(m)的和d[i]>sqrt(m)的.
先求包含第一类点的三元环个数. 由于边很少,所以枚举2条边即可.由于一个点的度不超过sqrt(m),所以一条边最多被枚到(sqrt(m))次,最多枚M条边,所以这个操作时O(m*sqrt(m))的.
再求不包含第一类点的三元环个数. 由于每条边贡献2个度,所以二类点的数量是O(sqrt(m))级的.直接枚举三个点,复杂度O((sqrt(m))^3)=O(m*sqrt(m))
所以算法总的复杂度是O(m*sqrt(m))的.
我先用了一个vector来判断点i与j是否有边,但是T掉了,然后get到了一个聪明的把一条边的两个节点一起hash的方法,就写了一个hash表水过去了。
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<set>
#define nn 100010
#define mod 1000007
#define base 2333
using namespace std;
namespace fastIO
{
#define buf_size 100000
bool error;
inline char gc()
{
static char buf[buf_size + 1],*l=buf,*r=buf;
if(l==r)
{
l=buf;
r=buf+fread(buf,1,buf_size,stdin);
if(l==r) {error = 1;return -1;}
}
return *l++;
}
inline bool blank(char ch) {return ch=='\n'||ch =='\t'||ch ==' '||ch =='\r'||error;}
inline bool getint(int &x)
{
char ch; int f = 1;
while (blank(ch = gc())); if (error) return false;
x = 0;
if (ch == '-') f=-1,ch=gc();
while (1){x = (x<<1) + (x<<3)+ch-'0';if(!isdigit(ch = gc())) break;}
x*=f;
return true;
}
inline void putint(long long x)
{
if(!x) {putchar('0'); return;}
if(x<0){x=-x; putchar('-');}
static int out[13];
register int len = 0;
while(x){out[++ len]=x%10; x/=10;}
while(len) putchar(out[len --]+'0');
}
#undef buf_size
}
using namespace fastIO;
int in[nn],fir[nn],nxt[nn<<1],to[nn<<1],a[1000007],b[1000007],head[1000007],next[1000007];
bool hash[1000007];
int e=0,inum=0;
void add(int u,int v)
{
nxt[++e]=fir[u];fir[u]=e;to[e]=v;
nxt[++e]=fir[v];fir[v]=e;to[e]=u;
}
long long c(long long n,int m)
{
long long an=(n-2)*(n-1)*n/6;
return an;
}
void addd(int u,int v)
{
int t=(u*base+v)%mod;
a[++inum]=u;b[inum]=v;next[inum]=head[t];head[t]=inum;
}
inline bool query(int u,int v)
{
int t=(u*base+v)%mod;
for (int p=head[t];p;p=next[p])
if (a[p]==u&&b[p]==v)
return 1;
return 0;
}
int main()
{
freopen("triangle.in","r",stdin);
freopen("triangle.out","w",stdout);
int n,m,u,v;
getint(n);getint(m);
long long all=0,sum=0;
for(register int i(1);i<=m;i++)
{
getint(u);getint(v);
addd(u,v);
add(u,v);
in[u]++;in[v]++;
}
for(register int i(1);i<=n;i++)
all+=in[i]*(n-1-in[i]);
all/=2;
all=c(n,3)-all;
for(register int i(1);i<=n;i++)
{
for(register int j=fir[i];j;j=nxt[j])
for(register int k=nxt[j];k;k=nxt[k])
{
if(query(to[j],to[k])||query(to[k],to[j]))
sum++;
}
}
putint(sum/3);putchar(' ');putint(all-sum/3);
return 0;
}
∆ (triangle)的更多相关文章
- [LeetCode] Triangle 三角形
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [LeetCode] Pascal's Triangle II 杨辉三角之二
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- [LeetCode] Pascal's Triangle 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- 【leetcode】Pascal's Triangle II
题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...
- 【leetcode】Pascal's Triangle
题目简述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
- POJ 1163 The Triangle(简单动态规划)
http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
- Triangle - Delaunay Triangulator
Triangle - Delaunay Triangulator eryar@163.com Abstract. Triangle is a 2D quality mesh generator an ...
- LeetCode 118 Pascal's Triangle
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- LeetCode 119 Pascal's Triangle II
Problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Ret ...
- 【leetcode】Triangle (#120)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
随机推荐
- Entrust - Laravel 用户权限系统解决方案 | Laravel China 社区 - 高品质的 Laravel 和 PHP 开发者社区 - Powered by PHPHub
说明# Zizaco/Entrust 是 Laravel 下 用户权限系统 的解决方案, 配合 用户身份认证 扩展包 Zizaco/confide 使用, 可以快速搭建出一套具备高扩展性的用户系统. ...
- HDU - 1724 Ellipse 自适应辛普森模板
OJ 题解传送门 //Achen #include<algorithm> #include<iostream> #include<cstring> #include ...
- Python要如何实现(列表)排序?
排序,是许多编程语言中经常出现的问题.同样的,在Python中,如何是实现排序呢?(以下排序都是基于列表来实现) 一.使用Python内置函数进行排序 Python中拥有内置函数实现排序,可以直接调用 ...
- Leetcode860.Lemonade Change柠檬水找零
在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 美元或 20 美元.你必须给 ...
- golang之下载安装配置
1.下载:根据操作系统和计算架构选择合适的安装包,操作系统类型有linux.mac.windows等,计算架构分为32位的386计算架构和64位的amd64计算架构 2.安装:推荐安装到 /usr/l ...
- iOS block 用法
1.定义Block /* 回传void ,参数也是void 的block*/ void (^blockReturningVoidWithVoidArgument)( void ); /* 回传整数,两 ...
- svn钩子(hooks)自动部署代码到web目录
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/li956732806/article/details/71158869 web目录:/hoe ...
- 大数据ETL详解
ETL是BI项目最重要的一个环节,通常情况下ETL会花掉整个项目的1/3的时间,ETL设计的好坏直接关接到BI项目的成败.ETL也是一个长期的过程,只有不断的发现问题并解决问题,才能使ETL运行效率更 ...
- jQuery纵向分类下拉菜单导航
在线演示 本地下载
- C++简单读取 & 写入实例
#include <fstream> #include <iostream> using namespace std; int main () { ]; // 以写模式打开文件 ...