hdu5416树形dp

hdu5416树形dp_第1张图片

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.NavigableSet;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeSet;

import org.omg.CORBA.Object;




public class Main {


    public static void main(String[] args) throws IOException{

           StreamTokenizer cin = new StreamTokenizer(new BufferedInputStream(System.in)); 
           InputReader in = new InputReader(System.in)  ;
           PrintWriter out = new PrintWriter(System.out) ;

           int t = in.nextInt() ;
           while(t-- > 0){

                new Task().solve(in, out)  ;   //out.flush() ;
           }

           out.flush() ; 


    }


}

class   Task{
        static final int N = 100002 ; 

        static class Edge{
               int v ;
               int w ;
               int next ;

               public Edge(int v , int w , int next) {
                      this.v = v ;
                      this.w = w ;
                      this.next = next ;
               }
        }

        static Edge[] e = new Edge[N*2] ;
        int id = 0 ;
        static int[] g = new int[N] ;

        void add(int u , int v , int w){
             e[id] = new Edge(v, w, g[u]) ;
             g[u] = id++ ;
        }

        static int[] dp = new int[2*N] ;

        void  dfs(int u , int father , int val){
              dp[val]++ ;
              for(int i = g[u] ; i != -1 ; i = e[i].next){
                    int v = e[i].v ;
                    if(v == father) continue ;
                    dfs(v , u , val ^ e[i].w) ;
              }
        }

        int ask ;
        long sum  ;

        void doit(int u , int father , int val){
             sum += dp[ask ^ val] ;
             for(int i = g[u] ; i != -1 ; i = e[i].next){
                    int v = e[i].v ;
                    if(v == father) continue ;
                    doit(v , u , val ^ e[i].w) ;
             }
        }


        public void solve(InputReader in , PrintWriter out) throws IOException{
               int n = in.nextInt() ;
               Arrays.fill(g, 1, n+1, -1);
               for(int i = 1 ; i < n ; i++){
                     int u = in.nextInt() ;
                     int v = in.nextInt() ;
                     int w = in.nextInt() ;
                     add(u , v , w) ;
                     add(v , u , w) ;
               }
               Arrays.fill(dp, 0) ;
               dfs(1 , -1 , 0) ;

               int q = in.nextInt() ;
               while(q-- > 0){
                    ask = in.nextInt() ; 
                    sum = 0 ;
                    doit(1, -1, 0) ;
                    if(ask == 0) sum += n ;
                    out.println(sum>>1);
               }
        }

}


class InputReader{
        public BufferedReader  reader;
        public StringTokenizer  tokenizer;

        public InputReader(InputStream stream){
                    reader = new BufferedReader(new InputStreamReader(stream), 32768) ;
                    tokenizer = null ;
        }

        public String next(){
                    while(tokenizer == null || ! tokenizer.hasMoreTokens()){
                        try{
                                tokenizer = new StringTokenizer(reader.readLine());
                        }catch (IOException e) {
                                throw new RuntimeException(e);
                        }
                    }
                    return tokenizer.nextToken();  
        }

        public int  nextInt() {
                    return Integer.parseInt(next());
        }

        public long nextLong() {
                    return Long.parseLong(next());
        }

}

你可能感兴趣的:(hdu5416树形dp)