Poj1236-network of schools

Source: Internet
Author: User

Want to see more problem-solving reports:
Http://blog.csdn.net/wangjian8006/article/details/7870410
Reprinted please indicate the source: http://blog.csdn.net/wangjian8006

There are n schools, from each school to another one-way network. There are two problems.
1: at least how many schools need to be assigned software in the initial stage so that all schools in the network can finally obtain the software.
2: You need to add at least several edges so that after any software is distributed to a school, after several transfers, all the schools in the network can finally obtain the software.
Solution:
First, find the connected component, and then check the total number of connected components at 0 points, and the total number of outgoing components at 0 points. Then, ask how many schools should issue software, it is the number of zero-entry points to ensure that all points can be found.
Then, the second question is how many edges can be added to reach a strongly connected component. The answer is the maximum number of inbound 0 and outbound 0.
So, why? After my discussion, I found out all the child trees in this figure, and then found the leaf nodes of a child tree (with an outbound degree of 0) connect to the root node of another subtree (the inbound degree is 0), so that after all the leaf nodes and root nodes are eliminated, the entire strongly connected component can be obtained, to check the minimum number of edges, you can check the number of leaf nodes and root nodes, that is, the number of outbound nodes and inbound nodes.
I can only say that this policy is correct and applies to General graphs. However, I don't think it can prove this conclusion very effectively.

 

/* Kosarajumemory 224 ktime 0 Ms */# include <iostream> using namespace STD; # define maxv 110 # define max (A, B) (A> B? A: B) int map [maxv] [maxv], order [maxv], belong [maxv], indegree [maxv], outdegree [maxv]; int N, num, count; bool vis [maxv]; void DFS (int x) {int I; vis [x] = 1; for (I = 1; I <= N; I ++) if (Map [x] [I] &! Vis [I]) DFS (I); Order [++ num] = x;} void dfst (int x) {int I; belong [x] = count; // indicates the connected component of the record node. vis [x] = 1; for (I = 1; I <= N; I ++) if (! Vis [I] & map [I] [x]) dfst (I);} void kosaraju () {int I; memset (VIS, 0, sizeof (VIS )); num = COUNT = 0; for (I = 1; I <= N; I ++) // sort the timestamp in ascending order for the first search if (! Vis [I]) DFS (I); memset (VIS, 0, sizeof (VIS); for (I = N; I> = 1; I --) // The second search starts with a large Timestamp and finds the connected component if (! Vis [order [I]) {count ++; // number of connected components dfst (Order [I]) ;}} void output () {int I, j, inzero = 0, outzero = 0; for (I = 1; I <= N; I ++) {indegree [I] = outdegree [I] = 0 ;} for (I = 1; I <= N; I ++) // find the connected component inbound and outbound degrees for (j = 1; j <= N; j ++) if (Map [I] [J] & belong [I]! = Belong [J]) {indegree [belong [J] ++; outdegree [belong [I] ++ ;}for (I = 1; I <= count; I ++) {// If (! Indegree [I]) inzero ++; If (! Outdegree [I]) outzero ++;} If (COUNT = 1) // printf ("1 \ N0 \ n") must be specified for only one node "); elseprintf ("% d \ n", inzero, max (inzero, outzero);} int main () {int I, A; while (~ Scanf ("% d", & N) {for (I = 1; I <= N; I ++) {While (scanf ("% d", &) & A) map [I] [a] = 1;} kosaraju (); output ();} return 0 ;}

 

========================================================== ========================================================== ======================

Introducing a good report http://www.byvoid.com/blog/scc-tarjan/ of the Tarjan Algorithm

 

/* Tarjanmemory 224 ktime 0 Ms */# include <iostream> using namespace STD; # define maxv 110 # define min (A, B) (A> B? B: A) # define max (A, B) (A> B? A: B) int N, map [maxv] [maxv], outdegree [maxv], indegree [maxv]; int dfn [maxv]; // The number of steps for the first access int low [maxv]; // The earliest number of steps in the subtree int STAP [maxv], stop; // simulate the stack bool instack [maxv]; // whether int count is in the stack; // records the number of connected components int CNT; // records the number of Search Steps int belong [maxv]; // specifies the connected component void Init () {COUNT = stop = CNT = 0; memset (instack, false, sizeof (instack); memset (MAP, 0, sizeof (MAP); memset (dfn, 0, sizeof (dfn);} void Tarjan (int x) {int I; dfn [x] = low [x] = ++ CNT; STAP [Stop ++] = X; I Nstack [x] = true; for (I = 1; I <= N; I ++) {If (! Map [x] [I]) continue; If (! Dfn [I]) {Tarjan (I); low [x] = min (low [I], low [x]);} else if (instack [I]) low [x] = min (dfn [I], low [x]); // connects to X, But I has been accessed, also in the stack // use the subtree node to update the time when the node first appears} If (low [x] = dfn [x]) {count ++; while (1) {int TMP = STAP [-- Stop]; belong [TMP] = count; instack [TMP] = false; If (TMP = x) Break ;}} void output () {int I, j, inzero = 0, outzero = 0; for (I = 1; I <= N; I ++) {indegree [I] = outdegree [I] = 0 ;}for (I = 1; I <= N; I ++) // find connected component inbound and outbound for (j = 1; j <= N; j ++) if (Map [I] [J] & belon G [I]! = Belong [J]) {indegree [belong [J] ++; outdegree [belong [I] ++ ;}for (I = 1; I <= count; I ++) {// If (! Indegree [I]) inzero ++; If (! Outdegree [I]) outzero ++;} If (COUNT = 1) // printf ("1 \ N0 \ n") must be specified for only one node "); elseprintf ("% d \ n", inzero, max (inzero, outzero);} int main () {int I, A; while (~ Scanf ("% d", & N) {Init (); for (I = 1; I <= N; I ++) {While (scanf ("% d", & A) map [I] [a] = 1 ;}for (I = 1; I <= N; I ++) if (! Dfn [I]) Tarjan (I); output ();} return 0 ;}

 

 

Related Article

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.