PKUACM 2018 D chocolate【并查集+克鲁斯卡尔】
2018-07-19 22:24
295 查看
传送:http://poj.openjudge.cn/practice/C18D/
依然是课件截图
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> using namespace std; const int N=405,mod=1e9+7,inf=2e9; int T,n,d ,f ; long long a ; char s ; struct qwe { int u,v,w; }e[N*N]; bool cmp(const qwe &a,const qwe &b) { return a.w<b.w; } int dis(char a[],char b[]) { int la=strlen(a+1)+1,lb=strlen(b+1)+1; for(int i=1;i<=la;i++) d[i][1]=i; for(int i=1;i<=lb;i++) d[1][i]=i; for(int i=2;i<=la;i++) for(int j=2;j<=lb;j++) { if(a[i-1]==b[j-1]) d[i][j]=d[i-1][j-1]; else d[i][j]=min(d[i][j-1],d[i-1][j])+1; } return d[la][lb]; } int zhao(int x) { return x==f[x]?x:f[x]=zhao(f[x]); } int hb(int x,int y) { x=zhao(x),y=zhao(y); if(x==y) return x; f[x]=y; return y; } bool ok(int x,int cnt) { int mn=inf,mx=-inf; for(int i=1;i<=cnt;i++) { int fu=zhao(e[i].u),fv=zhao(e[i].v); if(fu==x&fv==x) mx=max(mx,e[i].w); else if((fu==x&&fv!=x)||(fu!=x&&fv==x)) mn=min(mn,e[i].w); } return mx<mn; } int main() { scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=1;i<=n;i++) f[i]=i,a[i]=1; for(int i=1;i<=n;i++) scanf("%s",s[i]+1); int cnt=0; for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) e[++cnt]=(qwe){i,j,dis(s[i],s[j])}; sort(e+1,e+1+cnt,cmp); long long ans=0; int tot=n; for(int i=1;i<=cnt;i++) { int fu=zhao(e[i].u),fv=zhao(e[i].v); if(fu==fv) continue; int nw=hb(e[i].u,e[i].v); a[nw]=a[fu]*a[fv]%mod; if(ok(nw,cnt)) a[nw]=(a[nw]+1)%mod; tot--; if(tot==1) ans=a[nw]; } printf("%lld\n",ans); } return 0; }
相关文章推荐
- 贪心生成最小生成树-克鲁斯卡尔(Kruskal)算法(归并排序)(并查集)
- hdu 1879(并查集+克鲁斯卡尔)
- 51nod 1640天气晴朗的魔法(克鲁斯卡尔,并查集)
- hdu1102 克鲁斯卡尔+并查集
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
- POJ 1258 Agri-Net 克鲁斯卡尔(Kruskal)算法&并查集
- HDU 3371 Connect the Cities (最小生成树 并查集+克鲁斯卡尔)
- contesthunter 6201 走廊泼水节【克鲁斯卡尔+并查集】
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
- 深度优先DFS和广度优先BFS,破圈法,拓扑序列,prim,克鲁斯卡尔等生成算法(需要用到并查集)迪杰斯特拉算法和弗洛伊德的总结
- HDU-3938 Portal 克鲁斯卡尔思想 (离线并查集)
- 最小生成树算法汇总 (普里姆 && 克鲁斯卡尔与并查集结合)
- java实现图的最小生成树(森林)MST克鲁斯卡尔(Kruskal)算法
- 最小生成树MST-克鲁斯卡尔(Kruskal)算法
- 【类克鲁斯卡尔做法+枚举最小边】【HDU1598】【find the most comfortable road】
- hlgChocolate Auction【并查集】
- 关于最小生成树中的 Kruskal(克鲁斯卡尔)算法
- POJ-1251-Jungle Roads(最小生成树 克鲁斯卡尔)
- [您有新的未分配科技点][BZOJ3545&BZOJ3551]克鲁斯卡尔重构树
- 数据结构之(图之最小生成树)Kruskal(克鲁斯卡尔)算法