zoukankan      html  css  js  c++  java
  • 敢死队

    /*
    G将军有一支训练有素的军队,这个军队除开G将军外,每名士兵都有一个直接上级(可能是其他士兵,也可能是G将军)。现在G将军将接受一个特别的任务,需要派遣一部分士兵(至少一个)组成一个敢死队,为了增加敢死队队员的独立性,要求如果一名士兵在敢死队中,他的直接上级不能在敢死队中。
    请问,G将军有多少种派出敢死队的方法。注意,G将军也可以作为一个士兵进入敢死队。
    输入格式
    输入的第一行包含一个整数n,表示包括G将军在内的军队的人数。军队的士兵从1至n编号,G将军编号为1。
    接下来n-1个数,分别表示编号为2, 3, ..., n的士兵的直接上级编号,编号i的士兵的直接上级的编号小于i。
    输出格式
    输出一个整数,表示派出敢死队的方案数。由于数目可能很大,你只需要输出这个数除10007的余数即可。
    样例输入1
    3
    1 1
    样例输出1
    4
    样例说明
    这四种方式分别是:
    1. 选1;
    2. 选2;
    3. 选3;
    4. 选2, 3。
    样例输入2
    7
    1 1 2 2 3 3
    样例输出2
    40
    
    数据规模与约定
    对于20%的数据,n ≤ 20;
    对于40%的数据,n ≤ 100;
    对于100%的数据,1 ≤ n ≤ 100000。
    
    资源约定:
    峰值内存消耗 < 256M
    CPU消耗  < 2000ms
    
    请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
    
    所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
    
    注意: main函数需要返回0
    注意: 只使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。
    注意: 所有依赖的函数必须明确地在源文件中 #include <xxx>, 不能通过工程设置而省略常用头文件。
    
    提交时,注意选择所期望的编译器类型。
    */
    package test;
    
    import java.util.Scanner;  
    import java.util.Vector;  
      
    public class 敢死队 {  
      
        static class Node{  
            Vector<Integer> subordinate = new Vector<Integer>();  
            int[] dp = new int[2];  
        }  
          
        static Node node[];  
        public static void main(String[] args) {  
            Scanner input = new Scanner(System.in);  
            int N = input.nextInt();  
            node = new Node[N+1];  
            for (int i = 1; i < node.length; i++) { //初始化 
                node[i] = new Node();  
                node[i].dp[0] = 1;  
                node[i].dp[1] = 1;  
            }  
              
            for (int i = 2; i < node.length; i++) {  
                int m = input.nextInt();  
                node[m].subordinate.add(i);//储存下级。  
            }  
              
            for (int i = N; i > 0 ; i--) {  
                int sum = node[i].subordinate.size();//得到该士兵的下级数量  
                for (int j = 0; j < sum; j++) {  
                    int son = node[i].subordinate.get(j);  
                    node[i].dp[1] *= node[son].dp[0];  //选我的组合=所有下属不选的组合
                    node[i].dp[0] *= (node[son].dp[0]+node[son].dp[1]);  //不选我的组合=所有下属选+不选的组合
                    node[i].dp[1] %= 10007;  
                    node[i].dp[0] %= 10007;  
                }  
            }  
              
            int ans = (node[1].dp[0]+node[1].dp[1]-1)%10007;//减去所有都没有去的情况  
            System.out.println(ans);  
        }  
    }  
  • 相关阅读:
    【校招面试 之 C/C++】第23题 C++ STL(五)之Set
    Cannot create an instance of OLE DB provider “OraOLEDB.Oracle” for linked server "xxxxxxx".
    Redhat Linux安装JDK 1.7
    ORA-10635: Invalid segment or tablespace type
    Symantec Backup Exec 2012 Agent for Linux 卸载
    Symantec Backup Exec 2012 Agent For Linux安装
    You must use the Role Management Tool to install or configure Microsoft .NET Framework 3.5 SP1
    YourSQLDba介绍
    PL/SQL重新编译包无反应
    MS SQL 监控数据/日志文件增长
  • 原文地址:https://www.cnblogs.com/ljs-666/p/8570067.html
Copyright © 2011-2022 走看看