06-图3 六度空间 (30 分)

06-图3 六度空间 (30 分)

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


图1 六度空间示意图

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

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

输入格式:

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

输出格式:

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

输入样例:

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%

效率不行,大数据超时

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Net;
using System.Text;
using System.Xml;

class P
{
    public P(string s)
    {
        var ss = s.Split(' ');
        x = int.Parse(ss[0]);
        y = int.Parse(ss[1]);
        check = false;

        if (x > y)
        {
            int temp = x;
            x = y;
            y = temp;
        }

    }
    public P(string s, int i)
    {

        var ss = s.Split(' ');
        x = int.Parse(ss[0]);
        y = int.Parse(ss[1]);
        check = false;
    }
    public bool check;
    public int x, y;
}


class T
{

    static List<int> get六度空间(List<P> list, int x)
    {
        List<int> li = new List<int>();

        for (int i = 0; i < list.Count; i++)
        {
            if (list[i].check == false)
            {
                var item = list[i];
                if (item.x == x)
                {
                    li.Add(item.y);
                    list[i].check = true;
                    i--;
                }
                if (item.y == x)
                {
                    li.Add(item.x);
                    list[i].check = true;
                    i--;
                }
            }
        }

        return li;

    }


    static void Main(string[] args)
    {
        //六度空间
        P first = new P(Console.ReadLine(), 1);
        List<P> list = new List<P>();

        for (int i = 0; i < first.y; i++)
        {

            list.Add(new P(Console.ReadLine()));
        }


        for (int ia = 1; ia < first.x + 1; ia++)
        {for (int i = 0; i < list.Count; i++)
            {
                list[i].check = false;
            }
            List<int> treeList = new List<int>();
            int C = 0;

            treeList.Clear();
            treeList.Add(ia);
            var ii = treeList.Count;
            for (int i = 0; i < treeList.Count; i++)
            {
                foreach (var item in get六度空间(list, treeList[i]))
                {
                    if (!treeList.Contains(item))
                    {
                        treeList.Add(item);

                    }
                }
                if (i == ii - 1)
                {
                    C++;
                    ii = treeList.Count;
                }
                if (C == 6)
                {
                    break;
                }

            }
             

            var p = ia + ": " + (treeList.Count * 1.0 / first.x).ToString("0.00%"); ;
            Console.WriteLine(p);
        }
        return;

    }

}

猜你喜欢

转载自www.cnblogs.com/interim/p/9774343.html