成都赛区网选 HDU 4039 The Social Network(BFS)

给出n个人物关系,p个查询,询问每个人的推荐好友(公共好友最多的且不是好友的人)。

将人物看作点,存在好友关系看作边,则题意转化为查询与某点最短路径长度为2的最多路径的点。

这样直接构图后BFS即可,将深度为2的存进队列查询次数,注意下输出格式和数组的大小,人是有可能超过1000的!

桂副队长的代码,好长。。。

#include<stdio.h>
#include<queue>
#include<iostream>
#include<map>
#include<string>
#include<string.h>
#include<stdlib.h>
using namespace std;

int adjl[3005][3005];

struct Node
{
    int temp;
    int step;
    Node()
    {
        temp = step = 0;
    }
};

int main()
{
    int t;
    scanf("%d",&t);
    int o = 1;
    while(t--)
    {
        memset(adjl , 0 , sizeof(adjl));
        int n,q;
        scanf("%d%d",&n,&q);
        int i,j,k;
        string ch , sh;
        map<string , int> Map;
        map<int , string> reMap;
        int Count = 1;
        for( i = 0 ; i < n ; i++)
        {
            cin>>ch>>sh;
            if(Map[ch] == 0 )
            {
                Map[ch] = Count++;
                //cout<<ch<<" "<<Count-1<<endl;
                reMap[Count-1] = ch;
            }
            if(Map[sh] == 0)
            {
                Map[sh] = Count++;
                reMap[Count-1] = sh;
                //cout<<sh<<" "<<Count-1<<endl;
            }
            //system("pause");
            adjl[Map[ch]][++adjl[Map[ch]][0]] =  Map[sh];
            adjl[Map[sh]][++adjl[Map[sh]][0]] =  Map[ch];
        }

        /*
        for( i = 1 ; i <= Count - 1; i++)
        cout<<reMap[i]<<" "<<Map[reMap[i]]<<endl;

        system("pause");
        */

        printf("Case %d:\n",o++);

        for( i = 0 ; i < q ; i++)
        {
            cin>>ch;
            Node start;
            start.temp = Map[ch];
            start.step = 0;
            queue<Node> myq;
            int Flag[3005] = {0};
            myq.push(start);

            int num[3005] = {0};

            int MAX = 0;

            int g[3005] = {0};
            int gg = 1;

            while(!myq.empty())
            {
                Node temp = myq.front();
                myq.pop();
                if(temp.step == 0)
                {
                    for( j = 1 ; j <= adjl[temp.temp][0] ; j++)
                    {
                        Flag[adjl[temp.temp][j]] = 1;
                        Node tt ;
                        tt.temp = adjl[temp.temp][j];

                        tt.step = 1;
                        myq.push(tt);
                    }
                }
                else if(temp.step == 1)
                {
                    for( j = 1 ; j <= adjl[temp.temp][0] ; j++)
                    {


                        int Adj = adjl[temp.temp][j];
                        if(Adj != start.temp)
                        {
                            if(Flag[Adj] == 0)
                            {
                                num[Adj]++;
                                if(MAX < num[Adj])
                                {
                                    MAX = num[Adj];
                                    gg = 1;
                                    g[gg++] = Adj;
                                }
                                else if(MAX == num[Adj])
                                {
                                    g[gg++] = Adj;
                                }
                            }
                        }
                    }
                }
                else break;
            }

            //system("pause");

            //printf("gg = %d start.temp = %d\n",gg,start.temp);

            map<string,int> mm;
            for( j = 1 ; j <= gg - 1 ; j++)
            {
                //cout<<reMap[j]<<endl;
                mm[reMap[g[j]]]++;
            }
            if(MAX == 0)
            {
                printf("-\n");
            }
            else
            {
                map<string,int>::iterator map_m=mm.begin();
                cout<<map_m->first;
                map_m++;
                for(;map_m!=mm.end();map_m++)
                {
                    cout<<" "<<map_m->first;
                }
                printf("\n");
            }
        }

    }
    return 0;
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值