const int N = 100010, M = 5010;
class Solution {
public:
int h[N], e[M], ne[M], idx = 0;
int in[N], q[N];
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
in[b] ++ ;
}
bool canFinish(int n, vector<vector<int>> &g) {
memset(h, -1, sizeof h);
for (auto& c: g)
add(c[1], c[0]);
int cnt = 0;
int hh = 0, tt = -1;
for (int i = 0; i < n; i ++ )
if (!in[i])
q[++ tt] = i;
while (hh <= tt)
{
int t = q[hh ++];
cnt ++ ;
for (int i = h[t]; i != -1; i = ne[i])
{
int j = e[i];
if (-- in[j] == 0)
q[++ tt] = j;
}
}
return cnt == n;
}
};
const int N = 100010, M = 5010;
class Solution {
public:
vector<int> res;
int h[N], e[M], ne[M], idx;
int in[N], q[N];
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
in[b] ++ ;
}
vector<int> findOrder(int n, vector<vector<int>>& g) {
memset(h, -1, sizeof h);
for (auto& c: g)
add(c[1], c[0]);
int hh = 0, tt = -1;
int cnt = 0;
for (int i = 0; i < n; i ++ )
if (!in[i])
q[++ tt] = i;
while (hh <= tt)
{
int t = q[hh ++];
cnt ++ ;
for (int i = h[t]; i != -1; i = ne[i])
{
int j = e[i];
if (-- in[j] == 0)
q[++ tt] = j;
}
}
if (cnt == n)
{
for (int i = 0; i < n; i ++ )
res.push_back(q[i]);
}
return res;
}
};
讲解传送门
const int N = 26, M = N * N;
class Solution {
public:
int h[N], e[M], ne[M], idx = 0;
bool st[N];
int in[N], cnt = 0;
void add(int a, int b)
{
e[idx] = b, ne[idx]= h[a], h[a] = idx ++ ;
}
bool build(string s, string t)
{
int n = min(s.size(), t.size());
for (int i = 0; i < n; i ++ )
{
int a = s[i] - 'a', b = t[i] - 'a';
if (a != b)
{
add(a, b);
in[b] ++ ;
return true;
}
}
return s.size() <= t.size();
}
string alienOrder(vector<string>& words) {
memset(h, -1, sizeof h);
int n = words.size();
string res = "";
for (int i = 0; i < n; i ++ )
{
for (auto c: words[i])
{
if (!st[c - 'a'])
{
cnt ++ ;
st[c - 'a'] = true;
}
}
for (int j = 0; j < i; j ++ )
{
if (!build(words[j], words[i]))
return "";
}
}
queue<int> q;
for (int i = 0; i < 26; i ++ )
{
if (!in[i] && st[i])
q.push(i);
}
while (q.size())
{
int t = q.front();
q.pop();
res += (char)(t + 'a');
for (int i = h[t]; i != -1; i = ne[i])
{
int j = e[i];
if (-- in[j] == 0)
q.push(j);
}
}
return cnt == res.size() ? res : "";
}
};
讲解传送门
const int N = 510, M = N * N;
class Solution {
public:
int h[N], e[M], ne[M], idx = 0;
int in[N], q[N];
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
in[b] ++ ;
}
vector<int> loudAndRich(vector<vector<int>>& richer, vector<int>& quiet) {
memset(h, -1, sizeof h);
int n = quiet.size();
vector<int> res(n);
for (auto &c: richer)
add(c[0], c[1]);
int hh = 0, tt = -1;
for (int i = 0; i < n; i ++ )
{
res[i] = i;
if (!in[i])
q[++ tt] = i;
}
while (hh <= tt)
{
int t = q[hh ++];
for (int i = h[t]; i != -1; i = ne[i])
{
int j = e[i];
if (quiet[res[j]] > quiet[res[t]]) res[j] = res[t];
if (-- in[j] == 0)
q[++ tt] = j;
}
}
return res;
}
};
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
2022-04-12 【数据结构】单调栈专题