using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication3 { class Program { public static void Main() { m3(); } public static void m3() { LinkedListlist = new LinkedList (); StreamReader sr = new StreamReader(@"144047638844506.txt", Encoding.Default); String str = ""; while ((str = sr.ReadLine()) != null) { list.AddLast(str); } LinkedList pList = new LinkedList (); String[] strs; int id; int friendId; while (list.Count != 0) { str = list.First(); list.RemoveFirst(); strs = str.Split(' '); id = int.Parse(strs[0]); friendId = int.Parse(strs[1]); People root = new People(id, friendId); find(root, list); pList.AddLast(root); } Console.WriteLine(pList.Count); Console.Read(); } public static void find(People p, LinkedList list) { String str = ""; int pId; int pFriendId; for (int i = 0; i < list.Count; i++) { str = list.First(); list.RemoveFirst(); pId = int.Parse(str.Split(' ')[0]); pFriendId = int.Parse(str.Split(' ')[1]); if (pId == p.getId()) { p.addFriend(pFriendId); } else if (pFriendId == p.getId()) { p.addFriend(pId); } else { list.AddLast(str); } } if (p.hasFriend()) { foreach (People lp in p.getFriends()) { find(lp, list); } } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication3 { class People { private int id; private People[] friends; public People() { } public People(int id) { this.id = id; } public People(int id, int friends) { this.id = id; if (friends > 0) { this.friends = new People[1]; this.friends[0] = new People(friends); } } public int getId() { return id; } public void setId(int id) { this.id = id; } public People[] getFriends() { return friends; } public void setFriends(People[] friends) { this.friends = friends; } public void addFriend(int friendId) { if (this.friends == null || this.friends.Length <= 0) { this.friends = new People[] { new People(friendId) }; } else { People[] dest = new People[this.friends.Length + 1]; Array.Copy(this.friends, 0, dest, 0, this.friends.Length); dest[dest.Length - 1] = new People(friendId); this.friends = dest; } } public bool hasFriend() { return this.friends != null && this.friends.Length != 0; } public String toString() { return "People [id=" + id + ", friends=" + friends.Length; } } }
18 1 3 38 21 63 64 81 95 19 46 31 29 74 24 31 39 66 34 13 55 59 3 67 54 18 52 91 81 30 76 50 82 30 87 3 93 3 83 39 21 28 69 49 54 44 79 92 61 13 56 15 23 10 34 28 28 85 19 60 66 94 61 47 75 48 49 67 2 83 6 22 63 26 22 16 22 1 59 82 65 14 49 87 24 82 14 3 18 84 62 83 29 22 29 4 21 30 70 22 12 27 95 74 53 17 89 26 69 47 59 33 13 7 71 88 88 36 90 5 72 3 39 100 76 68 3 49 97 73 22 8 51 17 34 3 85 74 80 53 73 39 37 85 45 59 24 85 94 65 41 65 67 80 17 94 47 19 42 95 43 64 54 94 32 87 48 16 13 28 20 85 18 56 21 14 14 44 98 59 8 39 76 26 18 92 19 16 10 13 62 53 28 15 46 59 54 93 26 66 72 45 50 89 52 70 3 65 65 52 23 24 42 98 49 11 41 19 26 51 31 87 55 58 54 100 68 7 44 93 72 68 89 73 56 40 94 10 56 58 62 31 33 3 28 81 66 69 99 91 71 82 30 25 39 35 76 7 41 71 51 64 38 92 36 94 31 29 55 39 38 68 69 70 23 48 2 88 68 100 30 38 33 59 14 72 45 89 30 85 60 32 48 97 23 35 42 6 63 49 44 52 16 12 21 38 11 22 25 79 74 55 16 6 65 82 29 10 70 58 46 81 42 46 30 64 80 23 21 43 71 16 94 87 79 67 76 90 88 53 68 13 59 35 19 75 16 99 36 38 57 82 18 50 79 99 65 58 22 86 52 44 1 46 82 80 64 10 69 51 62 88 16 20 22 34 46 90 32 82 27 40 15 96 41 93 47 6 50 20 91 2 63 43 99 97 74 62 6 94 64 67 81 79 38 55 64 35 44 48 16 22 39 30 17 80 74 63 37 24 82 27 77 97 21 75 45 95 88 50 88 3 68 21 82 57 75 97 91 18 96 7 39 35 88 7 66 62 22 2 37 55 80 65 3 100 91 47 46 78 96 86 80 63 6 13 71 80 97 57 20 87 43 7 45 60 20 66 61 8 21 92 72 23 44 14 70 89 91 17 74 23 32 79 87 2 10 48 63 58 5 34 44 99 93 41 59 12 6 19 72 78 63 95 53 6 9 74 46 51 42 72 2 60 27 12 59 41 69 15 75 65 14 67 5 72 30 62 42 1 92 4 61 56 69 59 6 62 30 31 35 31 90 61 94 100 1 63 15 27 79 28 93 35 99 23 96 92 23 39 96 70 34 8 77 54 66 83 16 95 13 2 78 54 14 23 53 66 37 19 93 15 46 85 49 96 7 97 88 82 87 35 3 73 42 80 26 8 14 93 54 78 46 83 31 59 6 35 77 94 54 69 9 51 53 9 47 12 57 34 45 44 68 47 16 61 78 93 20 91 38 74 68 83 8 50 42 65 37 18 59 42 38 67 44 42 27 90 53 36 75 49 79 94 96 46 55 25 90 74 16 27 99 35 10 7 37 3 23 25 72 81 66 9 99 9 50 78 51 55 13 25 3 43 71 50 88 25 75 29 98 42 56 49 28 17 7 64 19 29 88 90 10 5 50 8 66 99 85 16 53 49 92 8 91 62 57 30 38 83 59 88 76 66 36 4 82 42 67 100 70 7 41 31 11 90 91 28 40 27 43 45 76 87 52 66 100 60 96 38 43 6 25 18 71 12 73 52 53 40 3 74 98 43 5 60 84 95 88 23 21 82 67 96 68 18 14 20 30 61 57 24 66 33 93 36 44 66 87 96 5 41 21 54 83 25 13 66 19 100 40 92 82 7 39 1 76 4 20 5 64 28 80 29 60 73 16 3 38 54 50 94 94 71 47 28 95 59 45 66 11 85 9 92 43 99 44 18 3 16 75 66 43 54 47 3 78 62 57 15 68 7 60 61 29 58 89 75 17 33 92 79 17 52 22 59 51 65 29 53 80 55 70 75 60 68 77 38 82 85 52 49 43 64 9 71 21 49 98 89 82 89 67 50 41 88 61 43 5 89 47 36 43 68 10 54 36 38 91 17 23 95 58 25 88 78 74 37 19 7 77 85 56 17 25 16 11 81 56 57 68 50 25 30 4 12 67 46 80 41 40 96 58 97 20 97 27 45 33 45 51 9 81 59 78 57 26 88 89 34 97 57 35 73 86 38 84 4 84 63 45 75 58 54 24 29 26 34 46 76 42 78 86 71 87 12 11 75 97 7 83 31 31 68 21 14 24 4 28 68 30 37 21 53 65 22 2 90 55 99 18 48 29 55 19 15 18 81 41 14 39 24 45 69 43 65 34 18 20 61 85 49 97 57 54 13 30 7 55 84 6 24 32 86 78 2 52 96 82 92 9 72 67 5 40 62 21 73 79 92 85 16 93 33 24 46 97 54 52 51 37 9 26 20 94 4 73 45 51 6 89 11 77 55 68 88 40 47 80 76 14 72 8 37 17 57 90 20 59 79 29 85 50 22 88 23 19 38 28 7 48 57 13 15 24 81 3 64 79 34 91 92 5 51 80 73 7 22 92 65 100 20 49 1 94 88 23 12 25 3 18 25 59 82 91 34 62 93 97 92 78 40 83 82 90 15 6 96 36 50 12 87 21 61 87 14 48 62 77 25 64 46 1 74 28 91 7 89 36 56 81 13 95 15 47 36 81 52 31 16 1 42 54 74 54 41 87 54 2 16 30 17 61 30 90 88 72 48 29 7 3 61 72 49 27 18 84 8 21 66 75 74 60 29 47 65 21 33 18 22 48 99 38 61 28 79 48 52 78 28 58 81 40 81 81 67 98 17 26 19 34 100 92 93 80 38 58 100 22 27 73 22 26 62 82 5 92 81 8 70 61 18 2 100 98 82 66 96 50 43 66 84 43 57 76 74 46 85 74 19 64 98 40 41 60 73 97 51 6 5 20 66 22 21 17 19 55 35 66 4 77 31 87 19 39 15 45 84 51 18 3 14 67 94 6 26 67 3 29 24 59 100 41 80 21 57 50 27 91 68 30 68 98 69 38 89 83 82 72 33 51 26 99 18 20 4 95 38 58 23 61 16 23 1 47 95 9 97 21 100 64 2 19 13 70 56 1 52 90 25 37 92 50 35 9 21 90 56 10 48 78 22 63 52 74 10 46 83 58 18 34 21 20 52 85 89 59 86 93 48 10 81 92 11 67 52 32 56 7 93 3 37 15 66 27 86 70 84 4 22 6 40 63 65 25 7 64 86 87 55 97 53 7 80 60 13 24 63 49 38 80 37 26 6 74 47 89 76 2 44 50 7 83 12 71 60 70 35 97 8 41 45 12 47 76 71 12 100 85 60 37 64 48 14 61 9 49 14 4 98 20 39 61 43 50 30 29 46 89 69 91 100 16 66 70 27 65 7 38 54 70 38 67 91 58 79 51 58 92 55 55 12 45 15 54 94 96 82 39 84 2 29 83 17 47 5 95 11 11 85 64 32 22 31 22 31 61 25 89 53 79 95 64 23 10 69 68 57 2 6 41 3 87 75 72 33 31 66 95 93 2 59 25 23 41 98 6 1 22 94 5 52 88 68 26 49 88 93 58 89 51 50 92 37 24 63 69 7 80 15 99 82 25 75 56 17 25 61 18 46 6 74 50 46 42 75 46 29 20 3 52 61 6 28 75 26 86 54 40 84 87 65 11 95 81 35 55 50 32 13 24 81 58 17 8 55 97 27 58 66 96 61 78 53 40 52 30 25 58 69 61 96 85 71 90 18 57 45 19 88 9 42 69 18 10 76 72 7 54 81 24 49 94 2 53 33 5 82 58 62 2 70 58 87 92 99 56 48 43 74 35 3 16 55 20 77 82 44 24 59 35 69 12 39 2 16 20 11 30 22 32 39 60 23 37 15 70 32 88 56 34 55 63 6 84 96 49 18 83 24 76 17 93 39 56 46 55 27 8 36 48 39 74 59 13 10 25 34 41 13 89 27 19 3 32 2 99 32 20 81 55 47 49 99 86 56 96 92 83 3 27 82 93 100 93 57 61 17 90 54 81 31 80 100 33 11 53 83 42 24 15 48 71 16 47 8 71 94 51 5 49 77 87 41 28 79 50 40 95 39 93 28 69 72 27 54 34 31 88 27 75 77 70 73 84 40 66 86 45 14 14 83 7 41 13 56 32 59 94 25 86 15 48 64 20 34 95 7 60 49 61 86 25 82 58 60 22 76 45 18 89 58 52 47 50 64 2 81 22 48 57 8 14 57 23 33 90 17 91 1 17 4 87 41 85 44 52 58 19 96 27 60 5 78 93 60 87 15 7 95 74 72 3 96 4 44 65 46 45 81 49 31 74 86 74 25 43 45 73 22 56 77 99 61 83 44 72 21 10 78 67 83 49 70 30 4 13 46 50 9 27 50 39 52 35 65 76 30 9 48 51 16 77 1