UVA 11324 the largest clique (graph theory-tarjan, dynamic planning)

Source: Internet
Author: User

Problem B:the Largest clique

Given a directed graph G, consider the following transformation. First, create a new graph T (g) to the same vertex set as g. Create a directed edge between both vertices u and v in T (G) if and only if there is a path BETW Een u and v in G so follows the directed edges only in the forward direction. This graph T (g) is often called the transitive closure of G.

we define a  clique  in a directed graph as a set of vertices  u  such, or for any, vertices  u  and  v  in  u , there is a directed edge either from  u  to  v  or from  v & nbsp;to  u   (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 graph G. It begins with a line of between integers n and m, where 0≤ n ≤1000 is the number of vertices of g and 0≤ m ≤50,000 is the number of directed edges of g. The vertices of G is numbered from 1 to N. The following m lines contain, distinct integers u and v between 1 and n whic H define a directed edge from u to v in G.

For each test case, output a single integer so is the size of the largest clique in T (G).

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


Main topic:

T Group test Data. Give a picture of the graph G. Find the node set with the largest node number, so that at random two nodes in the node U and v meet: either u can reach v. Either v can reach U (U and v mutually reachable also can).


Problem Solving Ideas:

"The points in the same strongly connected component are either selected or not selected. The SCC diagram is obtained after shrinking the strong connected component. The right of each SCC node is equal to its node number, then the title is converted to the maximum path of the SCC graph. Because the SCC diagram is a DAG, it can be solved with dynamic programming.



Problem Solving Code:

#include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <algorithm    >using namespace Std;const int maxn=1100;const int maxm=51000;struct edge{int u,v,next;    Edge (int u0=0,int v0=0) {u=u0;v=v0; }}e[maxm];int N,m,head[maxn],dfn[maxn],low[maxn],mark[maxn],w[maxn],color[maxn],dp[maxn],cnt,nc,index;vector <int> vec;vector <vector<int> > Dfsmap;void addedge (int u,int v) {E[cnt]=edge (u,v); e[cnt].next=head[ u];head[u]=cnt++;}    void input () {cnt=nc=index=0;    scanf ("%d%d", &n,&m);    Vec.clear ();        for (int i=0;i<=n;i++) {w[i]=dfn[i]=0;        Mark[i]=false;    Color[i]=dp[i]=head[i]=-1;    } int u,v;        while (m-->0) {scanf ("%d%d", &u,&v);    Addedge (U,V);    }}void Tarjan (int s) {dfn[s]=low[s]=++index;    Mark[s]=true;    Vec.push_back (s);        for (int i=head[s];i!=-1;i=e[i].next) {int d=e[i].v;            if (!dfn[d]) {Tarjan (d); Low[s]=min (Low[d],low[s]);        }else if (Mark[d]) {low[s]=min (low[s],dfn[d]);        }} if (Dfn[s]==low[s]) {nc++;        int D;            do{D=vec.back ();            Vec.pop_back ();            COLOR[D]=NC;            Mark[d]=false;        w[nc]++;    }while (d!=s);    }}int DP (int s) {if (dp[s]!=-1) return dp[s];    int Ans=w[s];        for (int i=0;i<dfsmap[s].size (); i++) {int d=dfsmap[s][i];    if (DP (d) +w[s]>ans) ANS=DP (d) +w[s]; } return Dp[s]=ans;}    void Solve () {for (int i=1;i<=n;i++) {if (!dfn[i]) Tarjan (i);    } dfsmap.clear ();    Dfsmap.resize (nc+1);        for (int i=0;i<cnt;i++) {int X=COLOR[E[I].U],Y=COLOR[E[I].V];            if (x!=y) {dfsmap[x].push_back (y);        cout<<x<< "<<y<<endl;"    }} int ans=0;        for (int i=1;i<=nc;i++) {if (DP (i) >ans) ANS=DP (i);    cout<<i<< "" <<ans<<endl; } printf ("%d\n", ans);}    int main () {int t;    scanf ("%d", &t);        while (t-->0) {input ();    Solve (); } return 0;}


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

UVA 11324 the largest clique (graph theory-tarjan, dynamic planning)

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.