zoukankan      html  css  js  c++  java
  • ZOJ 3172 Extend 7-day Vacation

    求树的直径:
    任选一点求BFS,所得的距离这个点最远的一定是直径上的一个距离该点较远的端点,再进行一次BFS就能找到直径。。。。。

    Extend 7-day Vacation

    Time Limit: 1 Second      Memory Limit: 32768 KB

    CYJJ is planning on her vacation. Since her boss has permitted her to stay at each city for a day with as many cities to visit as she like, she would try to extend her 7-day vacation as much as possible. With a map of highways connecting the cities in hands, she would like to find such two cities that there are the most number of cities on the path between them.

    Note

    the map shows that all the cities are connected either directly or indirectly by highways. However, to return to where she was, CYJJ would have to pass at least one highway twice.

    Input

    Each case starts with a line containing the number of cities N (1 <= N <= 1000), hence the cities are numbered from 0 to N - 1, and the number of highways M. Then M lines follow, each contains the two cities that a highway is directly adjacent to.
    Consective cases are separated by an empty line.

    Output

    For each test case, if it is possible to extend CYJJ's vacation, output in a line the maximum number of days she can take to have the vacation. Or if it is impossible, print in a line "Impossible".

    Sample Input

    10 9
    0 8
    0 1
    1 2
    1 3
    3 4
    4 5
    5 7
    5 6
    6 9

    5 4
    0 1
    1 2
    1 3
    3 4

    Sample Output

    8
    Impossible
    Author: CHEN, Yue
    Source: ZOJ 7th Anniversary Contest


    #include <iostream>

    #include <cstdio>
    #include <cstring>
    #include <queue>

    #define CLS(x,c)  memset(x,c,sizeof(x))

    using namespace std;

    int main()
    {
        int n,m,x,y;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        bool gp[1111][1111];
        int dist[1111],vis[1111];
        CLS(gp,false); CLS(dist,0);CLS(vis,0);

        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            gp[x][y]=gp[y][x]=true;
        }

        queue<int> q;
        q.push(0);    vis[0]=1;
        while(!q.empty())
        {
            int a;
            a=q.front();  q.pop();
            for(int i=0;i<n;i++)
            {
                if(gp[a]&&!vis)
                {
                    vis=1;
                    dist=dist[a]+1;
                    q.push(i);
                }
            }
        }

        int maxn=0,pos=0;
        for(int i=0;i<n;i++)
        {
            if(dist>maxn)
            {
                maxn=dist;  pos=i;
            }
        }

        q.push(pos);
        CLS(dist,0); CLS(vis,0); vis[pos]=1;
        while(!q.empty())
        {
            int a;
            a=q.front();  q.pop();
            for(int i=0;i<n;i++)
            {
                if(gp[a]&&!vis)
                {
                    vis=1;
                    dist=dist[a]+1;
                    q.push(i);
                }
            }
        }

        maxn=0;
        for(int i=0;i<n;i++)
        {
            if(dist>maxn)
            {
                maxn=dist;
            }
        }

        maxn++;
        if(maxn>=7) printf("%d ",maxn);
        else puts("Impossible");
    }
        return 0;
    }


  • 相关阅读:
    backbone.js初体验--构建简单分页应用时踩到的坑
    使用r.js打包js文件
    javascript原型式继承
    javascript浮点数运算修正
    javascript对象的浅复制与深复制
    javascript类式继承
    初识requirejs(二)
    标准版SCADA 上线了~~ 三菱 Fanuc 广数 华中 西门子 HAAS等等 可以做到一套程序通用,采集所有CNC PLC
    KepServerEX读写三菱PLC,车间现场测试记录,带你了解【数据采集的困境】的前世与今生
    Mitsubishi (三菱) Fanuc(发那科),CNC,网口数据采集,NC程序下发(其它品牌CNC,哈斯 马扎克 兄弟等,正在开发中)
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350924.html
Copyright © 2011-2022 走看看