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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Friendly Group Gym - 102769F 2020(并查集)ccpc秦皇岛分站赛

發布時間:2023/12/4 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Friendly Group Gym - 102769F 2020(并查集)ccpc秦皇岛分站赛 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題意:

n個學生要組成一個小組參加會議(可以不參加),
1.對于每兩個朋友(x ,y),如果他們倆都參加會議,該小組的友好價值將會增加 1;如果其中只有一位參加會議,則該組的友好價值將降低 1。
3.如果n個學生參加會議,對團隊的友好價值將降低n.

題目:

Professor Alex will organize students to attend an academic conference.

Alex has n excellent students, and he decides to select some of them (possibly none) to attend the conference. They form a group. Some pairs of them are friends.

The friendly value of the group is initially 0. For each couple of friends (x,y), if both of them attend the conference, the friendly value of the group will increase 1, and if only one of them attends the conference, the friendly value of the group will decrease 1. If k students attend the conference, the friendly value of the group will decrease k.

Alex wants to make the group more friendly. Please output the maximum friendly value of the group.

Input

The first line of the input gives the number of test cases, T (1≤T≤104). T test cases follow.

For each test case, the first line contains two integers n (1≤n≤3×105) and m (1≤m≤106), where n is the number of students and m is the number of couples of friends.

Each of the following m lines contains two integers xi,yi (1≤xi,yi≤n,xi≠yi), representing student xi and student yi are friends. It guaranteed that unordered pairs (xi,yi) are distinct.

The sum of n in all test cases doesn’t exceed 106, and the sum of m in all test cases doesn’t exceed 2×106.

Output

For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1), and y is the maximum friendly value of the group.

Example

Input

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

Output

Case #1: 1
Case #2: 0

分析:

1.由題意,友好價值=組群里的邊數-點數;
2.那么怎么求最大友好價值?由于對于每兩個朋友(x ,y),如果他們倆都參加會議,該小組的友好價值將會增加 1;如果其中只有一位參加會議,則該組的友好價值將降低 1,所以我們用并查集,直接找根節點遍歷到最后,這時就變成了一個個的組群,我們只要判斷這些組群是否參加會議即可,當組群里的邊數-點數<0時,不讓其參加,反之參加即可求得。

AC代碼:

#include<bits/stdc++.h> using namespace std; const int M=3e5+10; int t,n,m,k,ans; int f[M],a[M],b[M]; int getf(int x){if(f[x]==x) return x;return f[x]=getf(f[x]); } int main(){cin>>t;k=0;while(t--){cin>>n>>m;for(int i=1;i<=n;i++){f[i]=i;a[i]=0;//記錄邊,起始邊個數為零。b[i]=1;//記錄點,原始本身就為一個點。}for(int i=1;i<=m;i++){int t1,t2;scanf("%d%d",&t1,&t2);int u=getf(t1);int v=getf(t2);if(u==v) a[u]++;else {f[v]=u;a[u]+=a[v]+1;//因為最終怕判斷的根節點判組群,所以記錄邊和點只需要在根節點上即可。b[u]+=b[v];}}ans=0;for(int i=1;i<=n;i++){if(i==f[i]&&a[i]-b[i]>0)ans+=a[i]-b[i];}printf("Case #%d: %d\n",++k,ans);}return 0; }

總結

以上是生活随笔為你收集整理的Friendly Group Gym - 102769F 2020(并查集)ccpc秦皇岛分站赛的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。