1. 程式人生 > >HDU - 6178:Monkeys (貪心&樹上最大匹配輸&輸入優化)

HDU - 6178:Monkeys (貪心&樹上最大匹配輸&輸入優化)

xmlns input review possible tween math num monk include

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.

InputThe 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 a 1 ,a 2 ,,a N1 a1,a2,…,aN−1 , it means that there is an edge between vertex a i ai and vertex i+1 (1 <= a i ai <= i).
OutputFor 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

題意:給定一棵樹,有K個猴子,現在讓你把猴子放到節點上(一個節點最多一個猴子),求最少留多少邊,使得每個連通塊的猴子數不為1 。

思路:盡量把兩個猴子用一條邊連接起來。所以我們先貪心,有多少個“邊匹配”,假設最大值為cnt。然後如果夠,那麽答案是(K+1)/2;如果不夠,則加邊即可,答案是cnt+(K-cnt*2)。

求最大“邊匹配”的方法是貪心,從下想上貪,因為一個點最大貢獻到一條邊,所以把u和沒有被匹配的fa[u]匹配,其效果不會比fa[u]和fa[fa[u]]匹配差。

不用輸入優化會T。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
using namespace std;
const int maxn=1000010;
int vis[maxn],fa[maxn],cnt,ans;
inline char nc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline void read(int &sum){
    char ch=nc(); sum=0;
    while(!(ch>=0&&ch<=9))ch=nc();
    while(ch>=0&&ch<=9) sum=sum*10+ch-48,ch=nc();
}

int main()
{
     int T,N,K;
     read(T);
     while(T--){
        read(N);read(K); ans=cnt=0;
        rep(i,1,N) vis[i]=0;
        rep(i,2,N) read(fa[i]);
        for(int i=N;i>=2;i--)
             if(!vis[i]&&!vis[fa[i]]) ans++,vis[fa[i]]=1;
        if(ans*2>=K) printf("%d\n",(K+1)/2);
        else printf("%d\n",ans+(K-ans*2));
     }
     return 0;
}

HDU - 6178:Monkeys (貪心&樹上最大匹配輸&輸入優化)