zoukankan      html  css  js  c++  java
  • LuoguP3047 [USACO12FEB]附近的牛Nearby Cows(树形DP,容斥)

    [f[u][step] = egin{cases} C[u] & step = 0 \ (sum{f[v][step - 1]}) - f[u][step - 2] cdot (deg[u] - 1) & 1 leq step < maxSteps end{cases}]

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
    #define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
    #define Max(a,b) ((a) > (b) ? (a) : (b))
    #define Min(a,b) ((a) < (b) ? (a) : (b))
    #define Fill(a,b) memset(a, b, sizeof(a))
    #define Abs(a) ((a) < 0 ? -(a) : (a))
    #define Swap(a,b) a^=b^=a^=b
    #define ll long long
    
    #define ON_DEBUG
    
    #ifdef ON_DEBUG
    
    #define D_e_Line printf("
    
    ----------
    
    ")
    #define D_e(x)  cout << #x << " = " << x << endl
    #define Pause() system("pause")
    #define FileOpen() freopen("in.txt","r",stdin);
    
    #else
    
    #define D_e_Line ;
    #define D_e(x)  ;
    #define Pause() ;
    #define FileOpen() ;
    
    #endif
    
    struct ios{
        template<typename ATP>ios& operator >> (ATP &x){
            x = 0; int f = 1; char c;
            for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
            while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
            x*= f;
            return *this;
        }
    }io;
    using namespace std;
    
    const int N = 100007;
    
    int n, K;
    
    struct Edge{
    	int nxt, pre;
    }e[N << 1];
    int head[N], cntEdge;
    inline void add(int u, int v){
    	e[++cntEdge] = (Edge){ head[u], v}, head[u] = cntEdge;
    }
    
    int f[N][23];
    int in[N];
    int main(){
    	io >> n >> K;
    	R(i,2,n){
    		int u, v;
    		io >> u >> v;
    		add(u, v),
    		add(v, u),
    		++in[u],
    		++in[v];
    	}
    	
    	R(i,1,n){
    		io >> f[i][0];
    	}
    	
    	R(k,1,K){
    		R(u,1,n){
    			for(register int i = head[u]; i; i = e[i].nxt){
    				f[u][k] += f[e[i].pre][k - 1];
    			}
    			if(k > 1)
    				f[u][k] -= f[u][k - 2] * (in[u] - 1);
    			else
    				f[u][k] += f[u][0];
    		}
    	}
    	
    	R(i,1,n){
    		printf("%d
    ", f[i][K]);
    	}
    	
    	return 0;
    }
    

  • 相关阅读:
    算法复杂度(Algorithmic Complexity)
    Javascript引用指针
    树(Trees)
    什么是数据挖掘(What is Data Mining?)
    Javascript的声明
    Javascript继承(下)——干嘛要继承
    Linux 中使用 OpenCV
    Java集合类(Java Collections classes)
    哈希查找(Hashing)
    Javascript继承(上)——对象构建
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11252586.html
Copyright © 2011-2022 走看看