HDU 4587 determines how many connected components are left after an isolated point + cut point + Delete point

Source: Internet
Author: User

I 've been doing this for a long time ......

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4587

First, enumerate the first vertex to be deleted. The second vertex is to find a cut vertex. If there is no cut vertex, there is also an answer.

Learned:

1. it is not practical to use hard-built Graph Theory templates. For example, if you want to delete isolated points, the number of connected branches will be reduced by one, but it is hard to handle them all the time, finally, we made a point reduction,

How to determine if it is an isolated point:

It is to first use an array SCNT [I] = J, VV [J] ++ to indicate that vertex I is in the UNICOM branch where J is the ancestor, and make VV [J] ++ every time, this means that VV [J] indicates the number of points in the connected branches that use J as the ancestor as VV [J]. This is not a template and is self-developed, I started to try several other methods, wa...

2. In my own cut point template, when subset [I] = 0, it indicates that the number of connected branches does not increase when the point is deleted, this includes hanging vertices and isolated vertices. If subset [v]> 0 is not used as the cut point, V is the cut point, however, when finding the number of connected branches after deleting the vertex, the two cases of hanging vertex and isolated vertex must be separated, if subset [I] = 0 & I is a suspension vertex, the number of connected branches remains unchanged. If subset [I] = 0 & I is an isolated point, the number of connected branches is reduced by one. Therefore, it is important to determine whether the number of connected branches is isolated points in 1.

3. At the beginning of this question, I had no idea, because I always thought about how to make the maximum number of connected branches when deleting two points together. I didn't try to think like this: "You want to delete a vertex and then delete it" (that is, if you cannot think about it, you can think about it step by step ), I didn't think so much. "I don't know how to make a decision, I just enumerated it." Because the time was 12 s, enough enumeration, and my code ran out in ms.



# Include <cstdio> # include <cstring> # include <algorithm> # include <iostream> using namespace STD; const int maxn = 5000*2 + 100; struct node {int, next, from; int U;} e [maxn]; int n, m; int head [maxn]; int vis [maxn], son, subset [maxn], dfn [maxn], low [maxn], tmpdfn, first, VV [maxn], SCNT [maxn]; void Init () {memset (Head,-1, sizeof (head); For (INT I = 0; I <n * 2 + 10; I ++) E [I]. from =-1;} void addedge (int u, int V, int K) {e [K]. to = V; E [K]. from = u; E [K]. next = head [u]; // E [K]. U = 0; head [u] = K;} int RT; void init2 () {tmpdfn = 0; memset (subset, 0, sizeof (subset); memset (VIS, 0, sizeof (VIS); memset (dfn, 0, sizeof (dfn); memset (low, 0, sizeof (low); memset (VV, 0, sizeof (vv); memset (SCNT,-1, sizeof (SCNT);} void DFS (int u) {dfn [u] = low [u] = ++ tmpdfn; For (Int J = head [u]; J! =-1; j = E [J]. Next) {If (E [J].! = First) // {int v = E [J]. To; If (! Vis [v]) {vis [v] = 1; SCNT [v] = RT, VV [RT] ++; DFS (v ); low [u] = min (low [u], low [v]); If (low [v]> = dfn [u]) {If (u = RT) son ++; else subset [u] ++ ;}} else {LOW [u] = min (low [u], dfn [v]) ;}} int solve () {int ans = 0, CNT = 0; For (int K = 0; k <n; k ++) {// delete point first = K; init2 (); CNT = 0; For (INT I = 0; I <n; I ++) {if (I! = First) {If (! Vis [I]) {son = 0; RT = I; CNT ++; vis [I] = 1; SCNT [RT] = RT, VV [RT] ++; DFS (I); If (son) subset [RT] = Son-1 ;}} int Pos =-1, mMax = 0; For (INT I = 0; I <n; I ++) if (I! = First) // ans = max (ANS, subset [I] + CNT ); // cnt-1 + subset [I] + 1 {If (mMax <subset [I] + CNT) {pos = I; mMax = subset [I] + CNT ;}} if (VV [SCNT [POS] = 1) mMax --; // It is not a cut point. After this point is removed, the number of connected branches does not increase ans = max (ANS, mMax);} return ans;} int main () {// freopen ("hdu4587.txt", "r", stdin); int u, v, k; while (~ Scanf ("% d", & N, & M) {Init (); For (INT I = 0, K = 0; I <m; I ++) {scanf ("% d", & U, & V); addedge (u, v, k ++); addedge (v, U, K ++ );} printf ("% d \ n", solve ();} 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.