title Link:http://poj.org/problem?id=1236
Main topic:
Give you a network (a map), there are two tasks:
① the need for at least a few copies at the same time can make the entire network get a copy
② at least how many information tables are added (with a forward edge) so that the replicas are uploaded to any point, all of which can make the entire network get a copy
Problem Solving Ideas:
That is, given a direction graph, ask:
① must select at least a few vertices to be able to proceed from these vertices to reach all vertices
② at least how many edges must be added to make it possible to reach all vertices from any vertex
After the contraction point, we find out that the degree of 0 of the number of points is sum1,sum2,
The answer to the question ① is sum2;
The answer to the question ② is Max (SUM1,SUM2) (that is, all points have an out-of-degree entry greater than 0).
Note that there is only one point when you do not need to add an edge, the answer is 1, 0.
Code
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <stack>6#include <vector>7 using namespacestd;8 Const intn= the;9 Ten intN,cnt,num;//CNT is the current DFS sequence and num is the indent number. One intLow[n],dfn[n],fa[n],indeg[n],outdeg[n];//DFN is the DFS sequence, low is the first ancestor that the node can pass back (note that this is different from the low in the cut-edge cut) . Avector<int>v[N];//FA is the number of the strong Unicom component to which the node belongs. Indeg and outdeg for the indentation, the degree of the -stack<int>SK; - the voidinit () { -cnt=num=0; - for(intI=1; i<=n;i++){ - v[i].clear (); + } -memset (FA,0,sizeof(FA)); +memset (Low,0,sizeof(Low)); Amemset (DFN,0,sizeof(DFN)); atmemset (Indeg,0,sizeof(indeg)); -memset (Outdeg,0,sizeof(outdeg)); - } - - //seeking for strong Unicom components - voidTarjan (intu) { inlow[u]=dfn[u]=++CNT; - sk.push (u); to for(intI=0; I<v[u].size (); i++){ + intt=V[u][i]; - if(!dfn[t]) {//not been accessed the Tarjan (t); *low[u]=min (low[u],low[t]); $ }Panax Notoginseng Else if(!fa[t]) low[u]=min (low[u],dfn[t]);//has been accessed and is in the stack - } the if(low[u]==Dfn[u]) { +num++; A while(!Sk.empty ()) { the intt=sk.top (); + Sk.pop (); -fa[t]=num; $ if(T==u) Break; $ } - } - } the - intMain () {Wuyi while(~SCANF ("%d",&N)) { the init (); - for(intI=1; i<=n;i++){ Wu intx; - while(~SCANF ("%d", &x) &&x) v[i].push_back (x); About } $ for(intI=1; i<=n;i++) {//Traverse All points - if(!Dfn[i]) Tarjan (i); - } - for(intI=1; i<=n;i++) {//and find out whether the degree of the corresponding degree is 0 (note is not the degree of access) A for(intj=0; J<v[i].size (); j + +){ + intt=V[i][j]; the if(fa[t]!=Fa[i]) { -outdeg[fa[i]]=1; $indeg[fa[t]]=1; the } the } the } the intsum1=0, sum2=0; - for(intI=1; i<=num;i++){ in if(outdeg[i]==0) thesum1++; the if(indeg[i]==0) Aboutsum2++; the } the if(num==1)//There's only one point when it's a special sentence. thePuts"1\n0"); + Elseprintf"%d\n%d\n", Sum2,max (sum1,sum2)); - } the return 0;Bayi}
POJ 1236 Network of schools (Tarjan to find strong connected components + thinking)