【HDU - 6184】Counting Stars(三元环计数,二分,优化暴力,O(m*sqrt(m)),图论)

题干:

Little A is an astronomy lover, and he has found that the sky was so beautiful! 

So he is counting stars now! 

There are n stars in the sky, and little A has connected them by m non-directional edges. 

It is guranteed that no edges connect one star with itself, and every two edges connect different pairs of stars. 

Now little A wants to know that how many different "A-Structure"s are there in the sky, can you help him? 

An "A-structure" can be seen as a non-directional subgraph G, with a set of four nodes V and a set of five edges E. 

If V=(A,B,C,D)V=(A,B,C,D) and E=(AB,BC,CD,DA,AC)E=(AB,BC,CD,DA,AC), we call G as an "A-structure". 

It is defined that "A-structure" G1=V1+E1G1=V1+E1 and G2=V2+E2G2=V2+E2 are same only in the condition that V1=V2V1=V2 and E1=E2E1=E2. 

Input

There are no more than 300 test cases. 

For each test case, there are 2 positive integers n and m in the first line. 

2≤n≤1052≤n≤105, 1≤m≤min(2×105,n(n−1)2)1≤m≤min(2×105,n(n−1)2) 

And then m lines follow, in each line there are two positive integers u and v, describing that this edge connects node u and node v. 

1≤u,v≤n1≤u,v≤n 

∑n≤3×105∑n≤3×105,∑m≤6×105∑m≤6×105 

Output

For each test case, just output one integer--the number of different "A-structure"s in one line. 

Sample Input

4 5
1 2
2 3
3 4
4 1
1 3
4 6
1 2
2 3
3 4
4 1
1 3
2 4

Sample Output

1
6

题目大意:

    

解题报告:

暴力。这题主要是卡空间了啊不然随便搞的。。然后卡空间了变成怎么交都MLE、、、

对于题干,不难想到就是问有多少个双三元环,所有我们枚举每一条边,然后能构成的所有三元环个数假设x,那就是C(2,x)就是这条边的贡献。

暴力每个点i,然后枚举他的所有边获得点j,然后对于第三个点k分两种情况讨论:

①如果j的临边数目少于,那么直接暴力就好,复杂度

②如果临边数目大于,(这样的点一定不会多于的复杂度  个)那么暴力i的邻接点,二分判断有没有就可以了。

这样保证总复杂度

好像还有更优秀的做法:参考博客

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
vector<int> vv[MAX];
int e[MAX];
int n,m;
int main()
{while(~scanf("%d%d",&n,&m)) {for(int i = 1; i<=n; i++) e[i] = -1,vv[i].clear();for(int u,v,i = 1; i<=m; i++) {scanf("%d%d",&u,&v);if(u>v) swap(u,v);vv[u].pb(v);vv[v].pb(u);}for(int i = 1; i<=n; i++) sort(vv[i].begin(),vv[i].end());int SQRT = sqrt(m);ll ans = 0;for(int i = 1; i<=n; i++) {for(auto j : vv[i]) e[j] = i;for(auto j : vv[i]) {if(j > i) {int sz = vv[j].size(),cnt = 0;if(sz <= SQRT) {for(auto k : vv[j]) cnt += e[k] == i;}else {						for(auto k : vv[i]) cnt += binary_search(vv[k].begin(),vv[k].end(),j); }ans += 1LL*cnt*(cnt-1)/2;}}}printf("%lld\n",ans);}return 0 ;
}

还有一个问题啊、、为什么sz<SQRT的话,就一直报TLE啊????欢迎来讨论、、、

  • 焦作 L
  • Commet oj的一道题
  • 牛客多校第三场A

改成严格的msqrtm的算法:(建有向边)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int cnt[MAX];
int U[MAX],V[MAX],du[MAX];
vector<PII> vv[MAX];
PII e[MAX];
int n,m;
int main()
{while(~scanf("%d%d",&n,&m)) {for(int i = 1; i<=n; i++) du[i] = 0,e[i].FF = 0,vv[i].clear();for(int i = 1; i<=m; i++) cnt[i] = 0;for(int i = 1; i<=m; i++) scanf("%d%d",U+i,V+i),du[U[i]]++,du[V[i]]++;for(int u,v,i = 1; i<=m; i++) {u = U[i],v = V[i];if(du[u] > du[v]) vv[v].pb(pm(u,i));else if(du[u] < du[v]) vv[u].pb(pm(v,i));else {if(u<v) vv[u].pb(pm(v,i));else vv[v].pb(pm(u,i)); }}for(int u,v,i = 1; i<=m; i++) {u = U[i],v = V[i];for(auto x : vv[u]) e[x.FF] = pm(i,x.SS);for(auto x : vv[v]) {if(e[x.FF].FF == i) cnt[i]++,cnt[x.SS]++,cnt[e[x.FF].SS]++;} }ll ans = 0;for(int i = 1; i<=m; i++) ans += 1LL*cnt[i]*(cnt[i]-1)/2;printf("%lld\n",ans);}return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440457.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php 取oracle图片,在PHP中将图片存放ORACLE中_php

我这里提供一个用php操纵blob字段的例子给你&#xff0c;希望能有所帮助&#xff01;这个例子是把用户上传的图片文件存放到BLOB中。假设有一个表&#xff0c;结构如下&#xff1a;CREATE TABLE PICTURES (ID NUMBER,http://www.gaodaima.com/44856.html在PHP中将图片存放oracl…

【HDU - 6183】Color it(CDQ分治 或 动态开点线段树)

题干&#xff1a; Do you like painting? Little D doesnt like painting, especially messy color paintings. Now Little B is painting. To prevent him from drawing messy painting, Little D asks you to write a program to maintain following operations. The speci…

php create()方法,ThinkPHP中create()方法自动验证实例

ThinkPHP中create()方法自动验证实例2020-06-16 04:24:32自动验证是ThinkPHP模型层提供的一种数据验证方法&#xff0c;可以在使用create创建数据对象的时候自动进行数据验证。原理&#xff1a;create()方法收集表单($_POST)信息并返回&#xff0c;同时触发表单自动验证&#x…

【蓝桥杯官网试题 - 历届试题】格子刷油漆(dp)

题干&#xff1a; 问题描述 X国的一段古城墙的顶端可以看成 2*N个格子组成的矩形&#xff08;如下图所示&#xff09;&#xff0c;现需要把这些格子刷上保护漆。   你可以从任意一个格子刷起&#xff0c;刷完一格&#xff0c;可以移动到和它相邻的格子&#xff08;对角相邻也…

oracle软件静默安装程序,【oracle】静默安装 oracle 11gr2

【序言】oracle 提供了静默安装方法在不适用图形界面的情况下安装 oracle 软件 ,创建db,配置netca,快速完成oracle 的部署。在以下情形中可以使用静默安装a OUI 的 GUI 界面远程交互比较慢 .b 数据库服务器无法使用图形界面访问.c 批量部署oracle (标准环境统一情况下可以使用o…

【2050 Programming Competition - 2050 一万人码 】非官方部分题解(HDU)

1001 开场白 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 12147 Accepted Submission(s): 3502 Problem Description 来自世界各地的年青人在 https://2050.org.cn 握手团聚&#xff0c; 他们是航空…

oracle数据库建表视频,Oracle数据库的创建表全

CREATE TABLE "库名"."表名" ("FEE_ID" VARCHAR2(10 BYTE) constraint ABS_FEE_ID primary key,--主键&#xff0c;必须要有序列"BANK_GROUP_ID" VARCHAR2(5 BYTE),"ABS_PRODUCT_ID" VARCHAR2(30 BYTE))TABLESPACE "表…

oracle dump enq hw,经典故障分析 - ASSM引发的索引争用与 enq HW -contentio

作者介绍&#xff1a;孙加鹏 云和恩墨技术顾问六年Oracle技术顾问经验&#xff0c;所服务的行业包括电信运营商、金融业、制造业等。擅长Oracle的故障诊断、高可用架构、升级迁移等。目前主要服务于上海金融类客户。1故障概述2017年07月24日11:58左右&#xff0c;客户核心数据库…

【ZOJ - 3946】Highway Project(最短路子图,维护双权值,贪心,最小树形图)

题干&#xff1a; Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can reach other cities from the capital as fast as possible. Thus, he proposed the highway project. The Marjar Empire has N cities (including…

【ZOJ - 3956】Course Selection System(01背包)

题干&#xff1a; There are n courses in the course selection system of Marjar University. The i-th course is described by two values: happiness Hi and credit Ci. If a student selects m courses x1, x2, ..., xm, then his comfort level of the semester can be…

Linux把文件移动到容器外,Docker容器与主机之间拷贝文件的方法

一般情况下&#xff0c;我们在启动Docker容器的时候可以使用-v参数映射宿主机的文件或者目录到容器里&#xff0c;这样的话&#xff0c;在宿主机相关目录下的文件修改会自动在容器里生效。但是&#xff0c;如果我们已经启动了一个容器的话&#xff0c;就只能使用下面的这种方式…

【计蒜客 - 2019南昌邀请赛网络赛 - H】Coloring Game(找规律,思维dp)

题干&#xff1a; David has a white board with 2 \times N2N grids.He decides to paint some grids black with his brush.He always starts at the top left corner and ends at the bottom right corner, where grids should be black ultimately. Each time he can mov…

【HDU - 6514】Monitor(二维差分,前缀和)

题干&#xff1a; Monitor Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 163840/163840 K (Java/Others) Total Submission(s): 872 Accepted Submission(s): 145 Problem Description Xiaoteng has a large area of land for growing crops, and the land…

【CodeForces - 1153D】Serval and Rooted Tree(树形dp)

题干&#xff1a; Now Serval is a junior high school student in Japari Middle School, and he is still thrilled on math as before. As a talented boy in mathematics, he likes to play with numbers. This time, he wants to play with numbers on a rooted tree. …

实验楼Linux基础挑战2答案,实验楼-Linux基础-实验二 Linux的基本概念及操作

一、实验介绍1.1 实验内容实验楼环境介绍常用 Shell 命令及快捷键Linux 使用小技巧1.2 实验知识点Linux 基本命令通配符的使用查看帮助文档二、桌面环境1.Linux 桌面环境介绍相对于现在的 Windows 系统&#xff0c;UNIX/Linux 本身是没有图形界面的&#xff0c;我们通常在 UNIX…

【HDU - 1533】Going Home(网络流,二分图最优匹配,KM算法)

题干&#xff1a; On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step h…

【ZOJ - 4029】Now Loading!!!(整除分块,思维,二分,前缀和)

题干&#xff1a; 其中 zi 是第i次询问后的z。 解题报告&#xff1a; 因为有取log运算&#xff0c;所以分母的取值肯定不会超过30种&#xff0c;所以分每一个分母的时候&#xff0c;用前缀和优化一个和&#xff0c;最后求乘积就行了。&#xff08;其实不需要快速幂&#xff0c…

【ZOJ - 4032】Magic Points (思维,几何,构造)

题干&#xff1a; 解题报告&#xff1a; 想到了&#xff0c;这样绕圈构造。但是这样有个问题&#xff0c;最后一个点如何构造。 刚开始想的是n奇数 &#xff0c; 就8 10 这样的连一条&#xff0c;n偶数 就8 11 这样的连一条&#xff0c;随便构造一下就行&#xff0c;但是发…

android 仿真翻页动画,Android 两Activity之间动画效果(1)---------翻页效果

用Android rotate动画实现翻页效果&#xff0c;效果如图&#xff1a;要实现上面动画&#xff0c;首先搞明白rotate动画原理&#xff1b;(1)Degrees坐标&#xff1a;0度(360度)270度90度 顺时针旋转 180(2)rotate 关键属性fromDegrees 开始旋转时角度 toDegrees 结束时的角…

android 存储不被垃圾清理,手机内存足够大,就不需要清理垃圾了?你错了!

原标题&#xff1a;手机内存足够大,就不需要清理垃圾了?你错了!中新网4月20日电今天,人们使用智能手机的时间已超过电脑,希望在任何时候、任何地方,一部手机搞定所有。对手机的流畅度、性能和安全的要求越来越高。新手机刚到手时非常流畅,用一段时间就出现各种卡顿,网民对猎豹…