POJ
来源:互联网 发布:java工程师培训学费 编辑:程序博客网 时间:2024/06/07 01:30
题解:如果a
#include<iostream>#include<cstring>#include<cstdio>#include<cstring>#include<queue>using namespace std;#define x b-'A'#define y c-'A'int a[30];int n,m;int g[30][30];int in[30];int sum[30];int vis[30];queue<int>q;int so1 = 0,so2 = 0;int solve(char b,char c){ if(!g[x][y]){ g[x][y] = 1; in[y]++; } memset(sum,0,sizeof(sum)); int cnt = 0,ok = 1; for(int i = 0; i < n; i++) if(sum[i]==in[i]){ q.push(i); cnt++; a[i] = 1; } while(!q.empty()){ int u = q.front(); q.pop(); if(!q.empty()) ok = 0; // cout<<u<<endl;; for(int v = 0; v < n; v++) if(g[u][v]){ sum[v]++; if(sum[v]==in[v]){ a[v] = a[u]+1; q.push(v); cnt++; } } } //cout<<cnt<<endl; if(cnt!=n) return 2; if(ok) return 1; return 0;}int main(){ char s[30]; char str[30]; while(scanf("%d%d",&n,&m)&&n+m){ for(int i = 0; i < n; i++) a[i] = 1; so1 = 0,so2 = 0; memset(g,0,sizeof(g)); memset(in,0,sizeof(in)); int ans; for(int i = 1; i <= m; i++){ scanf("%s",s); if(so1||so2) continue; if(a[s[0]-'A']<a[s[0]-'A']) continue; if(s[1]=='<') ans = solve(s[0],s[2]); else ans = solve(s[2],s[0]); if(ans==1&&!so1) so1 = i; if(ans == 2&&!so2) so2 = i; } if(so2) printf("Inconsistency found after %d relations.\n",so2); else if(so1){ printf("Sorted sequence determined after %d relations: ",so1); for(int i = 0; i < n; i++) str[a[i]] = 'A'+i; for(int i = 1; i <= n; i++) putchar(str[i]); puts("."); } else printf("Sorted sequence cannot be determined.\n"); } return 0;}
阅读全文
0 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- Python语法基础12.break和continue
- stm32学习笔记(九)PWM输出
- WEB前端-HF-HTML5Programming-笔记-CH4 JS对象与函数
- Java实训第十四天8/14
- 11.Service的生命周期
- POJ
- 网站开发(五)分离后台公共页面
- MYSQL之MHA实现VIP切换用到脚本
- MYSQL之MHA实现VIP故障切换使用脚本(可用)
- Scala 中 10 个超赞的集合操作函数
- 练习 2017-08-14 合并流
- 编写windbg调试器扩展 进阶篇1
- php函数file_get_contents(一)
- 利用JQuery实现简单的购物车结算