Keeping On Track

题目描述

Acmar and Ibmar are at war! You are in charge of a rail network that transports important supplies throughout the great state of Acmar during this delicate time. The rail system is made up of a set of rail lines which meet at various junction points. While there is no limit to the number of rail lines that can meet at a junction, the network is set up so that there is only one path between any two junctions. You’ve tried to argue for some redundancy in the system, i.e., extra rail lines so that there are two or more paths connecting some junctions, but it’s wartime and budgets are tight.

However, this may soon change as you’ve just been given some terrible news from double agents working in Ibmar: within the next month enemy spies plan to blow up one of the junctions! Unfortunately, the exact junction is not known, but knowing your enemy well you are certain that they will undoubtedly strike the critical junction, specifically the junction whose removal disconnects the most pairs of other remaining junctions in the system. You don’t have much time to act, so the most you can do is add one new line connecting two currently unconnected junctions, thereby reducing the number of disconnected pairs after the critical junction has been destroyed. Your job is to determine how to make the number of disconnected pairs as small as possible by adding in the best possible rail line.

 

输入

Input starts with a line containing an integer n (2 ≤ n ≤ 10 000) indicating the number of rail lines in the system. Following that are n lines of the form i1 i2 indicating that a rail line connects junctions i1 and i2. Junctions are numbered consecutively starting at 0. All rail lines are two-way and no rail line appears more than once in the input. There is exactly one path between any two junction points given in the input.

 

输出

Display two values n1 and n2, where n1 is the number of pairs of junctions which will be disconnected when the enemy destroys the critical junction, and n2 is the number of pairs of junctions still disconnected after you add in the best possible rail line. There will never be more than one critical junction.

 

样例输入

6
0 1
1 2
2 3
2 4
4 5
4 6

 

样例输出

11 5
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=10050;
int n,critical=-1,maxpair=-1;
int head[maxn],tot;
bool  vis[maxn];
struct node
{
    int u,v,nextt;
}edge[10050*2];
void addedge(int u,int v)
{
    edge[++tot].u=u;
    edge[tot].v=v;
    edge[tot].nextt=head[u];
    head[u]=tot;
}
int dfs(int u)
{
    int v,ans=1;
    vis[u]=1;
    for(int i=head[u];i;i=edge[i].nextt)
    {
        v=edge[i].v;
        if(!vis[v])
        {
            ans+=dfs(v);
        }
    }
    return ans;
}
int dfs2(int u)
{
    int v,ans=0,son=0;
    vis[u]=1;
    for(int i=head[u];i;i=edge[i].nextt)
    {
        v=edge[i].v;
        if(!vis[v])
        {
            int tmp=dfs2(v);
            ans+=tmp*(n-tmp);
            son+=tmp;
        }
    }
    ans+=(n-son)*son;
    ans/=2;
    if(ans>maxpair)
    {
        maxpair=ans;
        critical=u;
    }
    return son+1;
}
int main()
{
    int u,v;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d %d",&u,&v);
        addedge(u,v);
        addedge(v,u);
    }
    dfs2(0);
    memset(vis,0,sizeof(vis));
    vis[critical]=1;
    int fir=0,sec=0;
    for(int i=head[critical];i;i=edge[i].nextt)
    {
        v=edge[i].v;
        if(!vis[v])
        {
            int tmp=dfs(v);
            if(tmp>fir)
            {
                sec=fir;
                fir=tmp;
            }
            else if(tmp>sec)
            {
                sec=tmp;
            }
        }
    }
    printf("%d %d\n",maxpair,maxpair-fir*sec);
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Before Playstation, there was Pong, at one time the ultimate in video game entertainment. For those of you not familiar with this game please refer to the Wikipedia entry (http://en.wikipedia.org/wiki/Pong) and the many fine websites extolling the game and its virtues. Pong is not so very different in structure from the Billiard ball simulation that you developed earlier in the course. They both involve a ball moving and colliding with obstacles. The difference in this case is that two of the obstacles are under user control. The goal of this project is to develop your own version of Pong in MATLAB using the keyboard as input, for example, one player could move the left paddle up and down using the q and a keys while the right paddle is controlled with the p and l keys. You may check the code for the Lunarlander game which demonstrates some of the techniques you can use to capture user input. You will also probably find the plot, set, line and text commands useful in your program. You have used most of these before in the billiard simulation and you can use Matlabs online help to get more details on the many options these functions offer. Your program should allow you to play a game to 11 keeping track of score appropriately. The general structure of the code is outlined below in pseudo code While not done Update Ball State (position and velocity) taking into account collisions with walls and paddles Check for scoring and handle appropriately Update Display Note that in this case it is implicitly assumed that capturing the user input and moving the paddles is being handled with callback functions which removes that functionality from the main loop. For extra credit you could consider adding extra features like spin or gravity to the ball flight or providing a single player mode where the computer controls one of the paddles.
最新发布
05-30

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值