拓扑排序
在图论中,拓扑排序(TopologicalSorting)是一个有向无环图(DAG,DirectedAcyclicGraph)的所有
顶点的线性序列。且该序列必须满足下面两个条件:
1.每个顶点出现且只出现一次。
2.若存在一条从顶点A到顶点B的路径,那么在序列中顶点A出现在顶点B的前面。
拓扑排序通常用来“排序”具有依赖关系的任务。
比如,如果用一个DAG图来表示一个工程,其中每个顶点表示工程中的一个任务,用有向边<A,B><A,B>表
示在做任务B之前必须先完成任务A。故在这个工程中,任意两个任务要么具有确定的先后关系,要么是没
有关系,绝对不存在互相矛盾的关系(即环路)。(有优良的特性,在实现过程中常常能维护某些性质)
应用
(常作为一道题中一部分,并综合其他内容)
•有向图判环
•例题:
给你0~n-1这n个点,然后给出m个关系,u,v代表u->v的
单向边,问你这m个关系中是否产生冲突。
•判断关系是否唯一
判断队列里是否每次最多只存在至多一个元素
模板
CodeForces - 919D Substring
You are given a graph with nn nodes and mm directed edges. One lowercase letter is assigned to each node. We define a path's value as the number of the most frequently occurring letter. For example, if letters on a path are "abaca", then the value of that path is 33 . Your task is find a path whose value is the largest.
Input
The first line contains two positive integers n,mn,m (1≤n,m≤3000001≤n,m≤300000 ), denoting that the graph has nn nodes and mm directed edges.
The second line contains a string ss with only lowercase English letters. The ii -th character is the letter assigned to the ii -th node.
Then mm lines follow. Each line contains two integers x,yx,y (1≤x,y≤n1≤x,y≤n ), describing a directed edge from xx to yy . Note that xx can be equal to yy and there can be multiple edges between xx and yy . Also the graph can be not connected.
Output
Output a single line with a single integer denoting the largest value. If the value can be arbitrarily large, output -1 instead.
Examples
Input
5 4 abaca 1 2 1 3 3 4 4 5
Output
3
Input
6 6 xzyabc 1 2 3 1 2 3 5 4 4 3 6 4
Output
-1
Input
10 14 xzyzyzyzqx 1 2 2 4 3 5 4 5 2 6 6 8 6 5 2 10 3 9 10 9 4 6 1 10 2 8 3 7
Output
4
Note
In the first sample, the path with largest value is 1→3→4→51→3→4→5 . The value is 33 because the letter 'a' appears 33 times.
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <cstring>
#include <string>
#include <queue>
using namespace std;
typedef long long LL;
const int maxn = 3e5 + 5;
int n ,m;
char a[maxn]; //存放字符
int ind[maxn]; //每个点的入度,接下来会有拓扑排序
int dp[maxn][30]; //转移 第二维是26种字符的转移状态
vector <int> v[maxn]; //建树
bool topsort() //拓扑排序
{
queue <int> q;
for(int i = 1; i <= n; i++)
{
if(ind[i] == 0)
{
q.push(i);
dp[i][a[i] - 'a'] ++; //每个点其实状态
ind[i] = -1;
}
}
int f = 0;
while(!q.empty())
{
int x = q.front(); //父节点
f++; //记录树上点的数量,假如有环的话 f会小于n
q.pop();
for(int i = 0;i < (int)v[x].size();i++)
{
int y = v[x][i]; //子节点
ind[y]--;
if(ind[y] == 0)
{
q.push(y);
ind[y] = -1;
}
for(int j = 0;j < 26;j++) //更新26种状态
{
if(a[y] - 'a' == j) //如果当前子节点的字符为j
dp[y][j] = max(dp[y][j], dp[x][j] + 1); //则值会有父亲节点j点的位置 + 1和自己本身的值转移过来
else
dp[y][j] = max(dp[y][j], dp[x][j]); //否者由父亲节点和自己的j位置转移过来
}
}
}
if(f == n) return true; //如果没有环
else return false;
}
int main()
{
scanf("%d %d %s",&n, &m, a + 1);
memset(ind, 0, sizeof(ind));
for(int i = 1;i <= n;i++) v[i].clear();
for(int i = 1;i<= m;i++)
{
int x, y;
scanf("%d %d ",&x, &y);
v[x].push_back(y); //建树
ind[y]++;
}
if(topsort() == 0) printf("-1\n"); //有环
else{
int ans = 0;
for(int i = 1; i <= n;i ++){
for(int j = 0; j < 26;j ++){
ans = max(ans,dp[i][j]); //暴力去跑一边 取最大值
}
}
printf("%d\n",ans);
}
return 0;
}
参考博客:
https://blog.csdn.net/Strawberry_595/article/details/81150142