Bzoj 3365 distance statistics tree Versioning

Source: Internet
Author: User

Same as poj1741

Link: http://blog.csdn.net/popoqqq/article/details/39959803

Same as poj1741, local tyrants, and poj have the same question. After 1741, the question can be watermarked.

I will tell you that I directly changed the code for the above question 0.0.

The data range is 4 W. The maximum distance is 1000. It won't blow up Int. Do it with confidence.

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define M 40400using namespace std;struct abcd{int to,f,next;bool ban;}table[M<<1];int head[M],tot=1;int n,m,k,ans;int siz[M],fa[M],dist[M];int stack[M],top;void Add(int x,int y,int z){table[++tot].to=y;table[tot].f=z;table[tot].next=head[x];head[x]=tot;table[tot].ban=0;}void Get_Centre_Of_Gravity(int x,int size,int &cg){int i;bool flag=1;siz[x]=1;for(i=head[x];i;i=table[i].next)if(!table[i].ban)if(table[i].to!=fa[x]){fa[table[i].to]=x;Get_Centre_Of_Gravity(table[i].to,size,cg);if(siz[table[i].to]>size>>1)flag=0;siz[x]+=siz[table[i].to];}if(size-siz[x]>size>>1)flag=0;if(flag)cg=x;}void DFS1(int x,int dis){int i;dist[x]=dis;for(i=head[x];i;i=table[i].next){if(table[i].ban)continue;if(table[i].to==fa[x])continue;fa[table[i].to]=x;DFS1(table[i].to,dis+table[i].f);}}void DFS2(int x){int i;stack[++top]=dist[x];for(i=head[x];i;i=table[i].next){if(table[i].ban)continue;if(table[i].to==fa[x])continue;DFS2(table[i].to);}}int Calculate(int root){int i,j,re=0;top=0;DFS2(root);sort(stack+1,stack+top+1);for(i=1,j=top;i<=top;i++){for(;j&&stack[j]+stack[i]>k;j--);re+=j;}return re;}void Tree_Divide_And_Conquer(int root,int size){int i,cg;fa[root]=0;Get_Centre_Of_Gravity(root,size,cg);siz[fa[cg]]=size-siz[cg];fa[cg]=0;DFS1(cg,0);ans+=Calculate(cg);for(i=head[cg];i;i=table[i].next)if(!table[i].ban){table[i].ban=table[i^1].ban=1;ans-=Calculate(table[i].to);Tree_Divide_And_Conquer(table[i].to,siz[table[i].to]);}}int main(){int i,x,y,z;char p[10];cin>>n>>m;for(i=1;i<n;i++)scanf("%d%d%d%s",&x,&y,&z,p),Add(x,y,z),Add(y,x,z);cin>>k;Tree_Divide_And_Conquer(1,n);cout<<(ans-n>>1)<<endl;}


Bzoj 3365 distance statistics tree Versioning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.