zoukankan      html  css  js  c++  java
  • Friend Chains

    Description

    For a group of people, there is an idea that everyone is equals to or less than 6 steps away from any other person in the group, by way of introduction. So that a chain of "a friend of a friend" can be made to connect any 2 persons and it contains no more than 7 persons.
    For example, if XXX is YYY’s friend and YYY is ZZZ’s friend, but XXX is not ZZZ's friend, then there is a friend chain of length 2 between XXX and ZZZ. The length of a friend chain is one less than the number of persons in the chain.
    Note that if XXX is YYY’s friend, then YYY is XXX’s friend. Give the group of people and the friend relationship between them. You want to know the minimum value k, which for any two persons in the group, there is a friend chain connecting them and the chain's length is no more than k .
     

    Input

    There are multiple cases. 
    For each case, there is an integer N (2<= N <= 1000) which represents the number of people in the group. 
    Each of the next N lines contains a string which represents the name of one people. The string consists of alphabet letters and the length of it is no more than 10. 
    Then there is a number M (0<= M <= 10000) which represents the number of friend relationships in the group. 
    Each of the next M lines contains two names which are separated by a space ,and they are friends. 
    Input ends with N = 0.
     

    Output

    For each case, print the minimum value k in one line. 
    If the value of k is infinite, then print -1 instead.
     

    Sample Input

    3 XXX YYY ZZZ 2 XXX YYY YYY ZZZ 0
     

    Sample Output

    2
     
     
    题意:给定一个图,n个点,m条边。求任意两点的距离的最小值中的最大值。
    写了好久才发现理解错题意,以为是sicily的Longest path,求图中的最长路径。这种情况当有环出现的时候就不对了。
    o(︶︿︶)o 唉
    思路:
    对每个点进行BFS,求每个点的高度,最高那个便是ans.
    注意判断不连通的情况(一次BFS下来仍然没有vis到的就是不连通)
     
    #include <iostream>
    #include <queue>
    #include <vector>
    #include <string>
    #include <cstring>
    #include <map>
    using namespace std;
    
    #define N 1002
    #define M 10002
    #define INT 9999
    string s;
    int n, m, ans;
    bool vis[N];
    vector<int> v[N];
    map<string, int> maps;
    
    void init()
    {
        ans = -1;
        
        for (int i = 0; i < N; ++i)
        {
            while (!v[i].empty())
            {
                v[i].pop_back();
            }
        }
        
        maps.erase(maps.begin(), maps.end());
    }
    
    void input()
    {
        for (int i = 0; i < n; ++i)
        {
            cin >> s;
            
            maps[s] = i;
        }
        
        cin >> m;
        
        //构建邻接表 
        for (int i = 0; i < m; ++i)
        {
            string a, b;
            
            cin >> a >> b;
            
            v[maps[a]].push_back(maps[b]);
            
            v[maps[b]].push_back(maps[a]);
        }
        
    }
    
    void BFS()
    {
        for (int k = 0; k < n; ++k)
        {
            memset(vis, 0, sizeof(vis));
            
            int layer[N] = {0};
            
            queue<int> q;
            
            q.push(k);
            
            vis[k] = 1;
            
            while (!q.empty())
            {    
                int cur = q.front();
                
                q.pop();
                
                for (int i = 0; i < v[cur].size(); ++i)
                {
                    if (!vis[v[cur][i]])
                    {
                        q.push(v[cur][i]);
                        
                        layer[v[cur][i]] = layer[cur]+1;
                        
                        vis[v[cur][i]] = 1;
                    }
                }
            }
            
            for (int i = 0; i < n; ++i)    //连通的话,一次BFS能遍历所有的点 
                if (!vis[i])
                    return;
                    
            ans = max(ans, *max_element(layer, layer+N));
        }
    }
    
    void print()
    {
        cout << ans << endl;
    }
    
    int main()
    {    
        while (cin >> n, n)
        {
            init();
            
            input();
            
            BFS();
            
            print();
        }
    }
    View Code
     
  • 相关阅读:
    c语言结构体数组引用
    c语言结构体数组定义的三种方式
    如何为SAP WebIDE开发扩展(Extension),并部署到SAP云平台上
    SAP SRM ABAP Webdynpro和CFCA usb key集成的一个原型开发
    使用SAP API portal进行SAP SuccessFactors的API测试
    SAP UI5应用里的页面路由处理
    在SAP WebIDE Database Explorer里操作hdi实例
    如何使用SAP事务码SAT进行UI应用的性能分析
    使用SAP WebIDE进行SAP Cloud Platform Business Application开发
    SAP CRM WebClient UI ON_NEW_FOCUS的用途
  • 原文地址:https://www.cnblogs.com/chenyg32/p/3136186.html
Copyright © 2011-2022 走看看