zoukankan      html  css  js  c++  java
  • HDU4039(map应用)

    The Social Network

    Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
    Total Submission(s): 2958 Accepted Submission(s): 870


    Problem Description
    The social network system (SNS) helps people to keep connecting with their friends. Every user in SNS has a friends list. The user can read news posted by the users in his friends list. Friend relation is symmetric - if A is a friend of B, B is always a friend of A.

    Another important function in SNS is friend recommendation. One effective way to recommend friends is recommend by mutual friends. A mutual friend between two users A and B, is a user who is a friend of both A and B. A user can not be a friend of himself. For a specific user A, the system will recommend the user who is not himself or his friend, and has mutual friends with A. If more than one such user exists, recommend the one has most mutual friends with A. If still a tie exists, output all of them.

    Input
    The first line is a integer T (T≤100), the number of test case.
    The beginning of each test case is two integers N and Q, the number of friend relationship and the number of query. 1 ≤ N, Q ≤ 1000
    The following N lines each contain two different names separated by a single space. Each name consisted by only lowercase letters, and its length is less than or equal to 15. This means the two users are friends. No friend relationship will be given more than once.
    The following Q lines each describe a query. Each line contain one user name. The data guarantee that this name appears at least once in above N lines.

    Output
    For each case, you should output one line containing “Case k: ” first, where k indicates the case number and counts from one. Then for each query, output one line, contains one or more names of recommended friends, separate by a single space, sorted by alphabetical order. If no persons can be recommended, output one line contains “-”.

    Sample Input
    1
    10 11
    hongshu digua
    yingying hongshu
    xmm hongshu
    huaxianzi xmm
    tangjiejie huaxianzi
    xhmz yingying
    digua xhmz
    zt tangjiejie
    xmm lcy
    notonlysuccess ljq
    hongshu
    digua
    yingying
    xmm
    huaxianzi
    tangjiejie
    xhmz
    zt
    lcy
    notonlysuccess
    ljq

    Sample Output
    Case 1:
    xhmz
    yingying
    digua
    digua tangjiejie yingying
    hongshu lcy zt
    xmm
    hongshu
    huaxianzi
    hongshu huaxianzi
    -
    -

    题意:给出n组朋友关系,有q个user推荐好友查询.每次按字典序输出user的推荐好友。推荐好友定义:①是user好友的好友.②不是user本身和user的好友

    思路:map+暴搜

    #include <iostream>
    #include <string.h>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <map>
    using namespace std;
    const int MAXN=2005;
    int n,m;
    map<string,int> mp;
    map<int,string> rp;
    int tot;
    int dep[MAXN];
    vector<int> arc[MAXN];
    vector<string> fri;
    int vis[MAXN];
    int getID(string name)
    {
        if(mp[name]==0)
        {
            mp[name]=tot;
            rp[tot]=name;
            tot++;
        } 
        return mp[name];
    }
    void addedge(int u,int v)
    {
        arc[u].push_back(v);
        arc[v].push_back(u);
    }
    int main()
    {
        int T;
        cin>>T;
        for(int cas=1;cas<=T;cas++)
        {
            mp.clear();
            rp.clear();
            for(int i=1;i<tot;i++)
            {
                arc[i].clear();
            }
            tot=1;
            cin>>n>>m;
            for(int i=0;i<n;i++)
            {
                string A,B;
                cin>>A>>B;
                int u=getID(A);
                int v=getID(B);
                addedge(u,v);
            }
            cout<<"Case "<<cas<<":"<<endl;
            for(int i=0;i<m;i++)
            {
                memset(dep,0,sizeof(dep));
                memset(vis,0,sizeof(vis));
                fri.clear();
                string name;
                cin>>name;
                int u=getID(name);
                for(int i=0;i<arc[u].size();i++)
                {
                    int v=arc[u][i];
                    vis[v]=1;//标记u的好友 
                }
                for(int i=0;i<arc[u].size();i++)        
                {
                    int v=arc[u][i];
                    for(int j=0;j<arc[v].size();j++)
                    {
                        int to=arc[v][j];
                        if(to!=u&&!vis[to])    dep[to]++;//u的推荐好友不能是u自己和u的好友 
                    }
                }
                int mx=*max_element(dep,dep+tot);
                if(mx!=0)
                {
                    for(int i=1;i<tot;i++)
                    {
                        if(dep[i]==mx)
                        {
                            string name=rp[i];        
                            fri.push_back(name);
                        }
                    }
                    sort(fri.begin(),fri.end());
                    int len=fri.size();
                    for(int i=0;i<len-1;i++)
                    {
                        cout<<fri[i]<<" ";
                    }
                    cout<<fri[len-1]<<endl;
                }
                else    cout<<"-"<<endl;
            }
        }
        return 0;
    }
     
  • 相关阅读:
    mapreduce 本地调试需要注意的问题
    socket-----爬虫&&文件传输
    多个地点ping服务器
    linux grep命令详解
    关于真多核和加多核&线程由哪几部分组成
    内存溢出和内存泄漏
    指针和引用的区别
    Doxygen的使用,配置及实例
    【转】doxygen+graphviz生成工程中的类继承树及函数调用图
    转载--void指针(void *的用法)
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5692495.html
Copyright © 2011-2022 走看看