There are n castles in the Lannister's Kingdom and some walls connect two castles, no two castles are connected by more than one wall, no wall connects a castle to itself.
Sir Jaime Lannister has discovered that Daenerys Targaryen is going to attack his kingdom soon. Therefore he wants to defend his kingdom. He has k liters of a strange liquid. He wants to distribute that liquid among the castles, so each castle may contain some liquid (possibly zero or non-integer number of liters). After that the stability of a wall is defined as follows: if the wall connects two castles a and b, and they contain x and y liters of that liquid, respectively, then the strength of that wall is x·y.
Your task is to print the maximum possible sum of stabilities of the walls that Sir Jaime Lannister can achieve.
The first line of the input contains two integers n and k (1 ≤ n ≤ 40, 1 ≤ k ≤ 1000).
Then n lines follows. The i-th of these lines contains n integers ai, 1, ai, 2, ..., ai, n (). If castles i and j are connected by a wall, then ai, j = 1. Otherwise it is equal to 0.
It is guaranteed that ai, j = aj, i and ai, i = 0 for all 1 ≤ i, j ≤ n.
Print the maximum possible sum of stabilities of the walls that Sir Jaime Lannister can achieve.
Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .
3 1
0 1 0
1 0 0
0 0 0
0.250000000000
4 4
0 1 0 1
1 0 1 0
0 1 0 1
1 0 1 0
4.000000000000
In the first sample, we can assign 0.5, 0.5, 0 liters of liquid to castles 1, 2, 3, respectively, to get the maximum sum (0.25).
In the second sample, we can assign 1.0, 1.0, 1.0, 1.0 liters of liquid to castles 1, 2, 3, 4, respectively, to get the maximum sum (4.0)
首先存在一个引理:对于简单图G,给每一个G的顶点赋一非负实数值,使所有顶点的值之和为1.对于任意一边连接的两点,计算其权值乘积,并将整个图的所有权值乘积求和。则最大值当且仅当图中最大团每个顶点权值相等,而其余顶点权值为0时取得。
CF上的官方题解有详细证明,非常精彩。
有了这样的引理之后问题就变得简单了,求出最大团的顶点数即可。
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <vector> 5 #include <set> 6 #include <map> 7 #include <string> 8 #include <cstring> 9 #include <stack> 10 #include <queue> 11 #include <cmath> 12 #include <ctime> 13 #include<bitset> 14 #include <utility> 15 using namespace std; 16 #define REP(I,N) for (I=0;I<N;I++) 17 #define rREP(I,N) for (I=N-1;I>=0;I--) 18 #define rep(I,S,N) for (I=S;I<N;I++) 19 #define rrep(I,S,N) for (I=N-1;I>=S;I--) 20 #define FOR(I,S,N) for (I=S;I<=N;I++) 21 #define rFOR(I,S,N) for (I=N;I>=S;I--) 22 #define rank rankk 23 #define DFT FFT 24 typedef unsigned long long ull; 25 typedef long long ll; 26 const int INF=0x3f3f3f3f; 27 const ll INFF=0x3f3f3f3f3f3f3f3fll; 28 //const ll M=1e9+7; 29 const ll maxn=2e5+7; 30 const int MAXN=1005; 31 const int MAX=1e5+5; 32 const int MAX_N=MAX; 33 const int N=55; 34 const ll MOD=1e9+7; 35 //const double eps=0.00000001; 36 //ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} 37 template<typename T>inline T abs(T a) {return a>0?a:-a;} 38 inline ll powMM(ll a,ll b,ll M){ 39 ll ret=1; 40 a%=M; 41 // b%=M; 42 while (b){ 43 if (b&1) ret=ret*a%M; 44 b>>=1; 45 a=a*a%M; 46 } 47 return ret; 48 } 49 void open() 50 { 51 freopen("1004.in","r",stdin); 52 freopen("out.txt","w",stdout); 53 } 54 55 struct MAX_CLIQUE { 56 static const int N=60; 57 58 bool G[N][N]; 59 int n, Max[N], Alt[N][N], ans; 60 61 bool DFS(int cur, int tot) { 62 if(cur==0) { 63 if(tot>ans) { 64 ans=tot; 65 return 1; 66 } 67 return 0; 68 } 69 for(int i=0; i<cur; i++) { 70 if(cur-i+tot<=ans) return 0; 71 int u=Alt[tot][i]; 72 if(Max[u]+tot<=ans) return 0; 73 int nxt=0; 74 for(int j=i+1; j<cur; j++) 75 if(G[u][Alt[tot][j]]) Alt[tot+1][nxt++]=Alt[tot][j]; 76 if(DFS(nxt, tot+1)) return 1; 77 } 78 return 0; 79 } 80 81 int MaxClique() { 82 ans=0, memset(Max, 0, sizeof Max); 83 for(int i=n-1; i>=0; i--) { 84 int cur=0; 85 for(int j=i+1; j<n; j++) if(G[i][j]) Alt[1][cur++]=j; 86 DFS(cur, 1); 87 Max[i]=ans; 88 } 89 return ans; 90 } 91 }; 92 93 MAX_CLIQUE edge; 94 int k; 95 int main() { 96 scanf("%d%d", &edge.n,&k); 97 for(int i=0; i<edge.n; i++) 98 for(int j=0; j<edge.n; j++) 99 scanf("%d", &edge.G[i][j]); 100 int da=edge.MaxClique(); 101 printf("%.7f ",(double)k*k*(da-1)/(2.0*da)); 102 return 0; 103 }