using
System;
using
System.Collections.Generic;
using
System.Linq;
public
class
Program {
public
static
string
findTopology(List<List<
int
> > edges)
{
Dictionary<
int
, List<
int
> > connections
=
new
Dictionary<
int
, List<
int
> >();
foreach
(List<
int
> edge
in
edges)
{
int
node1 = edge[0];
int
node2 = edge[1];
if
(!connections.ContainsKey(node1)) {
List<
int
> nodes =
new
List<
int
>();
nodes.Add(node2);
connections[node1] = nodes;
}
else
{
connections[node1].Add(node2);
}
if
(!connections.ContainsKey(node2)) {
List<
int
> nodes =
new
List<
int
>();
nodes.Add(node1);
connections[node2] = nodes;
}
else
{
connections[node2].Add(node1);
}
}
bool
isLinear = connections.All(
nodes = > nodes.Value.Count == 2);
bool
isStar = connections.All(
nodes = > nodes.Value.Count == 1);
bool
isRing
= connections.All(nodes =
> nodes.Value.Count == 2)
&& (edges[0][0] == edges[edges.Count - 1][1]);
string
topology
= isLinear ?
"linear"
: isStar ?
"star"
: isRing ?
"ring"
:
"mesh"
;
return
topology;
}
public
static
void
Main()
{
List<List<
int
> > edges =
new
List<List<
int
> >() {
new
List<
int
>() { 1, 2 },
new
List<
int
>() { 2, 3 },
new
List<
int
>() { 3, 4 },
new
List<
int
>() { 4, 5 },
new
List<
int
>() { 5, 6 },
new
List<
int
>() { 6, 1 },
new
List<
int
>() { 1, 3 },
new
List<
int
>() { 2, 4 },
new
List<
int
>() { 3, 5 },
new
List<
int
>() { 4, 6 }
};
Console.WriteLine(findTopology(edges));
}
}