1 solutions

  • 0
    @ 2024-12-10 23:10:15

    C++ :

    /*
    	author  :hzoi_ztx
    	title   :砍树 
    	ALG     :树形 dp 
    	comment :
    	[2014 10 10 test]
    */
    
    #include <cstdio>
    #include <algorithm>
    
    #define  maxn  1000010
    #define  maxt  1000010
    #define  root  1
    
    
    int n , x , y ;
    int A ;
    bool flag[maxn] = {false} ;
    int ans[maxn] = {0} ;
    
    
    struct FST{
    	int to , next ;
    } e[maxn*2] ; int star[maxn] = {0} , tot_e = 0 ;
    
    void addEdge(int u , int v) {
    	tot_e ++ ;
    	e[tot_e].to = v ; e[tot_e].next = star[u] ; star[u] = tot_e ;
    	tot_e ++ ;
    	e[tot_e].to = u ; e[tot_e].next = star[v] ; star[v] = tot_e ;
    }
    
    inline int max(const int a , const int b) {return a > b ? a : b ; }
    
    struct Tree{
    	int id ;
    	int lc , rc ;
    	int sum , max ;
    	Tree() { lc = rc = max = 0 ; sum = 1 ; }
    } T[maxt] ; int tot = 0 ;
    
    void addson(int fat , int son) {
    	if (T[fat].max < T[son].sum) T[fat].max = T[son].sum ;
    	T[fat].sum += T[son].sum ;
    	if (!T[fat].lc) T[fat].lc = son ;
    	else {
    		fat = T[fat].lc ;
    		if (T[fat].rc) while (fat = T[fat].rc , T[fat].rc) ;
    		T[fat].rc = son ;
    	}
    }
    
    void dfs(int u , int v) {
    	int p = star[v] , now = ++tot , son ;
    	T[now].id = v ;
    	while (p) {
    		if (e[p].to == u) {p = e[p].next ; continue ; }
    		son = tot+1 ;
    		dfs(v , e[p].to) ;
    		addson(now , son) ;
    		p = e[p].next ;
    	}
    }
    
    void solve(int M,int now) {
    	if (T[now].max <= A) ans[++ans[0]] = T[now].id ;
    	M += T[now].sum ;
    	int p = T[now].lc ;
    	if (p) {
    		if (M-T[p].sum <= A) solve(M-T[p].sum , p) ;
    		p = T[p].rc ;
    		while (p) {
    			if (M-T[p].sum <= A) solve(M-T[p].sum , p) ;
    			p = T[p].rc ;
    		}
    	}
    }
    
    int main() {
    	//#define READ
    	#ifdef  READ
    		freopen(".in" ,"r",stdin ) ;
    		freopen(".out","w",stdout) ;
    	#endif
    	scanf("%d", &n ) ; A = n>>1 ;
    	for (int i = 1 ; i < n ; i ++ ) {
    		scanf("%d%d", &x , &y ) ; addEdge(x , y) ;
    	}
    	dfs(root , root) ;
    	solve(0,root) ;
    	if (!ans[0]) printf("NONE\n") ;
    	else {
    		std::sort(ans+1 , ans+ans[0]+1) ;
    		for (int i = 1 ; i <= ans[0] ; i ++ ) printf("%d\n", ans[i] ) ;
    	}
    	return 0 ;
    }
    
    • 1

    Information

    ID
    588
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By