TCO14 2C l2:cliquegraph,graph theory, clique

Source: Internet
Author: User

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

References: Http://apps.topcoder.com/wiki/display/tc/TCO+2014+Round+2C

Assume that each edge is calculated first, the adjacency matrix is used to represent the graph, and then the BFS or Floyd-warshall algorithm is used to calculate the distance. The time complexity is O (n^3), which times out. According to the title of the tip know to use the nature of clique graph to do. The basic idea is to treat a clique as a whole in BFS. Once you visit a point in the clique, the distance from all points in the clique is available. Algorithmic descriptive narratives such as the following

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 on C to discover new vertices


Code:

#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) {L Ong 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 include the point vector <vector<int>> vcliques (N); Cliquesfor including point V (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 (visit Ed_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 ();//update all the points in the cliques that include V for (int i = 0; i < vcliques[v].size (); i++) {int c = vcliques[v][i];//cliquesif including V (v Isited_clique[c]) {continue;} VISITED_CLIQUE[C] = true;for (int j = 0; J < Cliques[c].size (); j + +) {int u = cliques[c][j];//clique C Z for Point if (visite D_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 ************************/


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

TCO14 2C l2:cliquegraph,graph theory, clique

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.