Dr. Baws has an interesting problem. His NN graduate students, while friendly with some select people, are generally not friendly with each other. No graduate student is willing to sit beside a person they aren't friends with.
The desks are up against the wall, in a single line, so it's possible that Dr. Baws will have to leave some desks empty. He does know which students are friends, and fortunately the list is not so long: it turns out that for any subset of KK graduate students, there are at most K−1K−1 pairs of friends. Dr. Baws would like you to minimize the total number of desks required. What is this minimum number?
Input
The input begins with an integer T≤50T≤50, the number of test cases. Each test case begins with two integers on their own line: N≤100000N≤100000, the number of graduate students (who are indexed by the integers 11 through NN), and MM, the number of friendships among the students. Following this are MM lines, each containing two integers ii and jj separated by a single space. Two integers ii and jj represent a mutual friendship between students ii and jj.
The total size of the input file does not exceed 2 MB.
Output
For each test case output a single number: the minimum number of desks Dr. Baws requires to seat the students.
Example
Input:
1
6 5
1 2
1 3
1 4
4 5
4 6
Output:
7
Explanation of Sample:
As seen in the diagram, you seat the students in two groups of three with one empty desk in the middle.
题意:有N个学生,其中有M对好朋友(朋友关系不成环),将N个学生安排坐成一行,非好朋友不能坐在一起(至少要隔开一个位置),问至少要几张椅子。思路:首先这是一个森林,对于每棵树,肯定要安排尽量多的人坐在一起,那就是最小路径数覆盖一棵树的问题了,贪心即可。
# include <iostream>
# include <cstdio>
# include <vector>
# include <cstring>
using namespace std;
const int maxn = 1e5+30;
vector<int>g[maxn];
int vis[maxn], flag[maxn], f[maxn], siz[maxn];
void dfs(int cur, int pre)
{
int cnt=0;
siz[cur] = vis[cur] = f[cur] = 1;
for(int to:g[cur])
{
if(to == pre) continue;
dfs(to, cur);
f[cur] += f[to];
siz[cur] += siz[to];
if(!flag[to]) ++cnt;
}
if(cnt >= 2) f[cur]-=2, flag[cur]=1;
else if(cnt == 1) f[cur]-=1;
}
int main()
{
int t, n, m, u, v;
scanf("%d",&t);
while(t--)
{
int kuai = 0, ans = 0;
scanf("%d%d",&n,&m);
for(int i=1; i<=n; ++i) g[i].clear();
memset(vis, 0, sizeof(vis));
memset(flag, 0, sizeof(flag));
while(m--)
{
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
for(int i=1; i<=n; ++i)
{
if(!vis[i])
{
++kuai;
dfs(i,0);
ans += siz[i]+f[i]-1;
}
}
ans += kuai-1;
printf("%d\n",ans);
}
return 0;
}