using
System;
using
System.Collections.Generic;
using
System.Linq;
class
MainClass
{
static
List<SortedDictionary<
int
,
int
>> edges;
static
int
GetEdgeLength(
int
a,
int
w)
{
SortedDictionary<
int
,
int
> edgeMap = edges[a];
int
index = FindFloorKeyIndex(edgeMap.Keys, w);
if
(index == -1)
{
return
1;
}
int
key = edgeMap.Keys.ElementAt(index);
return
edgeMap[key] + 1;
}
static
int
FindFloorKeyIndex(IEnumerable<
int
> keys,
int
w)
{
int
[] keysArray = keys.ToArray();
int
index = Array.BinarySearch(keysArray, w);
if
(index >= 0)
{
return
index;
}
index = ~index - 1;
return
index;
}
public
static
void
Main(
string
[] args)
{
int
n = 3;
edges =
new
List<SortedDictionary<
int
,
int
>>(n + 1);
for
(
int
i = 0; i <= n; i++)
{
edges.Add(
new
SortedDictionary<
int
,
int
>());
}
List<Triple<
int
,
int
,
int
>> inputEdges =
new
List<Triple<
int
,
int
,
int
>>
{
new
Triple<
int
,
int
,
int
>(3, 1, 3),
new
Triple<
int
,
int
,
int
>(1, 2, 1),
new
Triple<
int
,
int
,
int
>(2, 3, 2)
};
int
maxLength = 0;
foreach
(Triple<
int
,
int
,
int
> edge
in
inputEdges)
{
int
a = edge.First, b = edge.Second, w = edge.Third;
int
lengthAtA = GetEdgeLength(a, w);
if
(GetEdgeLength(b, w + 1) > lengthAtA)
{
continue
;
}
if
(!edges[b].ContainsKey(w))
{
edges[b][w] = 0;
}
edges[b][w] = Math.Max(edges[b][w], lengthAtA);
foreach
(
var
key
in
new
List<
int
>(edges[b].Keys))
{
if
(key > w && edges[b][key] <= lengthAtA)
{
edges[b].Remove(key);
}
else
{
break
;
}
}
maxLength = Math.Max(maxLength, lengthAtA);
}
Console.WriteLine(maxLength);
}
class
Triple<U, V, W>
{
public
U First {
get
; }
public
V Second {
get
; }
public
W Third {
get
; }
public
Triple(U first, V second, W third)
{
First = first;
Second = second;
Third = third;
}
}
}