Key cities--the plot of the cut point "AHA algorithm" code detailed

Source: Internet
Author: User

#include <iostream>using namespace Std;int n,m,e[9][9],root;int num[9],low[9],flag[9],index;void dfs (int cur,    int father) {int child=0;    index++;    Num[cur]=index;    Low[cur]=index;                          for (int i=1;i<=n;i++) {if (e[cur][i]==1) {//i is not a node that can be reached by the father if (num[i]==0) {///If it is Dad, it will not be 0 If not father, must be own ancestors//that is, you walked a ring//Because this is a deep search, of course, this ancestor is not necessarily the highest level of that, but, the total                than the father first access to the child++;                                              DFS (i,cur);//start looking for ancestors by their own son Low[cur]=min (Low[cur],low[i]);//Since cur can not go through dad to I, then, I can not through their father to reach the node, cur as can to                                              In this case, although cur than I first access, but it is very likely to find through I have visited a point already.                If you can find it, cur is not cutting it.                                                if (Cur!=root&&low[i]>=num[cur]) {///This is not found, because the initial value of low must be larger than NUM, so do not go through dad can not find the ancestors,                    The value of Low[i] is no smaller than num[cur].                Flag[cur]=1; } if (CUr==root&&child==2) {//child equals 2, it means the root node must be cut//because, only if his son has not been visited, will be his own son Simply put, at the beginning of the program, make sure that a node adjacent to it is the son, will immediately start deep search//Search                                        After the cable, other nodes adjacent to it, if accessed, will not enter the IF, so there will not be a new son//So the son number only two, the root node must be a cut point.                    The number of sons is three is also, but become three before, already became two when the time was confirmed, so write equals 2 no problem.                Flag[cur]=1;            }} else if (i!=father) {//not father, but ancestor Low[cur]=min (Low[cur],num[i]),//This sentence is the core AH!!! }}} return;}    int main () {int x, y;    cin>>n>>m;        for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) {e[i][j]=0;        }} for (int i=1;i<=m;i++) {cin>>x>>y;        E[x][y]=1;    E[y][x]=1;    } root=1;    DFS (1,root);    for (int i=1;i<n;i++) {if (flag[i]==1) {cout<<i<< "";    }}//cout<<low[6]<<endl;} 

  

Key cities--the plot of the cut point "AHA algorithm" code detailed

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.