更新了代码,可以了
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; } public P parent; public int step = int.MaxValue; public bool check; public int x, y;}class T{ static void print(List list) { string msg = "{ "; foreach (var item in list) { msg += item + " "; } msg += "}"; Console.WriteLine(msg); } static void Main(string[] args) { P first = new P(Console.ReadLine()); int[][] data = new int[first.x][]; for (int i = 0; i < first.x; i++) { data[i] = new int[first.x]; } for (int i = 0; i < first.y; i++) { var temp = new P(Console.ReadLine()); data[temp.x][temp.y] = int.MaxValue; data[temp.y][temp.x] = int.MaxValue; } for (int j = 0; j < first.x; j++) { data[j][j] = int.MaxValue; } string allmsg = ""; for (int j = 0; j < first.x; j++) { string msg = ""; msg = Dfs有列求行(ref msg, j, data); if (msg.Trim().Length > 0) Console.WriteLine("{ " + msg.Trim() + " }"); } for (int i = 0; i < first.x; i++) { for (int j = 0; j < first.x; j++) { if (data[i][j] == 1) data[i][j] = int.MaxValue; } } List listAll = new List (); for (int i = 0; i < first.x; i++) { for (int j = 0; j < first.x; j++) { if (data[i][j] == int.MaxValue) { //data[i][j] List list = new List (); list.Add(i); Bfs有列求行(list, data); listAll.AddRange(list.ToArray()); print(list); list.Clear(); } } } for (int i = 0; i < first.x; i++) { if (!listAll.Contains(i)) { Console.WriteLine("{ " + i + " }"); } } } static string Dfs有列求行(ref string newmsg, int i, int[][] data) { if (data[i][i] == int.MaxValue) { data[i][i] = 1; newmsg += i + " "; } for (int j = 0; j < data[i].Length; j++) { if (data[j][i] == int.MaxValue) { data[j][i] = 1; data[i][j] = 1; if (newmsg.Contains(j + "")) { continue; } newmsg = Dfs有列求行(ref newmsg, j, data); } } return newmsg; } static void Bfs有列求行(List list, int[][] data) { for (int i = 0; i < list.Count; i++) { for (int j = 0; j < data[0].Length; j++) { var x = list[i]; if (data[j][x] == int.MaxValue || data[x][j] == int.MaxValue) { data[j][x] = 1; data[x][j] = 1; if (list.Contains(j)) { continue; } list.Add(j); } } } } }
06-图1 列出连通集 (25 分)
给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集。假设顶点从0到N−1编号。进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点。
输入格式:
输入第1行给出2个整数N(0<N≤10)和E,分别是图的顶点数和边数。随后E行,每行给出一条边的两个端点。每行中的数字之间用1空格分隔。
输出格式:
按照"{ v1 v2 ... vk }"的格式,每行输出一个连通集。先输出DFS的结果,再输出BFS的结果。
输入样例:
8 60 70 12 04 12 43 5
输出样例:
{ 0 1 4 2 7 }{ 3 5 }{ 6 }{ 0 1 2 7 4 }{ 3 5 }{ 6 } 越来越没什么兴趣了 这个不是完全版,有一条没通过,不知道为啥
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 void Main(string[] args) { P first = new P(Console.ReadLine(), 1); Listlist = new List
(); for (int i = 0; i < first.y; i++) { list.Add(new P(Console.ReadLine())); } //排序 list.Sort(delegate (P a, P b) { if (a.x < b.x) return -1; if (a.x > b.x) return 1; if (a.x == b.x) { if (a.y < b.y) return -1; if (a.y > b.y) return 1; } return 0; }); //============= int temp = -1; for (int i = 0; i < first.x; i++) { if (list.Count > i) { if (list[i].x != i) { list.Insert(i, new P(i + " "+temp--));//特意插入一些不存在的 线.因为最后他要求从大到小的输出.所以加了这段 } } else { list.Add( new P(i + " "+temp--)); } } //=================== #region DFS List
list2 = new List
(); list2.AddRange(list); DFS(first, list2); #endregion BFS(first, list); return; } static List
dGet(Listl, int x) { List
lp = new List (); for (int i = 0; i < l.Count; i++) { var item = l[i]; //查找线的第一个节点,此点存在线的列表里,把这个点和线的另一端的点,加入打印列表,删除这个线,递归找另一端,是不是在线的列表里. if (item.y == x) { lp.Add(item.y); lp.Add(item.x); l.RemoveAt(i); lp.AddRange(dGet(l, item.x)); i--; } if (item.x == x) { lp.Add(item.x); lp.Add(item.y); l.RemoveAt(i); lp.AddRange(dGet(l, item.y)); i--; } } return lp; } ////// 在列表中的线,如果包含这个点,把另一端的点,返回,并把线删掉 /// /// /// ///static List get(List list, int x) { List
li = new List (); for (int i = 0; i < list.Count; i++) { var item = list[i]; if (item.x == x) { li.Add(item.y); list.RemoveAt(i); i--; } if (item.y == x) { li.Add(item.x); list.RemoveAt(i); i--; } } return li; } private static void DFS(P first, Listlist2) { List
treeList = new List (); List LLL = new List (); //构建一个 所有点的表 for (int i = 0; i < first.x; i++) { LLL.Add(i); } //当,给我的线,还有的话 while (list2.Count > 0) { treeList.Clear(); //查线的第一个节点,伸出去多少条线, var t = dGet(list2, list2[0].x); foreach (var item in t) { if (!treeList.Contains(item)) { treeList.Add(item); } } foreach (var item in treeList) { LLL.Remove(item); } print(treeList); } print(LLL, 1); } private static void BFS(P first, Listlist) { List
LLL = new List (); for (int i = 0; i < first.x; i++) { LLL.Add(i); } List treeList = new List (); while (list.Count > 0) { treeList.Clear(); treeList.Add(list[0].x); for (int i = 0; i < treeList.Count; i++) { foreach (var item in get(list, treeList[i])) { if (!treeList.Contains(item)) { treeList.Add(item); } } } foreach (var item in treeList) { LLL.Remove(item); } print(treeList); } print(LLL, 1); } private static void print(List treeList, int i = 0) { if (treeList.Count > 0) { if (i > 0)//剩余部分,剩余的,都是单独的一个个 { foreach (var item in treeList) { if (item > -1) { StringBuilder sb = new StringBuilder(); sb.Append("{ "); sb.Append(" " + item); sb.Append(" }"); Console.WriteLine(sb.ToString()); } } } else { StringBuilder sb = new StringBuilder(); sb.Append("{ "); foreach (var item in treeList) { if (item > -1) { sb.Append(" " + item); } } sb.Append(" }"); Console.WriteLine(sb.ToString()); } } }}