称号:http://community.topcoder.com/stat?c=problem_statement&pm=13251&rd=16017

參考:http://apps.topcoder.com/wiki/display/tc/TCO+2014+Round+2C

假设用先计算出每条边,用邻接矩阵来表示图,然后用BFS或 Floyd-Warshall算法来计算距离的话。时间复杂度是O(N^3),会超时。依据题名的提示知要利用clique graph的性质来做。基本思想是在BFS的时候将一个clique看成一个总体。一旦訪问到clique中的一个点,则这个clique中全部点的距离都能够得到。算法描写叙述例如以下

for each source vertex s:

    mark all vertices and all cliques as unvisited

    start BFS from s

    when processing a vertex v during the BFS:

        for each unvisited clique C that contains v:

            mark C as visited

            use edges in C to discover new vertices

代码:

#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <iostream>
#include <sstream>
#include <iomanip> #include <bitset>
#include <string>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <set>
#include <map> #include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std; #define CHECKTIME() printf("%.2lf\n", (double)clock() / CLOCKS_PER_SEC)
typedef pair<int, int> pii;
typedef long long llong;
typedef pair<llong, llong> pll;
#define mkp make_pair /*************** Program Begin **********************/
bool visited_vertex[5001];
bool visited_clique[5001]; class CliqueGraph {
public:
long long calcSum(int N, vector <int> V, vector <int> sizes) {
long long res = 0;
vector <int> S(sizes.size() + 1);
S[0] = 0;
for (int i = 0; i < sizes.size(); i++) {
S[i + 1] += S[i] + sizes[i];
}
vector <vector<int>> cliques(sizes.size()); // clique i 包括的点
vector <vector<int>> vcliques(N); // 包括点v的cliques
for (int i = 0; i < sizes.size(); i++) {
for (int j = S[i]; j < S[i + 1]; j++) {
cliques[i].push_back(V[j]);
vcliques[ V[j] ].push_back(i);
}
} for (int src = 0; src < N; src++) {
vector <int> D(N, 123456789);
D[src] = 0;
memset(visited_vertex, 0, sizeof(visited_vertex));
memset(visited_clique, 0, sizeof(visited_clique));
queue <int> Q;
Q.push(src);
visited_vertex[src] = true;
while (!Q.empty()) {
int v = Q.front();
Q.pop();
// 更新全部包括v的cliques中的全部点
for (int i = 0; i < vcliques[v].size(); i++) {
int c = vcliques[v][i]; // 包括v的cliques
if (visited_clique[c]) {
continue;
}
visited_clique[c] = true;
for (int j = 0; j < cliques[c].size(); j++) {
int u = cliques[c][j]; // clique c z中的的点
if (visited_vertex[u]) {
continue;
}
visited_vertex[u] = true;
D[u] = D[v] + 1;
Q.push(u);
}
}
}
for (int i = 0; i < N; i++) {
res += D[i];
}
}
return res / 2;
};
}; /************** Program End ************************/

版权声明:本文博主原创文章,博客,未经同意不得转载。

TCO14 2C L2: CliqueGraph,graph theory, clique的更多相关文章

  1. The Beginning of the Graph Theory

    The Beginning of the Graph Theory 是的,这不是一道题.最近数论刷的实在是太多了,我要开始我的图论与树的假期生活了. 祝愿我吧??!ShuraK...... poj18 ...

  2. HDU6029 Graph Theory 2017-05-07 19:04 40人阅读 评论(0) 收藏

    Graph Theory                                                                 Time Limit: 2000/1000 M ...

  3. Codeforces 1109D. Sasha and Interesting Fact from Graph Theory

    Codeforces 1109D. Sasha and Interesting Fact from Graph Theory 解题思路: 这题我根本不会做,是周指导带飞我. 首先对于当前已经有 \(m ...

  4. 2018 Multi-University Training Contest 4 Problem L. Graph Theory Homework 【YY】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6343 Problem L. Graph Theory Homework Time Limit: 2000 ...

  5. Graph Theory

    Description Little Q loves playing with different kinds of graphs very much. One day he thought abou ...

  6. 2017中国大学生程序设计竞赛 - 女生专场(Graph Theory)

    Graph Theory Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)To ...

  7. ACM学习历程—NPU1045 2015年陕西省程序设计竞赛网络预赛(热身赛)C题 Graph Theory(递推 && 组合数学 && 大数)

    Description In graph theory, a matching or independent edge set in a graph G = (V , E) is a set of e ...

  8. 图论介绍(Graph Theory)

    1 图论概述 1.1 发展历史 第一阶段: 1736:欧拉发表首篇关于图论的文章,研究了哥尼斯堡七桥问题,被称为图论之父 1750:提出了拓扑学的第一个定理,多面体欧拉公式:V-E+F=2 第二阶段( ...

  9. Introduction to graph theory 图论/脑网络基础

    Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...

随机推荐

  1. Akka FSM 源代码分析

    Akka FSM 源代码分析 萧猛 <simonxiao@qq.com> 啰嗦几句 有限状态机本身不是啥新奇东西,在GoF的设计模式一书中就有状态模式, 也给出了实现的建议.各种语言对状态 ...

  2. CF 452A(Eevee-直接试)

    A. Eevee time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  3. 微软最牛MS08-067漏洞各系统补丁下载地址

    本次MS08-067严重漏洞各系统补丁地址例如以下: 中文操作系统KB958644补丁下载地址: Windows Vista 安全更新程序 (KB958644) http://download.mic ...

  4. 【Linux探索之旅】第二部分第一课:终端Terminal,好戏上场

    内容简介 1.第二部分第一课:终端Terminal,好戏上场 2.第二部分第二课预告:命令行,世界尽在掌握 终端Terminal,好戏上场 随着第一部分的结束,我们进入了第二部分(小编你这好像是废话. ...

  5. Nginx禁止特定用户代理(User Agents)访问(转)

    Nginx可以通过各种方式来限制访问,例如NGINX基本Http认证.allow/deny等等,这些都是前文提过的,今天来看看nginx如果通过用户代理来禁止访问. user agent是什么? 用户 ...

  6. HPUX在oracle10g安装和卸载缩写

    创作品,出自 "深蓝的blog" 博客,欢迎转载,转载时请务必注明出处.否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlong/ ...

  7. Sphinx/Coreseek 4.1 跑 buildconf.sh 一个错误,无法生成configure档

    安装前 coorseek 什么时候,遇到一些错误.该官方网站无法看到的解决方案,终于 google 在大牛的博客评论区找到一个解决方案.突然跑到他的膝盖介绍~~ 这里整理是为了方便一些人发现,墙毕竟让 ...

  8. jqm的多列布局demo,html5的多列布局demo,多列布局的具体解说,html5开发实例具体解释

    因为移动设备屏幕宽度较小,所以一般不建议使用多列布局.但有时你可能须要并排放置一些元素(如button之类的). jQuery Mobile通过约定的类名ui-grid来提供了一种基于css的多列布局 ...

  9. POJ 1631 Bridging signals(LIS 二分法 高速方法)

    Language: Default Bridging signals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1076 ...

  10. How use Instruments and display the console in Command Lines applications

    I'm using Xcode on OSX to develop command line C applications. I would also like to use Instruments ...