using System using System Collections Generic using System Collections

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Arcs
{
class ArcsBundle
{
protected readonly int Nmax;
protected readonly int[] head;
protected int[] dest;
protected int[] next;
protected int ArcsCap;
public int ArcsCount { get; protected set; }
private const int defaultCapacity = 50;
public ArcsBundle(int Nmax)
{
this.Nmax = Nmax;
head = new int[Nmax];
for (int i = 0; i < Nmax; ++i)
{
head[i] = -1;
}
ArcsCount = 0;
ArcsCap = defaultCapacity;
dest = new int[ArcsCap];
next = new int[ArcsCap];
}
public ArcsBundle(int Nmax, int capacity)
{
this.Nmax = Nmax;
head = new int[Nmax];
for (int i = 0; i < Nmax; ++i)
{
head[i] = -1;
}
ArcsCount = 0;
ArcsCap = capacity;
dest = new int[ArcsCap];
next = new int[ArcsCap];
}
public ArcsBundle(int Nmax, int[] from, int[] to)
{
if (from.Length != to.Length)
{
throw new Exception("Wrong constructor args.");
}
this.Nmax = Nmax;
head = new int[Nmax];
for (int i = 0; i < Nmax; ++i)
{
head[i] = -1;
}
ArcsCount = 0;
ArcsCap = from.Length;
dest = new int[ArcsCap];
next = new int[ArcsCap];
for (int i = 0; i < ArcsCap; ++i)
{
AddArc(from[i], to[i]);
}
}
public static ArcsBundle CreateUnorientedGraph(int vertnum, int[] u, int[] v)
{
if (u.Length != v.Length)
{
throw new Exception("Wrong constructor args.");
}
ArcsBundle ab = new ArcsBundle(vertnum, 2 * u.Length);
for (int i = 0; i < u.Length; ++i)
{
ab.AddArc(v[i], u[i]);
ab.AddArc(u[i], v[i]);
}
return ab;
}
public void AddArc(int from, int to)
{
if (ArcsCap == ArcsCount)
{
ArcsCap *= 2;
int[] tDest = new int[ArcsCap];
int[] tNext = new int[ArcsCap];
dest.CopyTo(tDest, 0);
next.CopyTo(tNext, 0);
dest = tDest;
next = tNext;
}
dest[ArcsCount] = to;
next[ArcsCount] = head[from];
head[from] = ArcsCount++;
}
public class Bundle : IEnumerable
{
protected ArcsBundle handler;
public int vertex { get; protected set; }
public Bundle(int v, ArcsBundle handler)
{
this.handler = handler;
vertex = v;
}
public IEnumerator GetEnumerator()
{
for (int arc = handler.head[vertex]; arc != -1; arc = handler.next[arc])
{
yield return handler.dest[arc];
}
}
}
public Bundle this[int vertex]
{
get
{
if (vertex < 0 || vertex >= Nmax)
{
throw new Exception("Wrong vertex number");
}
return new Bundle(vertex, this);
}
}
}
class Program
{
static void Main(string[] args)
{
int VERTNUM = 10;
int[] from = new int[]{1, 2, 4, 5, 9};
int[] to = new int[]{2, 3, 5, 6, 6,};
ArcsBundle ab = ArcsBundle.CreateUnorientedGraph(VERTNUM, from, to);
List<List<int> > components = new List<List<int> >();
bool[] used = new bool[VERTNUM];
for (int i = 0; i < VERTNUM; ++i)
{
if (!used[i])
{
components.Add(new List<int>());
Queue<int> q = new Queue<int>();
q.Enqueue(i);
used[i] = true;
while (q.Count != 0)
{
int v = q.Dequeue();
components.Last().Add(v);
foreach (int x in ab[v])
{
if (!used[x])
{
used[x] = true;
q.Enqueue(x);
}
}
}
}
}
for (int i = 0; i < components.Count; ++i)
{
Console.Write("Component #{0}", i);
foreach(int x in components[i]){
Console.Write(" {0}", x);
}
Console.WriteLine();
}
}
}
}