05-图3. 六度空间 (30)


时间限制
1500 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard

“六度空间”理论又称作“六度分隔(Six Degrees of Separation)”理论。这个理论可以通俗地阐述为:“你和任何一个陌生人之间所间隔的人不会超过六个,也就是说,最多通过五个人你就能够认识任何一个陌生人。”如图6.4所示。


图6.4 六度空间示意图

“六度空间”理论虽然得到广泛的认同,并且正在得到越来越多的应用。但是数十年来,试图验证这个理论始终是许多社会学家努力追求的目标。然而由于历史的原因,这样的研究具有太大的局限性和困难。随着当代人的联络主要依赖于电话、短信、微信以及因特网上即时通信等工具,能够体现社交网络关系的一手数据已经逐渐使得“六度空间”理论的验证成为可能。

假如给你一个社交网络图,请你对每个节点计算符合“六度空间”理论的结点占结点总数的百分比。

输入格式说明:

输入第1行给出两个正整数,分别表示社交网络图的结点数N (1<N<=104,表示人数)、边数M(<=33*N,表示社交关系数)。随后的M行对应M条边,每行给出一对正整数,分别是该条边直接连通的两个结点的编号(节点从1到N编号)。

输出格式说明:

对每个结点输出与该结点距离不超过6的结点数占结点总数的百分比,精确到小数点后2位。每个结节点输出一行,格式为“结点编号:(空格)百分比%”。

样例输入与输出:

序号 输入 输出
1
10 9
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
1: 70.00%
2: 80.00%
3: 90.00%
4: 100.00%
5: 100.00%
6: 100.00%
7: 100.00%
8: 90.00%
9: 80.00%
10: 70.00%
2
10 8
1 2
2 3
3 4
4 5
5 6
6 7
7 8
9 10
1: 70.00%
2: 80.00%
3: 80.00%
4: 80.00%
5: 80.00%
6: 80.00%
7: 80.00%
8: 70.00%
9: 20.00%
10: 20.00%
3
11 10
1 2
1 3
1 4
4 5
6 5
6 7
6 8
8 9
8 10
10 11
1: 100.00%
2: 90.91%
3: 90.91%
4: 100.00%
5: 100.00%
6: 100.00%
7: 100.00%
8: 100.00%
9: 100.00%
10: 100.00%
11: 81.82%
4
2 1
1 2
1: 100.00%
2: 100.00%
 
#include <iostream>
#include <string.h>
#include <stdio.h>
#define N 10002
#define M 33*N
#include <malloc.h>
#include <queue>

using namespace std;

typedef struct ArcNode
{
    int node;
    struct ArcNode *next;
}ArcNode;

typedef struct
{
    int firNode;
    ArcNode *firstarc;
}FirVerNode;

typedef struct
{
    FirVerNode vertices[N];
    int vernum,arcnum;
}AlGraph;

bool visit[N]={false};
int  percent[N]={0};

void createFirNode(AlGraph &graph)
{
    for(int i=1;i<=graph.vernum;i++)
    {
        FirVerNode *k = (FirVerNode*)malloc(sizeof(FirVerNode));
        k->firNode = i;
        k->firstarc=NULL;
        graph.vertices[i]=*k;
    }
    ArcNode *p,*q;
    int start,endd;
    for(int i=1;i<=graph.arcnum;i++)
    {
        p = (ArcNode*)malloc(sizeof(ArcNode));
        cin>>start>>endd;
        p->node=endd;
        p->next=NULL;
        if(!visit[start])
        {
            visit[start]=true;
            graph.vertices[start].firstarc=p;
        }
        else
        {
            p->next=graph.vertices[start].firstarc;
            graph.vertices[start].firstarc = p;
        }
        p = (ArcNode*)malloc(sizeof(ArcNode));
        p->node=start;
        p->next=NULL;
        if(!visit[endd])
        {
            visit[endd]=true;
            graph.vertices[endd].firstarc = p;
        }
        else
        {
            p->next=graph.vertices[endd].firstarc;
            graph.vertices[endd].firstarc = p;
        }
    }

}

queue <int> que;

int BFS(AlGraph graph, int firVNode)
{
    int count=1;
    int level=0,head,tail,last=firVNode;
    visit[firVNode] = true;
    que.push(firVNode);
    while(!que.empty())
    {
        firVNode = que.front();
        head = firVNode;
        //cout<<"out: "<<firVNode<<endl;
        que.pop();
        ArcNode *p = graph.vertices[firVNode].firstarc;
        while(p)
        {
            //cout<<p->node<<" visit: "<<visit[p->node]<<endl;
            if(!visit[p->node])
            {
                visit[p->node]=true;
                //cout<<"push: "<<p->node<<endl;
                que.push(p->node);
                tail = p->node;
                count++;
            }
            p=p->next;
        }
        if(last==head)
        {
            last=tail;
            level++;
        }
        if(level==6)
        {
            while(!que.empty())
                que.pop();
            break;
        }

        //cin>>firVNode;
        /*for(int i=0;i<graph.vernum;i++)
        {
            if(!visit[i])
            {
                visit[i]=true;
                que.push(i);
                count++;
            }
        }*/
    }
    //cout<<"count:"<<count<<endl;
    return count;
}

void SDS(AlGraph graph)
{
    for(int v=1;v<=graph.vernum;v++)
    {
        memset(visit,false,graph.vernum+1);
        int count = BFS(graph,v);
        //cout<<v<<"="<<count*1.0/graph.vernum<<endl;
        printf("%d: %.2f%%\n",v,count*1.0/graph.vernum*100);
    }
}

void Display(AlGraph graph)
{
    for(int i=1;i<=graph.vernum;i++)
    {
        ArcNode *p = graph.vertices[i].firstarc;
        cout<<i<<" ";
        while(p)
        {
            cout<<p->node<<" ";
            p=p->next;
        }
        cout<<endl;
    }
}

int main()
{
    AlGraph graph;
    int n,m;
    cin>>graph.vernum>>graph.arcnum;
    createFirNode(graph);
    memset(visit,false,graph.vernum+1);

    //Display(graph);
    SDS(graph);

    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值