SPOJ:Office Mates(树最小路径覆盖)

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 K1K−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 T50T≤50, the number of test cases. Each test case begins with two integers on their own line: N100000N≤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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值