SPOJ - UOFTCG(树的路径覆盖,好题)

20 篇文章 8 订阅
13 篇文章 0 订阅

题目链接

UOFTCG - Office Mates

no tags 

Dr. Baws has an interesting problem. His  N  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  K graduate students, there are at most  K1  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  T50 , the number of test cases. Each test case begins with two integers on their own line:  N100000 , the number of graduate students (who are indexed by the integers  1  through  N ), and  M , the number of friendships among the students. Following this are  M  lines, each containing two integers  i  and  j  separated by a single space. Two integers  i  and  j  represent a mutual friendship between students  i  and  j .

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(N <= 100000)个学生,M 对朋友关系,学生只能挨着他的朋友坐。

桌子排列成一条直线,可以让一些桌子空出来.

数据保证对于任何含K(K<=N)个学生的集合,最多只有K-1对朋友。

求最少需要多少张桌子。


题解

这道题可以转化为图的最小路径覆盖。假设点数为n,最小路径覆盖条数为m,答案即为n+m-1。根据题意,发现数据是若干颗树。

那么,对于一棵树,怎么求最小路径覆盖呢?

有两种方法,贪心和树形dp,可参考博客:博客链接

树形dp解至今还没看懂==


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int inf=0x3fffffff;
const ll mod=1000000007;
const int maxn=100000+100;
int head[maxn];
struct edge
{
    int from,to,next;
}e[maxn*2];   //
int tol=0;
void add(int u,int v)
{
    e[++tol].to=v,e[tol].next=head[u],head[u]=tol;
}
int vis[maxn],sum[maxn],used[maxn];
void dfs(int u,int fa)
{
    vis[u]=1;
    sum[u]=1;
    int deg=0;
    for(int i=head[u];i;i=e[i].next)
    {
        int v=e[i].to;
        if(v==fa) continue;
        dfs(v,u);
        sum[u]+=sum[v];
        if(!used[v]) deg++;
    }
    if(deg>=2) used[u]=1,sum[u]-=2;
    else if(deg==1) sum[u]-=1;
}
int main()
{
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        memset(vis,0,sizeof(vis));
        memset(sum,0,sizeof(sum));
        memset(head,0,sizeof(head));
        memset(used,0,sizeof(used));
        tol=0;
        int n,m;
        scanf("%d%d",&n,&m);
        while(m--)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            add(u,v),add(v,u);
        }
        int ans=0;
        rep(i,1,n+1)
        {
            if(!vis[i]) dfs(i,0),ans+=sum[i];
        }
        ans=n+ans-1;
        printf("%d\n",ans);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值