BZOJ 3454 family and query set

Source: Internet
Author: User

BZOJ 3454 family and query set

Given an undirected graph, each vertex has Edge Weight, giving each connected block a degree of preference, and finding a minimum interval, the maximum number of happy degrees when all edges in this interval are retained

N <= 1000,M <= 5000

No rule of law series ......

If the brute-force enumeration interval is used and the favorite degree is calculated each time, the time complexity is O (nm ^ 2), and the timeout is

Fix a left endpoint and shift the right endpoint to the right. Use and query the edge adding in the set each time and maintain the sum of the favorite degrees. The time complexity is O (m ^ 2)

Then the question is done =

#include 
 
  #include 
  
   #include 
   
    #include #define M 1010using namespace std;struct abcd{int x,y,f;friend istream& operator >> (istream &_,abcd &a){scanf("%d%d%d",&a.x,&a.y,&a.f);return _;}bool operator < (const abcd &a) const{return fsize[y])swap(x,y);sum-=w[size[x]];sum-=w[size[y]];fa[x]=y;size[y]+=size[x];sum+=w[size[y]];}}int main(){using namespace Union_Find_Set;int i,j;cin>>n>>m>>k;for(i=1;i<=n;i++)scanf("%d",&w[i]);for(i=1;i<=m;i++)cin>>edges[i];sort(edges+1,edges+m+1);for(i=1;i<=m;i++)if(i==1||edges[i].f!=edges[i-1].f){sum=(long long)n*w[1];memset(fa,0,sizeof fa);for(j=i;j<=m;j++){Union(edges[j].x,edges[j].y);if(j==m||edges[j].f!=edges[j+1].f)if(sum>=k){ans=min(ans,edges[j].f-edges[i].f);break;}}}if(ans==2147483647)cout<<"T_T"<
    
     

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.