zoukankan      html  css  js  c++  java
  • HDU 6178 Monkeys【dfs】【输入外挂模板】

    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 == '
    ' || ch == '
    ' || ch == '	';
        }
    
        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
    ",ans+m-2*ans);
            }
            else
                printf("%d
    ",(m-1)/2+1);
        }
        return 0;
    }


    Fighting~
  • 相关阅读:
    Unity3D使用OpenFileDialog后崩溃
    JS定时器
    如果把我剥得一文不名丢在沙漠的中央,只要一行驼队经过———我就可以重建整个商业帝国
    软件开发架构
    C#对象序列化与反序列化
    我的作品们
    fread 不能读取最后一个数据块
    for循环中的i++和++i
    MOS管不能关断的原因!!!
    AD16 快速原理图封装导出
  • 原文地址:https://www.cnblogs.com/Archger/p/12774677.html
Copyright © 2011-2022 走看看