求一个图里任意两个点连接而成的最长的边

Problem K. Road Network
Program: batman.(cpp|java)
Input: batman.in
Balloon Color: Dark Blue
After a ?erce battle with his opponent, Bruce Wayne ?nally won the elections and became the mayor
of Gotham. Like every other politician, he had an agenda with lots of projects for the sake of Gotham’s
prosperity, but he was met with the same problem, lack of fund.
He decided to tackle the problem from a di?erent perspective; he will allow companies to buy
roads in the city (roads in the city are undirected). The city will get the money needed for the projects
and the companies can use the roads for advertisements (or so he thought).
After the deal was done, the companies were more cunning than he expected. They started to
threaten that they will block exactly one road in the city and prevent people from getting to their work,
in the hope that people will revolt against Mayor Wayne. The problem was that the city is designed as a
tree of connected zones, where there is only one unique path between any two zones. Hence, blocking a
road means that some zones are not reachable from others anymore.
Mayor Wayne discussed the problem with his council and identi?ed what they called vulnerable
roads. A road is vulnerable if blocking it can disconnect two zones from each other. Mayor Wayne wants
to prevent this from happening by building more roads but his budget could a?ord building only one
extra road. Can you help him ?gure out which road he should build, such that he minimizes the number
of vulnerable roads?
Input
Your program will be tested on one or more test cases. The ?rst line of the input will be a single integer
T, the number of test cases (1 ≤ T ≤ 100) followed by T test cases. The ?rst line of each test case will
contain one integer N, the number of zones in the city (1 ≤ N ≤ 10,000). The following N - 1 lines will
each contain a pair of integers x and y separated by a single space (1 ≤ x,y ≤ N) which means that zone
x is connected to zone y. It’s guaranteed that the edges will form a tree.
Output
For each test case, print a single line containing an integer, the minimum number of vulnerable roads in
the city after building the new road.
Examples
batman.in
2
3
1 2
1 3
4
1 2
2 3
2 4
standard output
0
1
题意:给出n个点 n-1条边 求再增加一条边使图中脆弱边的数量最少(脆弱边:若该边去掉后则该边相连的两点彻底失去联系)
分析:共有 n-1条边,一旦增加了一条边,则必会形成一个环,在该环上,去掉任何两点之间的边,这两点都不会失去联系,而除了该环上的边以外的其他边一旦去掉,该边相连的两点必会失去联系,所以脆弱边的数量就是n-x(x是新形成的环上的边的数量)为使脆弱边的数量最少,就要使x尽量大,x=新加的一条边+环上原来就存在的边,也就是说我要使这个环上尽可能包含更多的结点,也就是求任意两个结点之间形成的最长的边。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<string.h>
using namespace std;
#define INF 0x3f3f3f3f
#define MAXX 10005
int dis[MAXX],v[MAXX],head[MAXX],ans;
struct node
{
    int from,to,w,next;
}edge[2*MAXX];
int n,m,cnt=0;
int mmm=0;
int beg=0;
void init()
{
    for(int i=0;i<=n;i++)
        dis[i]=0;
        cnt=0;
        memset(head,-1,sizeof(head));
}
void add(int a,int b,int c)
{
    edge[cnt].from=a;
    edge[cnt].to=b;
    edge[cnt].w=c;
    edge[cnt].next=head[a];
    head[a]=cnt++;
}
void dfs(int x,int y)
{
    if(mmm<dis[x])
    {
        mmm=dis[x];
        beg=x;
    }
    for(int i = head[x];i != -1;i = edge[i].next)
    {
        int v =edge[i].to;
        if(v==y) continue;
        dis[v] = dis[x]+ edge[i].w;
        dfs(v,x);
    }
}
int main()
{
    int t,n,a,b;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        mmm=0,beg=0;
        init();
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&a,&b);
            add(a,b,1);
            add(b,a,1);
        }
        dfs(1,0);
        dis[beg]=0;
        mmm=0;
        dfs(beg,0);
        printf("%d\n",n-1-mmm);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值