Redundant Paths separate paths (double side connected component)

Casual working:
  In order to go to another from F (1≤F≤5000) a pasture in a Bessie and her companions sometimes have to pass some of their nasty horrible tree. Cows are tired of forced to go to a road, so they want to build a new road, so that each has at least two mutually separated paths are between pastures, so they have more choices. It has at least one path between every pair of pastures . All given R (F - 1 ≤ R ≤ 10000) described bidirectional paths, each path connecting two different pasture, calculate the minimum number of new roads, the path from end to end connected by a plurality of roads. Two paths separated from each other, refers to two paths do not overlap one way. However, there may be some of the same pastures on two separate paths. For the same between pastures, may have two different roads, you can also build a road between them, as another different path, please output the minimum number of required new roads.

answer:

  Modified road? Plus or minus side? It can be relatively easily conceivable side dual communication component .

  This problem requires that each node can be reached by at least two paths, the node satisfying the condition that the degree of> = 2 node (degrees undirected graph). Node 1 is of degree does not meet the meaning of the questions.

  Built between two sides of three ways:
  1, two degree> = 2 nodes connected: useless, give it.

  2, a degree> = 2, other = 1 is connected to two nodes: node satisfies a title intended to make.

  3, the node connected to two degrees = 1: the two nodes are intended to meet the problem.

  Very obviously, is connected to two degrees = 1 nodes must be optimal . This also shows that this question is to find the core node degree = 1.

100% wrong solution (bis point connected component):

  Like conventional double-point communication component, the first condensing point, to find what degree.

  But this AC code and method for finding connected components does not comply with a double point of (in the implementation process, necessary to determine the parent node tarjan off; the construction side, the need to re-off edge determination).

  This question can only say that the data did not consider this play, although AC, must be wrong solution.

Code:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define $ 5111
 5 using namespace std;
 6 int m,n,k,t,dfn[$],low[$],first[$],tot1,tar,sta[$],up,circle,tr=-1;
 7 int sum,cir[$],out[$];
 8 bool judge[$],vis[$][$];
 9 struct tree{    int to,next;    }a[$<<5],aa[$<<5];
10 inline int min(int x,int y){    return x<y?x:y;    }
11 inline void add(int x,int y){
12     a[++tot1]=(tree){    y,first[x]    };
13     first[x]=tot1;
14     a[++tot1]=(tree){    x,first[y]    };
15     first[y]=tot1;
16 }
17 inline void tarjan(int x,int dad,int tmp=0){
18     dfn[x]=low[x]=++tar;
19     sta[++up]=x;
20     for(register int i=first[x];i;i=a[i].next){
21         int to=a[i].to;
22         if(to==dad) continue;
23         if(!dfn[to]){
24             tarjan(to,x);
25             low[x]=min(low[x],low[to]);
26         }
27         else low[x]=min(low[x],dfn[to]);
28     }
29     if(dfn[x]==low[x]){
30         ++circle;
31         do{
32             tmp=sta[up--];
33             cir[tmp]=circle;
34         }while(tmp!=x);
35     }
36 }
37 signed main(){
38     scanf("%d%d",&n,&m);
39     for(register int i=1,x,y;i<=m;++i){
40         scanf("%d%d",&x,&y);
41         if(vis[x][y]==0) add(x,y),vis[x][y]=vis[y][x]=1;
42     }
43     tarjan(1,0);
44     for(register int i=1;i<=n;++i)
45         for(register int j=first[i];j;j=a[j].next){
46             int to=a[j].to;
47             if(cir[i]!=cir[to]) out[cir[i]]++;
48         }
49     for(register int i=1;i<=circle;++i) if(out[i]<2) sum++;
50     circle==1?puts("0"):printf("%d\n",(sum+1)/2);
51 }
View Code

100% Positive Solutions (double side connected component):

  Solutions of course double positive side connected component.

 1 inline void tarjan(int x,int opt){
 2     dfn[x]=low[x]=++tar;
 3     for(register int i=first[x];i;i=a[i].next){
 4         int to=a[i].to;
 5         if(!dfn[to]){
 6             tarjan(to,i);
 7             low[x]=min(low[x],low[to]);
 8             if(low[to]>dfn[x]) br[i]=br[i^1]=1; 
 9         }
10         else if(i!=(opt^1)) low[x]=min(low[x],dfn[to]);
11     }
12 }
13 inline void dfs(int x){
14     in[x]=dcc;
15     for(register int i=first[x];i;i=a[i].next){
16         int to=a[i].to;
17         if(in[to]||br[i]) continue;
18         dfs(to);
19     }
20 }

  同样是先缩点,求一下度数即可。

Code:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define $ 5111
 5 using namespace std;
 6 int m,n,sum,dfn[$],low[$],first[$],tot,tar,br[$],up,circle,dcc,in[$],out[$];
 7 struct tree{    int to,next;    }a[$<<5],aa[$<<5];
 8 inline int min(int x,int y){    return x<y?x:y;    }
 9 inline void add(int x,int y){
10     a[++tot]=(tree){    y,first[x]    };
11     first[x]=tot;
12     a[++tot]=(tree){    x,first[y]    };
13     first[y]=tot;
14 }
15 inline void tarjan(int x,int opt){
16     dfn[x]=low[x]=++tar;
17     for(register int i=first[x];i;i=a[i].next){
18         int to=a[i].to;
19         if(!dfn[to]){
20             tarjan(to,i);
21             low[x]=min(low[x],low[to]);
22             if(low[to]>dfn[x]) br[i]=br[i^1]=1; 
23         }
24         else if(i!=(opt^1)) low[x]=min(low[x],dfn[to]);
25     }
26 }
27 inline void dfs(int x){
28     in[x]=dcc;
29     for(register int i=first[x];i;i=a[i].next){
30         int to=a[i].to;
31         if(in[to]||br[i]) continue;
32         dfs(to);
33     }
34 }
35 signed main(){
36     scanf("%d%d",&n,&m); tot++;
37     for(register int i=1,x,y;i<=m;++i) scanf("%d%d",&x,&y),add(x,y);
38     tarjan(1,0);
39     for(register int i=1;i<=n;++i)  if(!in[i]) dcc++,dfs(i);
40     for(register int i=2,x,y;i<=tot;++i){
41         x=a[i^1].to,y=a[i].to;
42         if(in[x]!=in[y]) out[in[y]]++;
43     }
44     for(register int i=1;i<=dcc;++i) if(out[i]==1) sum++;
45     dcc==1?puts("0"):printf("%d\n",(sum+1)/2);
46 }
View Code

 

Guess you like

Origin www.cnblogs.com/OI-zzyy/p/11183705.html