标签:ret i++ cto dfs return ack max nod space
#include <bits/stdc++.h> using namespace std; const int N=1e5+7; vector<int>arr[N]; int minNode=0,minBalance=N; int du[N],n; int w[N]; int vis[N],bb[N]; void dfs(int u,int fa){ du[u]=1; int Tree_size=0,v; for(int i=0;i<arr[u].size();i++){ v=arr[u][i]; if(v!=fa){ dfs(v,u); du[u]+=du[v]; Tree_size=max(du[v],Tree_size); } } Tree_size=max(Tree_size,n-du[u]); if(Tree_size<minBalance){ minBalance=Tree_size; minNode=u; } } int DFS(int a){ for(int i=0;i<arr[a].size();i++){ if(bb[arr[a][i]]==0){ bb[arr[a][i]]=1; vis[arr[a][i]]=vis[a]^w[arr[a][i]]; DFS(arr[a][i]); } } } int main(){ int t,u,v; cin>>n; for(int i=1;i<=n;i++){ scanf("%d",w+i); } for(int i=0;i<n-1;i++){ scanf("%d%d",&u,&v); arr[u].push_back(v); arr[v].push_back(u); } dfs(1,0); cout<<minNode<<endl; vis[minNode]=w[minNode]; bb[minNode]=1; DFS(minNode); int m; cin>>m; while(m--){ scanf("%d%d",&u,&v); cout<<(vis[u]^vis[v])-1<<endl; } return 0; }
标签:ret i++ cto dfs return ack max nod space
原文地址:https://www.cnblogs.com/meanttobe/p/12079723.html