• 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;
    }


  • 相关阅读:
    spring boot的@RequestParam和@RequestBody的区别
    SpringBoot 中常用注解@PathVaribale/@RequestParam/@GetMapping介绍
    Required String parameter is not present
    Spring Boot 日志记录 SLF4J
    400错误,Required String parameter 'paramter' is not present
    初学 Spring boot 报错 Whitelabel Error Page 404
    Powershell获取WMI设备清单
    Powershell快速入门
    perl的Sys::Syslog模块(openlog,syslog,closelog函数,setlogsock)-自定义日志
    制作Windows10政府版的小白教程
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350924.html
Copyright © 2020-2023  润新知