And with the right set of preliminary investigation

Simple to learn a bit weighted disjoint-set reference this blog, feel well written: https://blog.csdn.net/yjr3426619/article/details/82315133

General disjoint-set and compared to the code changes in the main path and merge compression time. Weights between [x] as a weight belt, the path of compression time to update the weight (typically V [x] is represented by x and par, needs to be updated to the weights between the root of x x value). Then, when combined, to find the root px and py X and y, is connected to the px py (or even to px py) when the two weights must be calculated. Specific update code and look like the above blog, speak very clearly.

 

The following three topics recorded that he realized the blog in the examples.

hdu 3038: topic Link: https://vjudge.net/problem/HDU-3038

The effect is to give you a series of intervals and to determine how many and contradictory given before (if it is contradictory to skip). May be weighted disjoint-set do: seen as the interval endpoint nodes, the weights between two nodes and seen within this range. Given the current two nodes belong to the same set, and the interval thereof can be determined, and then determines whether or equal to a given value, if not equal ans ++; if not belong to the same set, put them corresponding roots were combined, and the weight is determined between two roots. The basic problem is the template, but there is a point to note: Every time should read as endpoint left open interval (-1 to make the left endpoint). Example: For example the interval [1,4], read in [1,2] and [1,4] may be determined after the [3,4], but a different set of time will actually executed as in 3, 4. But changed (0,2], and (2,4] does not have this problem, this time reading [3,4] is equivalent to seeking (2,4], 2, and 4 belong to the same set.

Code:

#include<cstdio>
#include<cstring>
using namespace std;

const int maxn=200+10;
int par[maxn],v[maxn];
int n,m,xx,x,y,t,i,j;

int find(int x){ //带权路径压缩 
	if (par[x]==x) return x;
	else{
		int p=par[x];
		par[x]=find(par[x]);
		v[x]+=v[p];
		return par[x];
	}
}

int main(){
	//freopen("hdu3038.txt","r",stdin);
	while (~scanf("%d%d",&n,&m)){
		int ans=0;
		for (i=0;i<=n;i++) par[i]=i;
		memset(v,0,sizeof(v));
		for (i=1;i<=m;i++){
			scanf("%d%d%d",&xx,&y,&t);
			x=xx-1; //
			int px=find(x);int py=find(y);
			if (px==py){
				int d=v[x]-v[y];  //***
				if (d!=t) ans++;
			} 
			else{
				par[px]=py;
				v[px]=v[y]-v[x]+t; //***
			}
		}
		printf("%d\n",ans);
	}
	//fclose(stdin);
	return 0;
}

 

  

hihocoder1515

Topic links: https://vjudge.net/problem/HihoCoder-1515

Water problem, set v [x] represents the ratio x x Current father par [x] low number of points on the line (I think it lower the number of points comfortable ......

Code:

#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=100+10;
int par[maxn],v[maxn]; //v[x]表示x比par[x]低多少分 
int s,n,m,q,t,i,j,x,y;

int find(int x){
	if (par[x]==x) return x;
	else{
		int p=par[x];
		par[x]=find(par[x]);
		v[x]+=v[p];
		return par[x]; 
	}
}

int main(){
	//freopen("hiho1515.txt","r",stdin);
	scanf("%d%d%d",&n,&m,&q);
	for (i=1;i<=n;i++) par[i]=i;
	memset(v,0,sizeof(v));
	for (i=1;i<=m;i++){
		scanf("%d%d%d",&x,&y,&s);s=-s;
		int px=find(x);int py=find(y);
		if (px!= py) { 
			for [px] = Py;
			v[px]=v[y]-v[x]+s;
		}
	}
	for (i=1;i<=q;i++){
		scanf("%d%d",&x,&y);
		int px=find(x);int py=find(y);
		if (px!=py) printf("-1\n");else printf("%d\n",v[y]-v[x]);
	}
	//fclose(stdin);
	return 0;
}

  

poj1182 food chain

Topic links: https://vjudge.net/problem/POJ-1182

This question should be regarded as a classic title, but also a little change. Own brain make for a successful get out (mainly to see the tips in a blog mode 3 ......)

Set v [x] = 0 and x represents PAR [x] is similar, v [x] = 1 x represents eat par [x], v [x ] = 2 x is represented PAR [x] eat. When updating the code to do the appropriate changes. First, when compressed path v [x] = (v [ x] + v [p])% 3, two right to find the x and y values of t, t = (v [x ] -v [y] +3 ) 3%, and the third is combined px py, seeking weights between px and py time, v [px] = (v [y] -v [x] + t + 3)% 3.

The code it:

#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=500+10;
int par[maxn],v[maxn];  //v[x]=0:xºÍpar[x]ͬÀà  v[x]=1:x³Ôpar[x]  v[x]=2:x±»par[x}³Ô 
int t,i,j,k,n,m,d,x,y,ans;

int find(int x){
	if (par[x]==x) return x;
	else{
		int p=par[x];
		par[x]=find(par[x]);
		v[x]=(v[p]+v[x])%3;  //* 
		return par[x];
	}
}

int main(){
	//freopen("poj1182.txt","r",stdin);
	scanf("%d%d",&n,&k);
	for (i=1;i<=n;i++) par[i]=i;
	memset(v,0,sizeof(v));
	ans=0;
	for (i=1;i<=k;i++){
		scanf("%d%d%d",&d,&x,&y);
		if (x<1||x>n||y<1||y>n) {
		    ++ years, continues;	
		}
		int px=find(x);int py=find(y);
		if (px==py){
			t=(v[x]-v[y]+3)%3; //*
			if (d==1&&t!=0) ans++;
			if (d==2&&t!=1) ans++;
		}
		else{
			t=d-1; //* 
			par[px]=py;
			v[px]=(v[y]-v[x]+t+3)%3; //*
		}
	}
	printf("%d\n",ans);
	//fclose(stdin);
	return 0;
}

 

  

 

Guess you like

Origin www.cnblogs.com/edmunds/p/12524390.html