题目描述
在一个有向无环图中,可能存在多种有效拓扑序列。以下图为例:
存在两种可行的拓扑序列:
0 1 2 3 4
0 2 1 3 4
本题会给出一个图,以及多个序列,你来判断每一个序列是否是该图的有效拓扑序列。
输入格式
第一行为2个正整数m和n,分别表示有向无环图的节点个数和边的数量。
接下来n行,代表n条边。分别是起点、终点、权重,以空格分隔。
(m<50,n<100)
接下来为一个正整数o,表示接下来会出现的序列个数。
(o<1000)
再往后是o个序列,序列中的每个值用空格隔开。
输出格式
按行输出:o个序列中,每一个序列是否为图的有效拓扑序列。
是的话输出:YES
不是的话输出:NO
输入样例
- 5 6
- 0 1
- 0 2
- 1 3
- 2 3
- 2 4
- 3 4
- 3
- 0 1 2 3 4
- 0 2 1 3 4
- 0 1 3 2 4
输出样例
- YES
- YES
- NO
代码展示
思路:循环判断当前结点入度,为零则将其邻接点入度-1,继续判断给定序列的下一结点。若为有效拓扑序列,(入度)则应按序陆续减为0;否则无效。
- #include
- #include
- #include
- #include
- using namespace std;
-
- #define INFO_MAX_SIZE 20
- #define MAX_SIZE 200
- //领接矩阵存储的图
- struct Graph{
- int vexNumber;
- string vexInfo[INFO_MAX_SIZE];
- int adjMatrix[MAX_SIZE][MAX_SIZE];
- };
- //弧结点定义
- struct ArcNode{
- int weight;//弧上的信息部分
- int adj;//邻接点的序号
- ArcNode *nextarc;
- };
- //顶点结点定义
- struct VexNode{
- string Info;
- ArcNode *firstarc;
- };
- //领接表结构的图的定义
- struct linkGraph{
- VexNode *vexes;
- int vexnumber;
- };
-
- struct tempNode{
- int col;
- int row;
- //tempNode *next;
- };
-
- struct temp{
- int num;
- tempNode *docu;
- };
-
-
- int preInitGraph(linkGraph &G,const Graph &g){
- G.vexes=new VexNode[g.vexNumber];
- G.vexnumber=g.vexNumber;
- for(int i=0;i
- G.vexes[i].firstarc=NULL;
- }
- return 0;
- }
- //将邻接矩阵存储的图转换为领接表存储的图
- void InitGraph(linkGraph &G,const Graph &g,temp &t){
- preInitGraph(G,g);
- for(int i=0;i
- int a,b;
- a=t.docu[i].row;b=t.docu[i].col;
- ArcNode *p=new ArcNode();
- p->nextarc=NULL;
- p->adj=b;
- ArcNode *q=G.vexes[a].firstarc;
- if(G.vexes[a].firstarc==NULL)
- G.vexes[a].firstarc=p;
- else{
- while(q->nextarc!=NULL){
- q=q->nextarc;
- }
- q->nextarc=p;
- }
- }
- }
-
- //输出领接表存储的图
- void PrintGraph(const linkGraph &G){
- for(int i=0;i
- cout<
- ArcNode *p=G.vexes[i].firstarc;
- cout<
- while(p!=NULL){
- cout<<" --> "<
adj; - p=p->nextarc;
- }
- cout<
- }
- }
-
- int check(linkGraph LG,int a[],int indegree[]){
- //PrintGraph(LG);
- int temp[LG.vexnumber]={0};
- for(int i=0;i
- for(int i=0;i
- if(temp[a[i]]!=0) return 1;
- for(ArcNode *p=LG.vexes[a[i]].firstarc;p!=nullptr;p=p->nextarc){
- temp[p->adj]--;
- }
- }
- return 0;
- }
-
-
- int main(){
- //freopen("/config/workspace/test/test","r",stdin);
- int n,m;
- cin>>n>>m;
-
- Graph G;
- G.vexNumber=n;
- temp t;
- t.num=m;
- t.docu=new tempNode[m];
- for(int i=0;i
- int a,b;
- cin>>a>>b;
- t.docu[i].row=a;
- t.docu[i].col=b;
- }
- linkGraph LG;
- InitGraph(LG,G,t);
- int indegree[LG.vexnumber]={0};
- //for(int i=0;i
- for(int i=0;i
- for(ArcNode *p=LG.vexes[i].firstarc;p!=nullptr;p=p->nextarc)
- indegree[p->adj]++;
- }//入度
- int k;
- cin>>k;
- for(int i=0;i
- int a[n];
- for(int j=0;j
- cin>>a[j];
- }
- int flag=check(LG,a,indegree);
- if(flag) cout<<"NO"<
- else cout<<"YES"<
- }
-
-
- return 0;
- }
//闲叙题外话:这周事情好多啊。
-
相关阅读:
Navicat Premium操作Mongodb(基本的增删改查)
【HMS core】【FAQ】Account Kit、push Kit典型问题合集1
Mybatis-Plus-【通俗易懂全讲解】
中小商业银行主动安全纵深防御体系解决方案
基于肤色模型(YCbCr模型)的人面定位统计算法,Matlab实现
埃菲尔之月手表口红礼盒情迷法式浪漫
opensips使用drouting进行路由
[附源码]计算机毕业设计JAVAjsp在线视频网站
【ARK UI】HarmonyOS 从相册选择图片并显示到Image组件上
EWM 过账期间修改(Posting only possible in periods***)
-
原文地址:https://blog.csdn.net/weixin_65908362/article/details/127989923
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU