TCO14 2C L2: CliqueGraph,graph theory, clique

称号: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 ************************/

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

时间: 2024-08-02 06:52:49

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

2017 UESTC Training for Graph Theory

2017 UESTC Training for Graph Theory A       思维 题意:给你一个有n个点和m条边的无向连通图,每条边都有一个权值w.我们定义,对于一条路径,它的Charm value为该路径上所有边的权值的最大值与最小值的差.询问从1到n的所有路径的Charm value的最小值. tags:有点思维定式了..一条路径里只要最大最小值,所以边可以重复走.这样我们只要把边从小到大枚举,把第 i 条边作为最小边,然后对于每个 i ,我们按顺序逐一加入比它大的边,直到点

Graph Theory

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 140    Accepted Submission(s): 74 Problem Description Little Q loves playing with different kinds of graphs very much. One day he thought about

Graph Theory Algorithms

1 /** 2 * 图论 3 **/ 4 5 /***************************** 图的抽象数据类型 ************************************/ 6 ADT Graph 7 { 8 数据: 9 Graph = (Vertex, Edge)是可以用不同方式存储的图,Vertex是顶点集, 10 Edge = {<vertex_1, vertex_2> | vertex_1, vertex_2属于Vertex,vertex_1不等于verte

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

Description In graph theory, a matching or independent edge set in a graph G = (V , E) is a set of edges ME such that no two edges in the matching M share a common vertex. The Nobel Prize in XiXiHaHa was awarded to Jerryxf team, amongst other things,

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/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1536    Accepted Submission(s): 830 Problem Description Ther

Graph Theory: Representation and Algorithms

Summer Project - 2019Graph Theory: Representation and Algorithms1 IntroductionIn order to complete this project successfully, the student should be familiar with many basicnotions of graph theory. The questions below whose answers should be included

图论介绍(Graph Theory)

1 图论概述 1.1 发展历史 第一阶段: 1736:欧拉发表首篇关于图论的文章,研究了哥尼斯堡七桥问题,被称为图论之父 1750:提出了拓扑学的第一个定理,多面体欧拉公式:V-E+F=2 第二阶段(19~20世纪): 1852: Francis Guthrie提出四色问题 1856: Thomas P. Kirkman & William R.Hamilton研究了哈密尔顿图 1878: Alfred Kempe给出给出四色定理证明 1890: 希伍德(Heawood)推翻原有四色定理证明 1

L1 和L2正则化,拉普拉斯分布和高斯分布

正则化是为了防止过拟合. 1. 范数 范数是衡量某个向量空间(或矩阵)中的每个向量以长度或大小. 范数的一般化定义:对实数p>=1, 范数定义如下: L1范数: 当p=1时,是L1范数,其表示某个向量中所有元素绝对值的和. L2范数: 当p=2时,是L2范数, 表示某个向量中所有元素平方和再开根, 也就是欧几里得距离公式. 2. 拉普拉斯分布 如果随机变量的概率密度函数分布为: 那么它就是拉普拉斯分布.其中,μ 是数学期望,b > 0 是振幅.如果 μ = 0,那么,正半部分恰好是尺度为 1/

l2-loss,l2范数,l2正则化,欧式距离

欧式距离: l2范数: l2正则化: l2-loss(也叫平方损失函数): http://openaccess.thecvf.com/content_cvpr_2017/papers/Li_Mimicking_Very_Efficient_CVPR_2017_paper.pdf 总结:l2范数和欧式距离很像,都是开根号.l2正则化和l2-loss都是直接开平方.上面这篇mimic的paper,就是用的l2-loss,可以看到他写的公式就是在l2范数上开平方.也可以这么理解,对于loss,需要求梯