using
System;
using
System.Collections.Generic;
public
class
Result {
public
int
winner;
public
int
loser;
public
Result(
int
winner,
int
loser)
{
this
.winner = winner;
this
.loser = loser;
}
}
public
class
TeamOrdering {
static
void
Main(
string
[] args)
{
int
[] teams = { 1, 2, 3, 4 };
Result[] results
= {
new
Result(1, 4),
new
Result(4, 3),
new
Result(2, 3),
new
Result(1, 2),
new
Result(2, 1),
new
Result(3, 1),
new
Result(2, 4) };
ArrangeTeams(teams, results);
}
static
void
ArrangeTeams(
int
[] teams, Result[] results)
{
Dictionary<
int
, List<
int
> > map
=
new
Dictionary<
int
, List<
int
> >();
int
winner = 0;
for
(
int
i = 0; i < results.Length; i++) {
winner = results[i].winner;
if
(map.ContainsKey(winner)) {
map[winner].Add(results[i].loser);
}
else
{
List<
int
> list =
new
List<
int
>();
list.Add(results[i].loser);
map.Add(winner, list);
}
}
List<
int
> output =
new
List<
int
>();
SetInOrder(teams, map, teams.Length, output);
foreach
(
int
item
in
output)
{
Console.Write(item +
" "
);
}
}
static
void
SetInOrder(
int
[] teams,
Dictionary<
int
, List<
int
> > map,
int
n, List<
int
> output)
{
if
(n < 1) {
return
;
}
else
if
(n == 1) {
output.Add(teams[n - 1]);
return
;
}
SetInOrder(teams, map, n - 1, output);
int
key = teams[n - 1];
int
i;
for
(i = 0; i < output.Count; i++) {
int
team = output[i];
List<
int
> losers = map[key];
if
(losers.IndexOf(team) != -1)
break
;
}
output.Insert(i, key);
}
}