using System using System Collections Generic using System Linq using

 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlockSort
{
class Program
{
public class IJ
{
public int[] I, J, S;
public IJ(int defaultCapacity)
{
this.I = new int[defaultCapacity];
this.J = new int[defaultCapacity];
this.S = new int[defaultCapacity + 1];
for (int i = 0; i < defaultCapacity; ++i)
{
this.S[i] = 0;
}
}
}
static void Main(string[] args)
{
int[,] input = new int[6, 2] { { 3, 4 }, { 0, 1 }, { 0, 3 }, { 2, 4 }, { 4, 1 }, { 1, 2 } };
int n = 5, m = 6; //n - number of vertices, m - number of arcs;
IJ lol = new IJ(m);
for (int i = 0; i < m; ++i)
{
lol.I[i] = input[i, 0];
lol.J[i] = input[i, 1];
}
for (int i = 0; i < m; ++i)
{
lol.S[lol.I[i] + 1]++;
}
for (int i = 1; i <= n; ++i)
{
lol.S[i] += lol.S[i - 1];
}
Console.WriteLine("I J");
for (int i = 0; i < m; ++i)
{
Console.WriteLine(lol.I[i] + " " + lol.J[i]);
}
for (int i = 0; i < n; ++i)
{
while (i < lol.S[lol.I[i]])
{
int i0 = lol.I[i], j0 = lol.J[i];
lol.I[i] = lol.I[lol.S[i0]];
lol.J[i] = lol.J[lol.S[i0]];
lol.I[lol.S[i0]] = i0;
lol.J[lol.S[i0]] = j0;
if (i >= lol.S[lol.I[i]])
{
lol.S[i]++;
i++;
}
}
}
Console.WriteLine();
Console.WriteLine("I J");
for (int i = 0; i < m; ++i)
{
Console.WriteLine(lol.I[i] + " " + lol.J[i]);
}
}
}
}