HDU 6178 Monkeys【dfs】【输入外挂模板】

18 篇文章 0 订阅
14 篇文章 0 订阅

Monkeys

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 153428/153428 K (Java/Others)
Total Submission(s): 717    Accepted Submission(s): 232


Problem Description
There is a tree having N vertices. In the tree there are K monkeys (K <= N). A vertex can be occupied by at most one monkey. They want to remove some edges and leave minimum edges, but each monkey must be connected to at least one other monkey through the remaining edges.
Print the minimum possible number of remaining edges.
 

Input
The first line contains an integer T (1 <= T <= 100), the number of test cases. 
Each test case begins with a line containing two integers N and K (2 <= K <= N <= 100000). The second line contains N-1 space-separated integers  a1,a2,,aN1 , it means that there is an edge between vertex  ai  and vertex i+1 (1 <=  ai  <= i).
 

Output
For each test case, print the minimum possible number of remaining edges.
 

Sample Input
  
  
2 4 4 1 2 3 4 3 1 1 1
 

Sample Output
  
  
2 2
 

Source

题意:给一棵树,有N个节点,树上有K个猴子,每个节点最多有一个猴子,从树上的边中去掉一些边,留下尽可能少的边使得每个猴子都至少和一个其他猴子相连,求最少留的边数。

思路:从叶子节点往上配对,看有多少组可以配成两对,如果要放得猴子大于对数,则没多一个猴子则多一条边,否则直接为猴子能配成的对数。

ps(此题要使用输入外挂,要不然会超时,这个输入外挂要用freopen输入)

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define ms(x,y) memset(x,y,sizeof(x))
using namespace std;

typedef long long ll;

const double pi = acos(-1.0);
const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;

/*------- 开挂 -------*/      //注意此输入外挂输入要用freopen
namespace fastIO {
    #define BUF_SIZE 100000
    // fread -> read
    bool IOerror = 0;

    char nc() {
        static char buf[BUF_SIZE], *pl = buf + BUF_SIZE, *pr = buf + BUF_SIZE;
        if(pl == pr) {
            pl = buf;
            pr = buf + fread(buf, 1, BUF_SIZE, stdin);
            if(pr == pl) {
                IOerror = 1;
                return -1;
            }
        }
        return *pl++;
    }

    inline bool blank(char ch) {
        return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
    }

    void read(int &x) {
        char ch;
        while(blank(ch = nc()));
        if(IOerror)
            return;
        for(x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0');
    }
    #undef BUF_SIZE
};
using namespace fastIO;
/*------- 完结 -------*/

struct Edge
{
    int to,next;   
}edge[maxn*2];

int head[maxn],e;
bool vis[maxn];
int ans;

void addedge(int u,int v)
{
    edge[e].to=v;
    edge[e].next=head[u];
    head[u]=e++;
}

void dfs(int u,int fa)
{
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].to;
        if(v==fa) continue;
        dfs(v,u);
        if(!vis[v])
        {
            if(!vis[u])
            {
                vis[u]=vis[v]=1;
                ans++;
            }
        }
    }
}

void init()
{
    ms(head,-1);
    ans=0;
    ms(vis,0);
    e=0;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int t;
    //scanf("%d",&t);
    read(t);
    while(t--)
    {
        init();
        int n,m;
        //scanf("%d%d",&n,&m);
        read(n);
        read(m);
        for(int i=1;i<n;i++)
        {
            int p;
            //scanf("%d",&p);
            read(p);
            addedge(p,i+1);
            addedge(i+1,p);
        }
        dfs(1,0);
        if(m>=ans*2)
        {
            printf("%d\n",ans+m-2*ans);
        }
        else
            printf("%d\n",(m-1)/2+1);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值