题目描述

Once upon a time, there arose a huge discussion among the dwarves in Dwarfland. The government wanted to introduce an identity card for all inhabitants.
Most dwarves accept to be small, but they do not like to be measured. Therefore, the government allowed them to substitute the field “height” in their personal identity card with a field “relative dwarf size”. For producing the ID cards, the dwarves were being interviewed about their relative
sizes. For some reason, the government suspects that at least one of the interviewed dwarves must have lied.
Can you help find out if the provided information proves the existence of at least one lying dwarf?

输入

The input consists of:
• one line with an integer n (1 ≤ n ≤ 105 ), where n is the number of statements;
• n lines describing the relations between the dwarves. Each relation is described by:
– one line with “s 1 < s 2 ” or “s 1 > s 2 ”, telling whether dwarf s 1 is smaller or taller than dwarf s 2 . s 1 and s 2 are two different dwarf names.
A dwarf name consists of at most 20 letters from “A” to “Z” and “a” to “z”. A dwarf name does not contain spaces. The number of dwarves does not exceed 104 .

输出

Output “impossible” if the statements are not consistent, otherwise output “possible”.

样例输入

3
Dori > Balin
Balin > Kili
Dori < Kili

样例输出

impossible

题意: 给定 字符串代表的名字 ,例如 s1,s2  如果输入为s1 > s2 代表s1比s2高,给定n个这样的大小关系比较式,

如果都满足逻辑关系,就输出possible,否则就输出impossible

分析:对于所给定的关系中,可以构成有向图,对这个有向图进行拓扑排序,如果成环就代表又不符合逻辑的关系式

如果完成了拓扑排序,就是代表无逻辑冲突,然后就是名字的量化表示

分析:

#include<iostream>
#include<stdio.h>
#include<vector>
#include<string.h>
#include<map>
using namespace std;
#define maxn 100010
int n,c[maxn],h[maxn];
vector<int> e[maxn];
char bb[2];
string aa,cc;
map<string,int> flag;
int countt=1;
int findd(string x)
{if(flag[x]!=0)return flag[x];flag[x]=countt++; return flag[x];
}
bool dfs(int u){c[u]=-1;for(int i=0;i<e[u].size();i++){int v=e[u][i];if(c[v]<0) return false;else if(!c[v]&&!dfs(v)) return false;}c[u]=1;return true;
}
bool toposort(){for(int u=1;u<=countt;u++)if(!c[u])if(!dfs(u)) return false;return true;
}
int main(){int u,v;scanf("%d",&n);flag.clear();memset(h,-1,sizeof(h));for(int i=0;i<n;i++){cin >> aa >> bb >>cc;u=findd(aa);v=findd(cc);if(bb[0]=='<') e[u].push_back(v);else           e[v].push_back(u);}if(toposort()) printf("possible\n");else           printf("impossible\n");
}

Dwarves(拓扑排序+字符串使用map量化表示)相关推荐

  1. 剑指 Offer II 114. 外星文字典(困难 图 bfs 哈希表 拓扑排序 字符串 数组)

    剑指 Offer II 114. 外星文字典 现有一种使用英语字母的外星文语言,这门语言的字母顺序与英语顺序不同. 给定一个字符串列表 words ,作为这门语言的词典,words 中的字符串已经 按 ...

  2. 【十二省联考2019】字符串问题【后缀自动机】【拓扑排序】

    题意:给一个字符串 SSS,以子串的形式给出一些 A 类串和 B 类串以及 mmm 对 A 类串支配 B 类串的关系.求一个总长度最长的 A 类串序列,使得每个串都存在一个 B 类串前缀被后一个串支配 ...

  3. HihoCoder - 1174 拓扑排序·一

    由于今天上课的老师讲的特别无聊,小Hi和小Ho偷偷地聊了起来. 小Ho:小Hi,你这学期有选什么课么? 小Hi:挺多的,比如XXX1,XXX2还有XXX3.本来想选YYY2的,但是好像没有先选过YYY ...

  4. 中石油训练赛 - Swapping Places(字典序最小的拓扑排序)

    题目链接:点击查看 题目大意:给出 s 个字符串表示种类,再给出 m 个朋友关系,表示两个种类的动物是朋友,现在给出一个长度为 n 的种类排列,规定相邻两个是朋友的种类的动物可以交换位置,问如何操作, ...

  5. Wannafly挑战赛22 B 字符路径 ( 拓扑排序+dp )

    链接:https://ac.nowcoder.com/acm/contest/160/B 来源:牛客网 题目描述 给一个含n个点m条边的有向无环图(允许重边,点用1到n的整数表示),每条边上有一个字符 ...

  6. ACM: hihicoder #1174 : 拓扑排序·一 STL- queue

    #1174 : 拓扑排序·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 由于今天上课的老师讲的特别无聊,小Hi和小Ho偷偷地聊了起来. 小Ho:小Hi,你这学期有选 ...

  7. 有向图的拓扑排序算法JAVA实现

    一,问题描述 给定一个有向图G=(V,E),将之进行拓扑排序,如果图有环,则提示异常. 要想实现图的算法,如拓扑排序.最短路径--并运行看输出结果,首先就得构造一个图.由于构造图的方式有很多种,这里假 ...

  8. 拓扑排序 Codeforces Round #290 (Div. 2) C. Fox And Names

    题目传送门 1 /* 2 给出n个字符串,求是否有一个"字典序"使得n个字符串是从小到大排序 3 拓扑排序 4 详细解释:http://www.2cto.com/kf/201502 ...

  9. leetcode阶段总结——拓扑排序

    leetcode阶段总结--拓扑排序 leetcode中经常出现的题型之一.其中,拓扑排序的概念可以参考这里,这里主要总结一下前300题中出现的几个关于拓扑排序的题,以待之后复习的时候查找. leet ...

最新文章

  1. Hibernate中的数据库方言(Dialect)
  2. taro 缺点_Taro小程序富文本解析4种方法
  3. ERROR 3009 (HY000): Column count of mysql.user is wrong. Expected 45, found 42. Created with MySQL 5
  4. WebService生成客户端代理的工具WSDL参数介绍
  5. BloomFilter ——大规模数据处理利器
  6. eclipse修改java编译的版本
  7. POJ 1002 电话号码字符串处理
  8. 冯诺依曼计算机流程图,基本流程图综述
  9. java privatekey输出字符串_[Java教程]根据字符串(String)生成公钥(PublicKey)和私钥(PrivateKey)对象_星空网...
  10. 关于VMware: vmw_ioctl_command error Invalid argument.解决办法
  11. ShardingSphere分库分表核心原理精讲第十一节 分布式事务详解
  12. 美国计算机有读一年的学校吗,美国计算机排名多少的学校值得去读?相关院校资讯!...
  13. 关于 百度飞浆paddleOCR编译32位版本 的解决方案
  14. Android log抓取工具
  15. 教育邮箱白嫖office365
  16. 美图秀秀插件上传若干问题
  17. 十二省联考2019酱油记
  18. The disadvantages of an elite education精英教育的弊端
  19. samba服务及vsftp服务及nfs服务简单配置
  20. Shimeji开源桌宠代码学习(2)

热门文章

  1. 苹果内购IAP记录-2 StoreKit新版
  2. 分享 Python 教学视频,从基础到爬虫、网页、数据分析、机器学习.....
  3. hin2vec运行笔记代码导图笔记
  4. PHP学习-3 端口开放
  5. AMPL IDE语法整理
  6. Zookeeper出现Error contacting service. It is probably not running问题
  7. 重塑矩阵(一个矩阵转化成另一个矩阵)
  8. Oracle bpm实现oa,Oracle BPM/SOA API 操作流程
  9. 解决SAP业务问题的思考——逆向思维
  10. 5毛VS600亿,食品安全问题是卫龙上市最大的拦路虎?