Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for bricks. The company has its own code describing each type of a truck. The code is simply a string of exactly seven lowercase letters (each letter on each position has a very special meaning but that is unimportant for this task). At the beginning of company’s history, just a single truck type was used but later other types were derived from it, then from the new types another types were derived, and so on.
1/Σ(to,td)d(to,td)
where the sum goes over all pairs of types in the derivation plan such that t o is the original type and t d the type derived from it and d(t o,t d) is the distance of the types.
Since historians failed, you are to write a program to help them. Given the codes of truck types, your program should find the highest possible quality of a derivation plan.
Today, ACM is rich enough to pay historians to study its history. One thing historians tried to find out is so called derivation plan — i.e. how the truck types were derived. They defined the distance of truck types as the number of positions with different letters in truck type codes. They also assumed that each truck type was derived from exactly one other truck type (except for the first truck type which was not derived from any other type). The quality of a derivation plan was then defined as
where the sum goes over all pairs of types in the derivation plan such that t o is the original type and t d the type derived from it and d(t o,t d) is the distance of the types.
Since historians failed, you are to write a program to help them. Given the codes of truck types, your program should find the highest possible quality of a derivation plan.
Input
The input consists of several test cases. Each test case begins with a line containing the number of truck types, N, 2 <= N <= 2 000. Each of the following N lines of input contains one truck type code (a string of seven lowercase letters). You may assume that the codes uniquely describe the trucks, i.e., no two of these N lines are the same. The input is terminated with zero at the place of number of truck types.
Output
For each test case, your program should output the text “The highest possible quality is 1/Q.”, where 1/Q is the quality of the best derivation plan.
Sample Input
4 aaaaaaa baaaaaa abaaaaa aabaaaa 0
Sample Output
The highest possible quality is 1/3.
题意:d[i][j]=s[i]与s[j]中同个位置不同的字符数
题解:没有
K树:
#include<algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cassert> #include <cstdio> #include <vector> #include <string> #include <cmath> #include <queue> #include <stack> #include <set> #include <map> using namespace std; #define P(a,b,c) make_pair(a,make_pair(b,c)) #define rep(i,a,n) for (int i=a;i<=n;i++) #define per(i,a,n) for (int i=n;i>=a;i--) #define CLR(vis) memset(vis,0,sizeof(vis)) #define MST(vis,pos) memset(vis,pos,sizeof(vis)) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef pair<int,pair<int,int> >pii; typedef long long ll; const ll mod = 1000000007; ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; } const int MAXN=11000;//最大点数 const int MAXM=4000005;//最大边数 int F[MAXN];//并查集使用 struct Edge { int u,v,w; }edge[MAXM];//存储边的信息,包括起点/终点/权值 int tol;//边数,加边前赋值为0 void addedge(int u,int v,int w) { edge[tol].u=u; edge[tol].v=v; edge[tol++].w=w; } bool cmp(Edge a,Edge b) {//排序函数,讲边按照权值从小到大排序 return a.w<b.w; } int find(int x) { if(F[x]==-1)return x; else return F[x]=find(F[x]); } int Kruskal(int n)//传入点数,返回最小生成树的权值,如果不连通返回-1 { memset(F,-1,sizeof(F)); sort(edge,edge+tol,cmp); int cnt=0;//计算加入的边数 int ans=0; for(int i=0;i<tol;i++) { int u=edge[i].u; int v=edge[i].v; int w=edge[i].w; int t1=find(u); int t2=find(v); if(t1!=t2) { ans+=w; F[t1]=t2; cnt++; } if(cnt==n-1)break; } if(cnt<n-1)return -1;//不连通 else return ans; } char s[10000][10]; int cost(int i,int j){ int sum=0; rep(h,0,6){ if(s[i][h]!=s[j][h])sum++; } return sum; } int main(){ int n,flag=1; while(scanf("%d",&n)&&n){ tol=0; rep(i,1,n)scanf("%s",s[i]); rep(i,1,n){ rep(j,1,n){ if(j>i){ addedge(i,j,cost(i,j)); } } } if(flag)flag=0; else printf("\n"); printf("The highest possible quality is 1/%d.", Kruskal(n)); } return 0; }
P树:
#include<algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cassert> #include <cstdio> #include <vector> #include <string> #include <cmath> #include <queue> #include <stack> #include <set> #include <map> using namespace std; #define P(a,b,c) make_pair(a,make_pair(b,c)) #define rep(i,a,n) for (int i=a;i<=n;i++) #define per(i,a,n) for (int i=n;i>=a;i--) #define CLR(vis) memset(vis,0,sizeof(vis)) #define MST(vis,pos) memset(vis,pos,sizeof(vis)) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef pair<int,pair<int,int> >pii; typedef long long ll; const ll mod = 1000000007; ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; } const int INF=0x3f3f3f3f; const int MAXN=2018; bool vis[MAXN]; int lowc[MAXN]; int cost[MAXN][MAXN]; int Prim(int n)//点是0~n-1 { int ans=0; memset(vis,false,sizeof(vis)); vis[0]=true; for(int i=1;i<n;i++)lowc[i]=cost[0][i]; for(int i=1;i<n;i++){ int minc=INF; int p=-1; for(int j=0;j<n;j++) if(!vis[j]&&minc>lowc[j]){ minc=lowc[j]; p=j; } if(minc==INF)return -1;//原图不连通 ans+=minc; vis[p]=true; for(int j=0;j<n;j++) if(!vis[j]&&lowc[j]>cost[p][j]) lowc[j]=cost[p][j]; } return ans; } char s[10000][10]; int costt(int i,int j){ int sum=0; rep(h,0,6){ if(s[i][h]!=s[j][h])sum++; } return sum; } int main(){ int n,flag=1; while(scanf("%d",&n)&&n){ // tol=0; rep(i,0,n-1)scanf("%s",s[i]); rep(i,0,n-1){ rep(j,0,n-1){ if(j>i){ // addedge(i,j,cost(i,j)); cost[j][i]=cost[i][j]=costt(i,j); } } } if(flag)flag=0; else printf("\n"); printf("The highest possible quality is 1/%d.", Prim(n)); } return 0; }