∆ (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 ...
随机推荐
- python实例 异常处理
#! /usr/bin/python s=input("Input your age:") if s =="": raise Exception(&qu ...
- HDU 4280 Island Transport(dinic+当前弧优化)
Island Transport Description In the vast waters far far away, there are many islands. People are liv ...
- 转:解决Onethink上传视频的问题 超棒的解决方案
用过Onethink的都知道,它是不能上传视频的. 有人想到用上传附件的方式上传视频,但是结果……就是提示没有上传文件. 要是正常上传个一两兆的图片啊,压缩文件什么的还是可以的. 所以,重点来了 怎么 ...
- js经典校验之注册与登录校验
平时都专注于后台功能的实现和逻辑需求的分析及数据库方面的设计,很少关注前端的设计,而项目开发过程中专门负责后台是不太可能的事,所以前端我们也需要会用,除了漂亮的首页等其他的交给美工来做,一些功能性的东 ...
- bzoj 3895 取石子——博弈论
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3895 看题解:https://blog.csdn.net/popoqqq/article/d ...
- Vim 中自定义注释快捷键
写程序的时候写过的代码不忍心立马删掉,所以注释很多,所以找了下注释的快捷健. 打开 /etc/vim/vimrc文件,添加如下两行代码即可. /* 注释该行 */ map = I/* ^[A */j ...
- ArcGISTiledMapServiceLayer
<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>第一 ...
- Sublime Text编辑器运行Python程序控制台输入
将文件保存为 .py后,安装插件: 1)按ctrl+shift+p快捷键呼出一个输入框,输入Install Package,回车,在新出现的输入框里输入“SublimeREPL”并安装. 2)点击To ...
- React高阶组件 和 Render Props
高阶组件 本质 本质是函数,将组件作为接收参数,返回一个新的组件.HOC本身不是React API,是一种基于React组合的特而形成的设计模式. 解决的问题(作用) 一句话概括:功能的复用,减少代码 ...
- python3.7 安装gensim使用word2Vec库
应用的文章(个人试验过,完全正确):https://radimrehurek.com/gensim/index.html#install