zoukankan      html  css  js  c++  java
  • hdoj 4612 Warm up【双连通分量求桥&&缩点建新图求树的直径】

    Warm up

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
    Total Submission(s): 5093    Accepted Submission(s): 1131


    Problem Description
      N planets are connected by M bidirectional channels that allow instant transportation. It's always possible to travel between any two planets through these channels.
      If we can isolate some planets from others by breaking only one channel , the channel is called a bridge of the transportation system.
    People don't like to be isolated. So they ask what's the minimal number of bridges they can have if they decide to build a new channel.
      Note that there could be more than one channel between two planets.
     
    Input
      The input contains multiple cases.
      Each case starts with two positive integers N and M , indicating the number of planets and the number of channels.
      (2<=N<=200000, 1<=M<=1000000)
      Next M lines each contains two positive integers A and B, indicating a channel between planet A and B in the system. Planets are numbered by 1..N.
      A line with two integers '0' terminates the input.
     
    Output
      For each case, output the minimal number of bridges after building a new channel in a line.
     
    Sample Input
    4 4
    1 2
    1 3
    1 4
    2 3
    0 0
     
    Sample Output
    0
     
    题意:n个点m条边,问新建一条边,可以让桥的数量达到最少,输出最少的桥数
     
    题解:先求出所有的桥的数量,将桥连接的各个图进行缩点,根据树的直径的求法,求出最长的路径,用桥数量减去树的直径
    //scc代表 双联通分量  写习惯了 顺手就写成scc了也懒得改了 
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<stack>
    #include<queue>
    #include<vector>
    #define MAXM 2000100
    #define MAX 200100
    #define INF 0x7fffff
    using namespace std;
    int n,m,bridge,sum;
    int low[MAX],dfn[MAX];
    int head[MAX],ans,age;
    int sccno[MAX];//代表当前点属于哪个双连通分量 
    int dfsclock,scccnt;
    vector<int>newmap[MAX];//储存缩点后的新图 
    int instack[MAX];//标记当前点是否入栈 
    int dis[MAX];//求树的直径时记录路径的长度 
    int vis[MAX];//求树的直径时标记是否入队列 
    stack<int>s;
    struct node
    {
    	int beg,end,next;
    }edge[MAXM];
    void init()
    {
    	ans=0;
    	bridge=0;
    	memset(head,-1,sizeof(head));
    }
    void add(int u,int v)
    {
        edge[ans].beg=u;
        edge[ans].end=v;
        edge[ans].next=head[u];
    	head[u]=ans++;
    }
    void getmap()
    {
    	int a,b;
    	while(m--)
    	{
    		scanf("%d%d",&a,&b);
    		add(a,b);
    		add(b,a);
    	}
    }
    void tarjan(int u,int fa)
    {
    	int v,i;
    	low[u]=dfn[u]=++dfsclock;
    	instack[u]=1;
    	s.push(u);
    	int flag=1;
    	for(i=head[u];i!=-1;i=edge[i].next)
    	{
    		v=edge[i].end;
    		if(flag&&v==fa)//判重边 
    		{
    			flag=0;
    			continue;
    		}		    
    		if(!dfn[v])
    		{
    			tarjan(v,u);
    			low[u]=min(low[u],low[v]);
    			if(dfn[u]<low[v])
    				bridge++;
    		}
    		else if(instack[v])
    		    low[u]=min(low[u],dfn[v]);
    	}
        if(dfn[u]==low[u])
        {
        	scccnt++;
        	while(1)
        	{
        		v=s.top();
        		s.pop();
    			instack[v]=0;
    			sccno[v]=scccnt;
    			if(v==u)
    			break; 
        	}
        }
    }
    void find()
    {
        int i;
    	memset(low,0,sizeof(low));
    	memset(sccno,0,sizeof(sccno));
    	memset(dfn,0,sizeof(dfn));
    	memset(instack,0,sizeof(instack));
    	dfsclock=scccnt=0;
    	for(i=1;i<=n;i++)
    	{
    		if(!dfn[i])
    			tarjan(i,-1);
    	}	
    }
    void suodian()
    {
    	int u,v,i,j;
    	for(i=1;i<=scccnt;i++)
    		newmap[i].clear();
    	for(i=0;i<ans;i=i+2)
    	{
    		u=sccno[edge[i].beg];
    		v=sccno[edge[i].end];
    		if(u!=v)
    		{
    			newmap[u].push_back(v);
    			newmap[v].push_back(u);
    		}
    	}
    }
    void bfs(int beg)
    {
    	queue<int>q;
    	memset(dis,0,sizeof(dis));
    	memset(vis,0,sizeof(vis));
    	int i,j;
    	while(!q.empty())
    	    q.pop();
    	sum=0;
    	age=beg;
    	vis[beg]=1;
    	q.push(beg);
    	int u;
    	while(!q.empty())
    	{
    		u=q.front();
    		q.pop();
    		for(i=0;i<newmap[u].size();i++)
    		{
    			if(!vis[newmap[u][i]])
    			{
    				dis[newmap[u][i]]=dis[u]+1;
    				vis[newmap[u][i]]=1;
    				q.push(newmap[u][i]);
    				if(sum<dis[newmap[u][i]])
    				{
    					sum=dis[newmap[u][i]];
    					age=newmap[u][i];
    				}
    			}
    		}
    	}
    }
    void solve()
    {
    	int i,j;
    	bfs(1);
    	bfs(age);
    	printf("%d
    ",bridge-sum);
    	//printf("%d
    %d
    ",bridge,sum); 
    }
    int main()
    {
    	while(scanf("%d%d",&n,&m),n|m)
    	{
    		init();
    		getmap();
    		find();
    		//printf("%d#
    ",bridge);
    		suodian();
    		solve();
    	}
    	return 0;
    }
    

      

     
  • 相关阅读:
    解决Ant design vue的Layout布局,左右布局侧边栏无效不显示问题
    寻找写代码感觉(十二)之 封装分页请求参数和返回参数
    解决关于interceptor拦截器跨域AccessControlAllowOrigin报错问题
    解决Vue3引入自定义组件报错Cannot find module ‘xxx‘ or its corresponding type declarations问题
    解决antD中关于table组件中报这样的警告warning.js?2149:7 Warning: [antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key.]问题
    Centos 更改MySQL5.7数据库目录位置
    解决typescript:error TS2531: Object is possibly 'null'.问题
    解决npm WARN Local package.json exists, but node_modules missing, did you mean to install?问题
    弱监督学习文章略读记录
    弱监督学习最新文章略读
  • 原文地址:https://www.cnblogs.com/tonghao/p/4874499.html
Copyright © 2011-2022 走看看