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


  • 相关阅读:
    根据经纬度获取距离
    获取本浏览器经纬度坐标
    仿造mongodb的存储方式存一些假数据
    ty修饰符 public private static
    ty 枚举类型
    限制字符串的选择
    typeScript类型别名
    ty 函数的讲解
    ty数组的讲解
    接口的讲解
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350924.html
Copyright © 2011-2022 走看看