Poj 1236 strong connectivity

Source: Internet
Author: User

I have been calling this question for a long time, and finally I have overcome it.

Graph Theory, templates must be highly reliable

Abstract question:

Two tasks to be completed

1: at least a few vertices can be traversed to traverse all vertices, that is, the number of vertices with zero degree of shrinkage.

If you do not scale down a vertex, for example, there are two rings with no vertex entry degree of 0, but you still need to traverse the vertices in the two rings respectively.

2. If at least several sides need to be connected, the graph will become strongly connected, that is, to connect the points with an inbound degree of 0 and an outbound degree of 0, it is equivalent to connecting the leaves of a tree to the root, the minimum number of sides to be connected is greater than the number of connected blocks with an outbound degree of 0 (it should be easy to think about)

In addition, you need to note that the source image may be strongly connected. After the point is reduced, there will be only one point, and the inbound outbound degree is 0. Therefore, we need to determine

# Include <stdio. h> # include <string. h >#include <vector >#include <algorithm> using namespace STD; const int max = 10010; vector <int> edge [Max]; int st [Max]; int dfn [Max], low [Max]; int top, btype, tdfn; int belong [Max]; bool INS [Max]; void DFS (INT s) {int I, t; dfn [s] = low [s] = ++ tdfn; INS [s] = true; ST [++ top] = s; for (I = 0; I <edge [s]. size (); I ++) {T = edge [s] [I]; If (! Dfn [T]) {DFS (t); If (low [T] <low [s]) low [s] = low [T];} else if (INS [T] & dfn [T] <low [s]) low [s] = dfn [T];} if (dfn [s] = low [s]) {btype ++; do {T = sT [top --]; INS [T] = false; belong [T] = btype;} while (T! = S) ;}} void SCC (int n) {int I; Top = btype = tdfn = 0; memset (INS, false, sizeof (INS); memset (dfn, 0, sizeof (dfn); for (I = 1; I <= N; I ++) if (! Dfn [I]) DFS (I);} int chu [Max]; int in [Max]; int main () {int I, j, A, N; int a_ans, B _ans; while (scanf ("% d", & N )! = EOF) {for (I = 0; I <= N; I ++) edge [I]. clear (); for (I = 1; I <= N; I ++) {While (scanf ("% d", & A),) {edge [I]. push_back (a) ;}}if (n = 1) {printf ("1 \ N0 \ n"); continue;} SCC (N ); if (btype = 1) {printf ("1 \ N0 \ n"); continue;} memset (CHU, 0, sizeof (CHU); memset (in, 0, sizeof (in); for (I = 1; I <= N; I ++) {int TMP = belong [I]; for (j = 0; j <edge [I]. size (); j ++) {int tmp1 = belong [edge [I] [J]; If (belong [I]! = Belong [edge [I] [J]) {chu [TMP] ++; in [tmp1] ++ ;}}a_ans = B _ans = 0; for (I = 1; I <= btype; I ++) {If (chu [I] = 0) B _ans ++; If (in [I] = 0) a_ans ++;} B _ans = a_ans> B _ans? A_ans: B _ans; printf ("% d \ n", a_ans, B _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.