# BZOJ 5101([POI2018]Powód-kruskal+dp)

Input

Output

ansi $ans_i$为连通块i的方案。

ansi=ansxansy+Ww $ans_i=ans_x*ans_y + W-w$(W为这个连通块水位的上界）

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#define ALL(x) (x).begin(),(x).end()
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define MAXN (500000<<2)
int n,m,fa[MAXN];
int getfa(int x) {return (fa[x]==x)?x:(fa[x]=getfa(fa[x]));}
pair<int, pi > e[MAXN];
int id(int i,int j){return (i-1)*m+j;}
ll ans[MAXN],c[MAXN]={};
int main()
{
// freopen("bzoj5101.in","r",stdin);
// freopen(".out","w",stdout);
int cnt=0;
For(i,n) {
For(j,m-1) {
int p=id(i,j),q=id(i,j+1);
}
}
For(i,n-1) {
For(j,m) {
int p=id(i,j),q=id(i+1,j);
}
}
For(i,n*m)fa[i]=i,ans[i]=1;
sort(e+1,e+1+cnt);
int tot=n*m;
For(i,cnt) {
int x=e[i].se.fi,y=e[i].se.se,w=e[i].fi;
x=getfa(x),y=getfa(y);
if (x^y) {
++tot;
fa[x]=fa[y]=tot; fa[tot]=tot; c[tot]=w;
ans[x] += w-c[x];  ans[y]+=w-c[y];
ans[x]%=F; ans[y]%=F;
ans[tot]=ans[x]*ans[y]%F;
}
}
cout<<(ans[tot]+H-c[tot])%F<<endl;

return 0;
}