Poj1144 network cut point

Source: Internet
Author: User

Problem address: http://poj.org/problem? Id = 1144

 

In fact, after reading this question, I know that the request is cut, but I haven't learned how to solve it yet.

 

So I found some information online.

 

I found the correct information and understood it slowly. But I made a mistake when I wrote the code. The result was a tragic wa, and then I had to go there for a long time.

 

I think that code is not complete.

 

To the end (very powerful ......) I found that I wrote a wrong word.

 

Then the AC is ready.

 

To celebrate, I learned how to cut points. By the way, you can cut the edge by the way.

 

So I tried using the two methods of the adjacent table and the adjacent matrix.

 

Because the data size is relatively small, it takes less time to submit the adjacent matrix. Unlike the list of adjacent tables, we need to apply for and release space all the time.

 

 

Paste the following code:

 

Due to continuous debugging, the Code is a bit sloppy =

 

Adjacent table:

 

# Include <iostream>
Using namespace STD;
Int pre [105], back [105];
Bool visited [105];
Int min (int A, int B)
{
If (A <B) return;
Else return B;
}
Struct Node
{
Int ver;
Node * next;
};
Node computer [105];
Void DFS (int I, int root, Int & Index, Int & COUNT)
{
Int n = 0;
Bool critical = false;
Node * t;
Visited [I] = true;
Pre [I] = index;
Back [I] = pre [I];
Index ++;
For (t = computer [I]. Next; t = T-> next)
{
If (! Visited [T-> ver])
{
DFS (t-> ver, root, index, count );
If (I = root)
{
N ++;
If (n = 2) Critical = true;
}
Else
{
Back [I] = min (back [I], back [T-> ver]);
If (back [T-> ver]> = pre [I])
{
Critical = true;
}
}
}
Else
{
Back [I] = min (back [I], pre [T-> ver]);
}
}
If (critical)
{
Count ++;
}
}
Int main ()
{
Node * temp, * t;
Int N, I, X, Y, CT, root, index;
While (scanf ("% d", & N )! = EOF)
{
If (n = 0) break;
For (I = 1; I <= N; I ++) computer [I]. Next = NULL;
For (I = 1; I <= N; I ++) visited [I] = false;
While (scanf ("% d", & X ))
{
If (x = 0) break;
While (getchar ()! = '/N ')
{
Scanf ("% d", & Y );
Temp = computer [X]. Next;
Computer [X]. Next = (node *) malloc (sizeof (node ));
Computer [X]. Next-> ver = y;
Computer [X]. Next-> next = temp;
Temp = computer [Y]. Next;
Computer [Y]. Next = (node *) malloc (sizeof (node ));
Computer [Y]. Next-> ver = X;
Computer [Y]. Next-> next = temp;
}
}
Index = 1;
Ct = 0;
Root = 1;
DFS (root, root, index, CT );
Printf ("% d/N", CT );
For (I = 1; I <= N; I ++)
{
Temp = computer [I]. Next;
While (temp)
{
T = temp-> next;
Free (temp );
Temp = T;
}
}
}
Return 0;
}

 

Adjacent matrix:

 

# Include <iostream>
Using namespace STD;
Bool map [2, 105] [2, 105];
Int pre [105], back [105];
Bool visited [105];
Int N;
Int min (int A, int B)
{
If (A <B) return;
Else return B;
}
Void DFS (int I, int root, Int & Index, Int & COUNT)
{
Int num = 0, J;
Bool critical = false;
Visited [I] = true;
Pre [I] = index;
Back [I] = index;
Index ++;
For (j = 1; j <= N; j ++)
{
If (Map [I] [J])
{
If (! Visited [J])
{
DFS (J, root, index, count );
If (I = root)
{
Num ++;
If (num = 2) Critical = true;
}
Else
{
Back [I] = min (back [I], back [J]);
If (back [J]> = pre [I])
{
Critical = true;
}
}
}
Else
{
Back [I] = min (back [I], pre [J]);
}
}
}
If (critical)
{
Count ++;
}
}
Int main ()
{
Int I, j, X, Y, CT, root, index;
While (scanf ("% d", & N )! = EOF)
{
If (n = 0) break;
For (I = 1; I <= N; I ++)
{
For (j = 1; j <= N; j ++)
{
Map [I] [J] = false;
}
}
For (I = 1; I <= N; I ++) visited [I] = false;
While (scanf ("% d", & X ))
{
If (x = 0) break;
While (getchar ()! = '/N ')
{
Scanf ("% d", & Y );
Map [x] [Y] = true;
Map [y] [x] = true;
}
}
Index = 1;
Ct = 0;
Root = 1;
DFS (root, root, index, CT );
Printf ("% d/N", CT );
}
Return 0;
}

 

Data inspection:

 

Data input:

 

21
17 4 8
1 11
7 5
13 1
3 1
14 5
15 20
9 12
6 8
16 14
18 8
8 4
20 18 10
2 3
12 5
5 9 20
19 20 9 11 2
11 3
4 15
10 3
21 3
0
0

 

Data output:

6

 

P.s. In fact, the graph algorithm is quite interesting ~ I read the Kruskal and prim algorithms in the morning, but I have already learned them before. Now I think it's almost the same, and I haven't really typed the code == ......

 

Study hard! Every day!

 

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.