日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2023/12/10 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【HDU - 6184】Counting Stars(三元环计数,二分,优化暴力,O(m*sqrt(m)),图论) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題干:

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

題目大意:

????

解題報(bào)告:

暴力。這題主要是卡空間了啊不然隨便搞的。。然后卡空間了變成怎么交都MLE、、、

對(duì)于題干,不難想到就是問有多少個(gè)雙三元環(huán),所有我們枚舉每一條邊,然后能構(gòu)成的所有三元環(huán)個(gè)數(shù)假設(shè)x,那就是C(2,x)就是這條邊的貢獻(xiàn)。

暴力每個(gè)點(diǎn)i,然后枚舉他的所有邊獲得點(diǎn)j,然后對(duì)于第三個(gè)點(diǎn)k分兩種情況討論:

①如果j的臨邊數(shù)目少于,那么直接暴力就好,復(fù)雜度。

②如果臨邊數(shù)目大于,(這樣的點(diǎn)一定不會(huì)多于的復(fù)雜度? 個(gè))那么暴力i的鄰接點(diǎn),二分判斷有沒有就可以了。

這樣保證總復(fù)雜度。

好像還有更優(yōu)秀的做法:參考博客

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 ; }

還有一個(gè)問題啊、、為什么sz<SQRT的話,就一直報(bào)TLE啊????歡迎來討論、、、

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

改成嚴(yán)格的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 ; }

?

總結(jié)

以上是生活随笔為你收集整理的【HDU - 6184】Counting Stars(三元环计数,二分,优化暴力,O(m*sqrt(m)),图论)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。