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"<