Road Construction
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 7980 Accepted: 4014

Description

It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads that lead between the various tourist attractions on the island.

The roads themselves are also rather interesting. Due to the strange customs of the island, the roads are arranged so that they never meet at intersections, but rather pass over or under each other using bridges and tunnels. In this way, each road runs between two specific tourist attractions, so that the tourists do not become irreparably lost.

Unfortunately, given the nature of the repairs and upgrades needed on each road, when the construction company works on a particular road, it is unusable in either direction. This could cause a problem if it becomes impossible to travel between two tourist attractions, even if the construction company works on only one road at any particular time.

So, the Road Department of Remote Island has decided to call upon your consulting services to help remedy this problem. It has been decided that new roads will have to be built between the various attractions in such a way that in the final configuration, if any one road is undergoing construction, it would still be possible to travel between any two tourist attractions using the remaining roads. Your task is to find the minimum number of new roads necessary.

Input

The first line of input will consist of positive integers n and r, separated by a space, where 3 ≤ n ≤ 1000 is the number of tourist attractions on the island, and 2 ≤ r ≤ 1000 is the number of roads. The tourist attractions are conveniently labelled from 1 to n. Each of the following r lines will consist of two integers, v and w, separated by a space, indicating that a road exists between the attractions labelled v and w. Note that you may travel in either direction down each road, and any pair of tourist attractions will have at most one road directly between them. Also, you are assured that in the current configuration, it is possible to travel between any two tourist attractions.

Output

One line, consisting of an integer, which gives the minimum number of roads that we need to add.

Sample Input

Sample Input 1 10 12 1 2 1 3 1 4 2 5 2 6 5 6 3 7 3 8 7 8 4 9 4 10 9 10  Sample Input 2 3 3 1 2 2 3 1 3

Sample Output

Output for Sample Input 1 
 Output for Sample Input 2
 0
题意:给出一个图求出至少添加多少边才能将其变为一个双联通图。
sl:缩点之后得到一个DAG,求出DAG图所有的叶子节点,可以通过low数组记录下每个节点所属的联通分量,然后
枚举每个节点的子节点low值是不是一样,不一样则其中一个节点的入度加1然后就是图论的小知识点了。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<vector>
 5 using namespace std;
 6 const int MAX =  ;
 7 vector<int> G[MAX];
 8 int pre[MAX],low[MAX],deg[MAX];
 9 int dfs_clock;
 void add_edge(int from,int to)
 {
     G[from].push_back(to);
     G[to].push_back(from);
 }
 
 int dfs(int u,int fa)
 {
     int lowu=pre[u]=++dfs_clock;
     for(int i=;i<G[u].size();i++)
     {
         int v=G[u][i];
         if(!pre[v])
         {
             int lowv=dfs(v,u);
             lowu=min(lowu,lowv);
         }
         else if(pre[u]>pre[v]&&v!=fa)
         {
             lowu=min(lowu,pre[v]);
         }
     }
     low[u]=lowu;
 //    printf("%d\n",lowu);
     return lowu;
 }
 void solve(int n)
 {
     memset(pre,,sizeof(pre));
     memset(low,,sizeof(low));
     memset(deg,,sizeof(deg)); int ans=;
     dfs(,-);
     for(int i=;i<=n;i++)
     {
         for(int j=;j<G[i].size();j++)
         {
            // printf("!!%d %d\n",low[i],low[G[i][j]]);
             if(low[i]!=low[G[i][j]])
             deg[low[i]]++;
         }
     }
     for(int i=;i<=n;i++)
     if(deg[i]==) ans++;
     ans=(ans+)>>;
     printf("%d\n",ans);
 }   
 int main()
 {
     int n,m; int a,b;
     while(scanf("%d %d",&n,&m)==)
     {
         for(int i=;i<=n;i++) G[i].clear();
         dfs_clock=;
         for(int i=;i<m;i++)
         {
             scanf("%d %d",&a,&b);
             add_edge(a,b);
         }
         solve(n);
     }
     return ;
 }
												

最新文章

  1. DeepMind背后的人工智能:深度学习原理初探
  2. transfromjs动画效果
  3. 用TypeScript开发爬虫程序
  4. Terrocotta - 基于JVM的Java应用集群解决方案
  5. 状压dp-poj-1170-Shopping Offers
  6. 关于masonry
  7. 照片提取GPS 转成百度地图坐标
  8. C primer Plus_part6
  9. JAVA库函数总结【持续更新】
  10. 在vue中使用Autoprefixed
  11. nginx介绍(五) - 高可用
  12. java使用jedis访问CentOS中的redis
  13. 【liferay】6、可配置式cookie
  14. 《Netty权威指南》(三)Netty 入门应用
  15. 架构模式逻辑层模式之:表模块(Table Model)
  16. mysql数据库使用mysqldump工具针对一个数据库备份,使用--databases选项与不使用该参数的区别
  17. Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) F. Substrings in a String
  18. 【干货】2个小时教你hexo博客添加评论、打赏、RSS等功能 (转)
  19. 洛谷P3112 [USACO14DEC]后卫马克Guard Mark
  20. c#通过URL地址从服务器上下载文件

热门文章

  1. 使用Gitalk实现静态页面评论的功能
  2. Spark之RDD的定义及五大特性
  3. P4451 [国家集训队]整数的lqp拆分
  4. base64编码上传图片java后台接收实例
  5. C#上机作业及代码Question1
  6. Oracle11gR2设置连接数process与会话session值
  7. visual studio 2015安装
  8. linux下jdk与tomcat的安装与配置
  9. MVC之参数验证(三)
  10. Android RxJava 2.0中backpressure(背压)概念的理解