Uva11324 the largest clique --- strongly connected + dp

Source: Internet
Author: User

For a directed graph G, finding a subgraph requires that any two of them have at least one side to be reachable,

The maximum number of vertices contained in this subgraph.


First, find the SCC contraction point to create a graph. The weight of each point is the number of points contained in the vertex.

Any two points must be reachable. In fact, all edges must be in the same direction. Otherwise, there must be two points inaccessible,

In this way, the question is converted into the longest path of the Dag graph.

Then the memory-based search starts from the point where the input degree is 0. DP [I] indicates the maximum number of points containing the root I.


# Include <iostream> # include <cstring> # include <string> # include <cstdio> # include <cmath> # include <algorithm> # include <vector> # include <queue> # include <map> # define INF 0x3f3f3f # define EPS 1e-6 # define ll _ int64 # define M 1010 using namespace STD; int sta [m], top; // stack bool vis [m] In the Tarjan algorithm; // check whether int dfn [m] is in the stack; // deep-first search for the access order int low [m]; // The earliest order int ccnt that can be traced back; // The number of strongly connected components int ID of the directed graph; // Index Number Vector <int> E [m]; // adjacent Table: vector <int> part [m]; // obtain the strongly connected component result int INPART [m]; // record the int degree [m] In the strongly connected component of the vertex number; // record the degree vector of each strongly connected component <int> edge [m]; // create an int ans, n, m, DP [m], in [m], point [m]; void Tarjan (int x) {int I, J; dfn [x] = low [x] = ID ++; vis [x] = 1; STA [++ top] = x; for (I = 0; I <E [X]. size (); I ++) {J = E [x] [I]; If (dfn [J] =-1) {Tarjan (j ); low [x] = min (low [X], low [J]);} else if (vis [J]) low [x] = min (low [X], dfn [J]);} If (dfn [x] = low [x]) {do {J = ST A [top --]; vis [J] = 0; part [ccnt]. push_back (j); INPART [J] = ccnt; point [ccnt] ++;} while (J! = X); ccnt ++ ;}} void solve (int n) {memset (STA,-1, sizeof Sta); memset (VIS, 0, sizeof vis ); memset (dfn,-1, sizeof dfn); memset (low,-1, sizeof low); memset (point, 0, sizeof point); Top = ccnt = id = 0; for (INT I = 1; I <= N; I ++) if (dfn [I] =-1) Tarjan (I) ;} int DFS (INT X) {If (DP [x]) return DP [X]; DP [x] = point [X]; int I; for (I = 0; I <edge [X]. size (); I ++) {int TMP = edge [x] [I]; DP [x] = max (DP [X], point [x] + DFS (TMP);} return DP [X];} Int main () {int n, m, I, j, a, B, T; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M); for (I = 0; I <= N; I ++) {part [I]. clear (); e [I]. clear (); edge [I]. clear () ;}while (M --) {scanf ("% d", & A, & B); e [A]. push_back (B);} solve (n);/* printf ("ccnt: % d \ n", ccnt); for (I = 0; I <ccnt; I ++) {for (j = 0; j <part [I]. size (); j ++) printf ("% d", part [I] [J]); puts ("") ;}*/memset (in, 0, sizeof in); for (I = 1; I <= N; I ++) // enumerate the edges in the source image {for (j = 0; j <E [I]. size (); j ++) {int A = INPART [I]; int B = INPART [E [I] [J]; // if (! = B) {in [B] ++; edge [A]. push_back (B) ;}}}ans = 0; memset (DP, 0, sizeof DP); for (I = 0; I <ccnt; I ++) // The scaled-down graph is from 0 to the {If (! In [I]) ans = max (ANS, DFS (I);} printf ("% d \ n", ANS);} return 0 ;}


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.