poj 1093 Sorting It All Out AOV网络+拓扑

简介:

   

      一道拓扑的题,只是每增加一条拓扑一次,要注意是否有人为添加的边,也就是同一时刻有没有多个可选项。

 

      这题少加了个return 0,错了n次……注意细节。

 

       非常简单的题,结果写了70多分钟……

 

/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define INF 1E9
using namespace std;
bool vis[30];
int q[700];
int first[30],in[30],t[30];
int U[700],next[700],cnt;
int m,n;
void add(int v,int u)
{
   in[u]++;
   next[cnt]=first[v];
   first[v]=cnt;
   U[cnt++]=u;
}
char s[30];
int solve(int n)
{
    memset(s,0,sizeof(s));
    int top=-1,i,j,now,pos=0,c=0;
    bool flag=1;
    for(i=0;i<n;i++)//找到初始状态
    {
        t[q[i]]=in[q[i]];
        if(t[q[i]])continue;
        t[q[i]]=top;top=q[i];
        c++;
    }
    for(i=0;i<n;i++)
    {
        if(c>1) flag=0;//如果c>1,代表有人为添加的序列
        else if(c==0)return -1;//存在环
        s[pos++]=top+'A';
        c--;
        now=top;top=t[top];
        for(j=first[now];j!=-1;j=next[j])
          if(--t[U[j]]==0)
          {
              t[U[j]]=top;
              top=U[j];
              c++;
          }
    }
    if(flag) return pos;
    return 0;//少加了这个return 0,错了n次……
}
bool input()
{
    scanf("%d%d",&n,&m);
    if(n==0&&m==0)return 0;
    memset(first,-1,sizeof(first));
    memset(in,0,sizeof(in));
    memset(vis,0,sizeof(vis));
    cnt=0;
    int i,now=0,ans,pos,ff=0;
    char so[5];
    for(i=0;i<m;i++)
    {
       scanf("%s",so);if(ff)continue;
       add(so[0]-'A',so[2]-'A');
       if(vis[so[0]-'A']==0)
           vis[so[0]-'A']=1,q[now++]=so[0]-'A';
       if(vis[so[2]-'A']==0)
           vis[so[2]-'A']=1;q[now++]=so[2]-'A';
       ans=solve(now);//每增加一条一次拓扑
       if(ans==-1){pos=i+1;ff=1;continue;}
       else if(ans==n){ff=1;pos=i+1;continue;}
    }
    if(ans==n) printf("Sorted sequence determined after %d relations: %s.\n",pos,s);
    else if(ans==-1) printf("Inconsistency found after %d relations.\n",pos);
    else printf("Sorted sequence cannot be determined.\n");
    return 1;
}
int main()
{
    while(input());
}


 

目录
相关文章
|
6月前
|
监控
计算机网络的拓扑结构
计算机网络的拓扑结构。
109 0
|
4月前
|
SQL 前端开发 NoSQL
IT社区|网络社区平台|基于SpringBoot的IT社区平台
IT社区|网络社区平台|基于SpringBoot的IT社区平台
|
7月前
|
机器学习/深度学习 传感器 算法
基于Matlab模拟无线网络拓扑、估计链路质量并可视化拓扑
基于Matlab模拟无线网络拓扑、估计链路质量并可视化拓扑
|
7月前
|
存储 安全 网络安全
it网络构建及设备系统配置以及IT安全策略实施如何实现?
it网络构建及设备系统配置以及IT安全策略实施如何实现?
|
11月前
|
运维 网络架构
《企业运维之云上网络原理与实践》——第五章 云上网络互连——配套实验:通过现网的配置分析当前 CEN TR 的组网拓扑(1)
《企业运维之云上网络原理与实践》——第五章 云上网络互连——配套实验:通过现网的配置分析当前 CEN TR 的组网拓扑(1)
93 0
|
11月前
|
运维
《企业运维之云上网络原理与实践》——第五章 云上网络互连——配套实验:通过现网的配置分析当前 CEN TR 的组网拓扑(2)
《企业运维之云上网络原理与实践》——第五章 云上网络互连——配套实验:通过现网的配置分析当前 CEN TR 的组网拓扑(2)
78 0
|
11月前
|
弹性计算 运维
《企业运维之云上网络原理与实践》——第五章 云上网络互连——配套实验:通过现网的配置分析当前 CEN TR 的组网拓扑(3)
《企业运维之云上网络原理与实践》——第五章 云上网络互连——配套实验:通过现网的配置分析当前 CEN TR 的组网拓扑(3)
70 0
|
11月前
|
机器学习/深度学习 人工智能 网络架构
图神经网络的困境,用微分几何和代数拓扑解决
图神经网络的困境,用微分几何和代数拓扑解决
109 0
|
存储 负载均衡 安全
IT知识百科:什么是超融合数据中心网络?
IT知识百科:什么是超融合数据中心网络?
185 0
|
运维 监控 网络协议
如何监控IT正常运行时间,网络正常运行对企业业务至关重要
随着企业的扩展,其IT网络规模也将不断增长。当将大量属于不同类别,由不同供应商制造的设备添加到您的IT基础结构中时,正常运行时间的管理复杂性就急剧上升
110 0
如何监控IT正常运行时间,网络正常运行对企业业务至关重要

热门文章

最新文章