# Bzoj5101 [poi2018]powódź

## 并查集判断连通性

Posted by yjjr's blog on April 7, 2018

# 题目

## Sample Input

3 2 2
1
1
1
1 2
1 1


## Sample Output

65


# 分析

g数组表示方案数，h数组表示当前水位，不过多解释（懒）

# code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define reg(x) for(int i=last[x];i;i=e[i].next)
using namespace std;
ll f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
#define pos(x,y) (x-1)*m+y
const int mod=1e9+7,maxn=5e5+6;
int n,m,H,tot,fa[maxn],h[maxn];ll g[maxn],ans;
struct node{int x,y,v;}p[maxn<<1];
inline bool cmp(node a,node b){return a.v<b.v;}
inline int find(int x){return x==fa[x]?fa[x]:fa[x]=find(fa[x]);}
int main()
{
sort(p+1,p+1+tot,cmp);
rep(i,1,n*m)fa[i]=i,g[i]=1,h[i]=0;
rep(i,1,tot){
int r1=find(p[i].x),r2=find(p[i].y);
if(r1!=r2){
g[fa[p[i].y]]=(g[fa[p[i].x]]+p[i].v-h[fa[p[i].x]])*(g[fa[p[i].y]]+p[i].v-h[fa[p[i].y]])%mod;
h[fa[p[i].y]]=p[i].v,fa[r1]=r2;
}
}
cout<<(g[find(1)]+H-h[find(1)])%mod<<endl;
return 0;
}