-
Notifications
You must be signed in to change notification settings - Fork 0
/
1857.有向图中最大颜色值.cs
96 lines (77 loc) · 2 KB
/
1857.有向图中最大颜色值.cs
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
/*
* @lc app=leetcode.cn id=1857 lang=csharp
*
* [1857] 有向图中最大颜色值
*/
// @lc code=start
public class Solution
{
struct Edge
{
public int to, next;
};
Edge[] E;
int num = 0;
int[] head;
private void AddEdge(int x, int y)
{
E[++num].next = head[x];
E[num].to = y;
head[x] = num;
}
int flag = 0;
int[] vis;
public int LargestPathValue(string colors, int[][] edges)
{
if (edges.Length == 0)
return 1;
int pNum = colors.Length;
int[] In = new int[pNum];
E = new Edge[edges.Length * 2];
head = new int[pNum];
for (int i = 0; i < edges.Length; i++)
{
AddEdge(edges[i][0], edges[i][1]);
In[edges[i][1]]++;
}
//toposort
Queue<int> queue = new Queue<int>();
for (int i = 0; i < pNum; i++)
if (In[i] == 0)
queue.Enqueue(i);
int pTopo = 0;
int[] topo = new int[pNum];
while (queue.Count != 0)
{
int st = queue.Dequeue();
topo[pTopo++] = st;
for (int i = head[st]; i != 0; i = E[i].next)
{
int qt = E[i].to;
if (--In[qt] == 0)
queue.Enqueue(qt);
}
}
if (pTopo != pNum)
return -1;
int[,] A = new int[pNum, 26];
int Ans = 0;
for (int i = 0; i < pNum; i++)
A[i, colors[i] - 'a'] = 1;
for (int i = 0; i <= 25; i++)
{
for (int j = 0; j < pNum; j++)
{
int st = topo[j];
for (int k = head[topo[j]]; k != 0; k = E[k].next)
{
int qt = E[k].to;
A[qt, i] = Math.Max(A[qt, i], A[st, i] + ((colors[qt] - 'a' == i) ? 1 : 0));
Ans = Math.Max(Ans, A[qt, i]);
}
}
}
return Ans;
}
}
// @lc code=end