CodeForces
来源:互联网 发布:夏河淘宝日货是正品吗 编辑:程序博客网 时间:2024/06/14 17:39
题意:给出
思路:
Lemma : Let G be a simple graph. To every vertex of G we assign a nonnegative real number such that the sum of the numbers assigned to all vertices is 1. For any two connected vertices (by an edge), compute the product of the numbers associated to these vertices. The maximal value of the sum of these products is when assign equal numbers to a maximal clique (a subgraph that all of its vertices are connected to each other) and 0 to the rest of the graph.
简而言之就是直接把K平均分给最大团里的点
转自:点击打开链接
首先假如整个图是一个点数为
假如图中存在一个点数为
若存在一个点数为
基于以上结果,猜测答案取得最优时即为点集为最大团时。
下面就是求最大团的过程了。
由于一般图求最大团是NP-hard的,做的求最大团的题目,只做过436 ms
。
然而打开status一看,尽是15ms 30ms的代码,于是点开某30ms代码学习,发现了一个没接触过的搜索算法15ms
;第二种是带轴优化,对于一个与当前团内点有边相连但不在当前团内的点15ms
。
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;const int maxn = 1e2;int best, num[maxn], gra[maxn][maxn], n;bool dfs(int *adj, int tot, int cnt){ int tmp[maxn]; if(tot == 0) { if(best < cnt) { best = cnt; return true; } return false; } for(int i = 0; i < tot; i++) { if(cnt+tot-i <= best) return false; if(cnt+num[adj[i]] <= best) return false; int k = 0; for(int j = i+1; j < tot; j++) if(gra[adj[i]][adj[j]]) tmp[k++] = adj[j]; if(dfs(tmp, k, cnt+1)) return true; } return false;}int MaxClique(){ int adj[maxn]; best = 0; if(n <= 0) return 0; for(int i = n-1; i >= 0; i--) { int k = 0; for(int j = i+1; j < n; j++) if(gra[i][j]) adj[k++] = j; dfs(adj, k, 1); num[i] = best;// cout << k << endl; } return best;}int main(){ int K; scanf("%d%d", &n, &K); for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) scanf("%d", &gra[i][j]); int tmp = MaxClique();// cout << tmp << endl; double res = 0.5 * K * K * (tmp - 1) / tmp; printf("%.8f\n", res); return 0;}
另一种代码:
#include<cstdio>#include<cstring>#define N 1010bool flag[N], a[N][N];int ans, cnt[N], group[N], n, vis[N];// 最大团: V中取K个顶点,两点间相互连接// 最大独立集: V中取K个顶点,两点间不连接 // 最大团数量 = 补图中最大独立集数 bool dfs( int u, int pos ){ int i, j; for( i = u+1; i <= n; i++){ if( cnt[i]+pos <= ans ) return 0; if( a[u][i] ){ // 与目前团中元素比较,取 Non-N(i) for( j = 0; j < pos; j++ ) if( !a[i][ vis[j] ] ) break; if( j == pos ){ // 若为空,则皆与 i 相邻,则此时将i加入到 最大团中 vis[pos] = i; if( dfs( i, pos+1 ) ) return 1; } } } if( pos > ans ){ for( i = 0; i < pos; i++ ) group[i] = vis[i]; // 最大团 元素 ans = pos; return 1; } return 0;} void maxclique(){ ans=-1; for(int i=n;i>0;i--) { vis[0]=i; dfs(i,1); cnt[i]=ans; }}
- codeforces~~~
- Codeforces
- codeforces
- Codeforces
- codeforces
- codeforces
- Codeforces
- Codeforces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Listener refused the connection(ORA-12519, TNS:no appropriate service handler found)
- 从网络请求Json数据放到数据库实现删除修改
- 剑指offer面试题13 在O(1)时间删除链表节点
- 常用排序算法总结
- java 重载
- CodeForces
- 2018年网易校招内推编程题之彩色砖块
- 用python做数据分析4|pandas库介绍之DataFrame基本操作
- Android日历开发:日历显示需要准备哪些数据
- 最长回文串
- 2017多校训练Contest3: 1003 Kanade's sum hdu6058
- 9.2 随机变量
- 新零售将更加关注以人为本,以用户为核心
- Python 基于语法提取音节个数的方法(可能不完美,欢迎指教)