Graph Theory, trainning-part-2, C. The largest clique

Source: Internet
Author: User
C. The largest cliqueTime Limit: 3000 msmemory limit: 131072kb64-bit integer Io format: % LLD Java class name: Main

Given a Directed GraphG, Consider the following transformation. First, create a new GraphT (G)To have the same vertex setG. Create a directed edge between two verticesUAndVInT (G)If and only if there is a pathUAndVInGThat follows the directed edges only in the forward direction. This graphT (G)Is often calledTransitive ClosureOfG.

We defineCliqueIn a directed graph as a set of verticesUSuch that for any two verticesUAndVInU, There is a directed edge either fromUToVOr fromVToU(Or both). The size of a clique is the number of vertices in the clique.

The number of cases is given on the first line of input. Each test case describes a graphG. It begins with a line of two integersNAndM, Where 0 ≤N≤ 1000 is the number of verticesGAnd 0 ≤M≤ 50,000 is the number of directed edgesG. The verticesGAre numbered from 1N. The followingMLines contain two distinct integersUAndVBetween 1 andNWhich define a directed edge fromUToVInG.

For each test case, output a single integer that is the size of the largest clique inT (G).

Sample Input
15 51 22 33 14 15 2
Output for sample input
4

Solving: Solving strongly connected subgraphs, reducing points, and dynamic planning on Dag. After finding all strongly connected subgraphs, scale down each strongly connected subgraph. The so-called point is to use this strongly connected block as a point to create a new graph. Any point in the original graph must belong to a strongly connected block. Therefore, the new graph is created based on the connected blocks of each point. Note the directionality. The dynamic planning on the Dag is for the directed graph. Therefore, the correctness of the direction must be ensured. After creating a new graph, find the longest path on the Dag.

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define INF 0x3f3f3f3f15 using namespace std;16 const int maxn = 1010;17 int low[maxn],dfn[maxn],iindex,sccBlocks;18 bool instack[maxn],vis[maxn];19 int belong[maxn],val[maxn],dp[maxn],n,m;20 stack<int>s;21 vector<int>g[maxn];22 vector<int>mp[maxn];23 void tarjan(int u){24     dfn[u] = low[u] = ++iindex;25     instack[u] = true;26     s.push(u);27     for(int i = 0; i < g[u].size(); i++){28         int v = g[u][i];29         if(!dfn[v]){30             tarjan(v);31             low[u] = min(low[u],low[v]);32         }else if(instack[v] && low[u] > dfn[v]) low[u] = dfn[v];33     }34     if(dfn[u] == low[u]){35         int v;36         sccBlocks++;37         do{38             v = s.top();39             s.pop();40             instack[v] = false;41             belong[v] = sccBlocks;42         }while(u != v);43     }44 }45 int dag(int u){46     if(dp[u]) return dp[u];47     else if(mp[u].size() == 0) return dp[u] = val[u];48     int ans = 0;49     for(int v = 0; v < mp[u].size(); v++){50         ans = max(ans,dag(mp[u][v]));51     }52     return dp[u] = ans+val[u];53 }54 int main(){55     int t,u,v,i,j;56     scanf("%d",&t);57     while(t--){58         scanf("%d%d",&n,&m);59         for(i = 0; i <= n; i++){60             g[i].clear();61             dfn[i] = low[i] = 0;62             instack[i] = false;63             val[i] = belong[i] = 0;64             dp[i] = 0;65             mp[i].clear();66         }67         for(i = 0; i < m; i++){68             scanf("%d%d",&u,&v);69             g[u].push_back(v);70         }71         iindex = sccBlocks = 0;72         for(i = 1; i <= n; i++)73             if(!dfn[i]) tarjan(i);74         for(u = 1; u <= n; u++){75             val[belong[u]]++;76             memset(vis,false,sizeof(vis));77             for(j = 0; j < g[u].size(); j++){78                 v = g[u][j];79                 if(!vis[belong[v]] && belong[v] != belong[u]){80                     vis[belong[v]] = true;81                     mp[belong[u]].push_back(belong[v]);82                 }83             }84         }85         int ans = 0;86         for(i = 1; i <= sccBlocks; i++)87             ans = max(ans,dag(i));88         printf("%d\n",ans);89     }90     return 0;91 }
View code

 

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.