题意:

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秦皇岛分站赛相关推荐

  1. Gym 101915J(并查集)

    传送门 题面: J. The Volcano Eruption time limit per test 5.0 s memory limit per test 64 MB input standard ...

  2. Good Number Gym - 102769G 2020年CCPC秦皇岛分站赛

    题意: 如果一个数字是Good Number,当且仅当 ⌊xk⌋\left \lfloor\sqrt[k]{x}\right \rfloor⌊kx​⌋(向下取整) 能整除 x . 现在给出 n,k , ...

  3. gym:Problem A Artwork(并查集思维题)

    20162017-acmicpc-nordic-collegiate-programming-contest-ncpc-2016 Problem A Artwork 题目链接 http://codef ...

  4. Por Costel and the Match Gym - 100923H(经典种类并查集)

    Por Costel and the Match Gym - 100923H 题目链接:https://vjudge.net/problem/Gym-100923H 题目: Oberyn Martel ...

  5. 并查集 ---- 扩展域并查集判二分图 + 循环模拟字典树 The 2020 ICPC Asia Macau Regional Contest C. Club Assignment (详解)

    题目链接 题目大意: 有n个数,现在要把他们拆分成两个集合,假设S为集合,有如下定义: f(S)={min(x⊕y)∣x,y∈S,andx!=y}f(S)=\{min(x\oplus y)|x,y\i ...

  6. 【BFS】【并查集】【Tarjan】【LCA】Gym - 101173H - Hangar Hurdles

    给你一张地图,给你q次询问,每次问你从A点到B点,最大能移动多大的箱子. 把每个点所能容纳的最大箱子求出来(BFS,八连通,一开始将所有边界点和障碍点入队).然后从大到小排序.然后用并查集将相邻(四联 ...

  7. 【杭电多校2020】Total Eclipse【贪心】【并查集】

    题意:nnn个点mmm条边的无向图,每个点有一个正点权,每次选择一个连通子图,将里面的权值都减111.求所有点权为000的最小步数. T≤10,n≤105,m≤2×105T\leq 10,n\leq ...

  8. Tree Restoration Gym - 101755F (并查集)

    There is a tree of n vertices. For each vertex a list of all its successors is known (not only direc ...

  9. 2020牛客暑期多校训练营(第八场)I-Interesting Computer Game(并查集 + 思维)

    链接: I-Interesting Computer Game 题意: 给出 n 组 a,b,每次可以选择 a 或者选择 b ,问做多可以选多少个不同的数. 思路: 考虑每个连通块 , 如果是 n 个 ...

最新文章

  1. Djkastra堆(手写堆)优化版
  2. VUE 笔记(持续更新中...)
  3. 如何在DOS/Windows和Linux/Unix之间进行文件格式转换?
  4. 045_Collapse折叠面板
  5. 万物之始正则表达式全解析三部曲(上篇)-正则表达式基础知识及语法
  6. clodeblocks debug断点调试_Intellij IDEA高阶DEBUG大杀器
  7. 临阵磨枪,血拼季网站优化的最后三板斧
  8. 在RAC中重建EM(转)
  9. 【华科考研复试机试题】华中科技大学考研复试机试题解题报告
  10. MySQL基础总结(一)
  11. 利用反射对dao层进行重写
  12. 安装Sphinx 和 Thinking Sphinx
  13. [转载]各种在线api地址
  14. VB6里自动提交/自动填表的一种相对通用的方案
  15. 基于W5300和FPGA的实时数据采集系统设计
  16. Vuejs 使用 lib 库模式打包 umd 解决 NPM 包发布的问题
  17. gps修改国内服务器,gps修改国内服务器地址
  18. cocos2d-lua 3x 基础概念(包括场景、导演、在屏幕上显示自定义对象等)
  19. [轉]ERP系统之比较——SAP、Oracle、BAAN、JDE、SSA
  20. 程序员必备的21款工具与编程灵感

热门文章

  1. (十一)python3 只需3小时带你轻松入门——面向对象
  2. 服务器安装红帽系统进入不图形界面,CentOS 安装图形化界面方法
  3. python自带的函数有哪些_为什么说 Python 内置函数并不是万能的?
  4. 轮子,辛苦你了。 | 今日最佳
  5. 不得了,日本出版社竟是这样吸引死宅学编程的
  6. 一般将来时语法课教案_速看,如何在考场写出一篇脱颖而出的教案
  7. centos7 禁止ip访问_centos7.6版本限制某个IP访问指定端口
  8. python从入门到实践django_Django入门——《Python编程从入门到实践》
  9. typora插入代码设置_Typora基本功能介绍
  10. matlab如何求矩阵的转置矩阵,怎么用MATLAB程序求转置矩阵?急需,高手帮忙………………...