Dwarves (有向图判环)
Dwarves
时间限制: 1 Sec 内存限制: 64 MB
提交: 14 解决: 4
[提交][状态][讨论版]
题目描述
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?
输入
• 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 .
输出
样例输入
3
Dori > Balin
Balin > Kili
Dori < Kili
样例输出
impossible
【分析】有向图判环问题。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <queue>
#include <vector>
#define inf 0x7fffffff
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = ;
const int M = ;
int n,m,cnt=;
int vis[N];
vector<int>vec[N];
bool flag=false;
map<string,int>p;
void dfs(int x) {
vis[x]=-;
if(flag)return;
for(int i=; i<vec[x].size(); i++) {
int v=vec[x][i];
if(vis[v]==-) {
flag=true;
return;
}
else if(!vis[v])dfs(v);
}
vis[x]=;
}
int main() {
string a,b,c;
scanf("%d\n",&n);
while(n--) {
cin>>a>>b>>c;
if(!p[a])p[a]=++cnt;
if(!p[c])p[c]=++cnt;
if(b[]=='>')vec[p[a]].push_back(p[c]);
else vec[p[c]].push_back(p[a]);
}
for(int i=; i<=cnt; i++) {
if(flag)break;
if(!vis[i])dfs(i);
}
if(flag)puts("impossible");
else puts("possible");
return ;
}
Dwarves (有向图判环)的更多相关文章
- COJ 3012 LZJ的问题 (有向图判环)
传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=1042 试题描述: LZJ有一个问题想问问大家.他在写函数时有时候很头疼,如 ...
- HDU 3342 Legal or Not(有向图判环 拓扑排序)
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 5154 Harry and Magical Computer 有向图判环
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5154 题解: 有向图判环. 1.用dfs,正在访问的节点标记为-1,已经访问过的节点标记为1,没有访 ...
- CodeForces 937D 936B Sleepy Game 有向图判环,拆点,DFS
题意: 一种游戏,2个人轮流控制棋子在一块有向图上移动,每次移动一条边,不能移动的人为输,无限循环则为平局,棋子初始位置为$S$ 现在有一个人可以同时控制两个玩家,问是否能使得第一个人必胜,并输出一个 ...
- POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39602 Accepted: 13 ...
- Codeforces Round #460 (Div. 2): D. Substring(DAG+DP+判环)
D. Substring time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...
- CodeForces-1217D (拓扑排序/dfs 判环)
题意 https://vjudge.net/problem/CodeForces-1217D 请给一个有向图着色,使得没有一个环只有一个颜色,您需要最小化使用颜色的数量. 思路 因为是有向图,每个环两 ...
- hdu4975 A simple Gaussian elimination problem.(正确解法 最大流+删边判环)(Updated 2014-10-16)
这题标程是错的,网上很多题解也是错的. http://acm.hdu.edu.cn/showproblem.php?pid=4975 2014 Multi-University Training Co ...
- hdu4888 Redraw Beautiful Drawings 最大流+判环
hdu4888 Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/6553 ...
随机推荐
- 戴文的Linux内核专题:01介绍
转自Linux中国 译者按:本文作者戴文.科利尔.约翰逊(Devyn Collier Johnson)今年才19岁,但是他在Linux内核.人工智能.编程语言方面拥有丰富的经验,本文是其在linux. ...
- git初识
下载网上的开发员的项目 git clone https://github.com/xxxxxx/test.git 下载好,进入刚下载的好的test项目目录,可以修改对应的代码,然后 git statu ...
- HDU 1045 - Fire Net (最大独立集)
题意:给你一个正方形棋盘.每个棋子可以直线攻击,除非隔着石头.现在要求所有棋子都不互相攻击,问最多可以放多少个棋子. 这个题可以用搜索来做.每个棋子考虑放与不放两种情况,然后再判断是否能互相攻击来剪枝 ...
- Stm32_调试出现 Error:Flash Download Failed-"Cortex-M3"
rror:Flash Download Failed-"Cortex-M3"出现一般有两种情况: 1.SWD模式下,Debug菜单中,Reset菜单选项(Autodetect/HW ...
- copy和assign的使用和区别
1.使用copy和assign都可以进行修饰属性或者变量. 2.区别: (1)copy的使用:使用这个进行修饰的属性,当已经进行初始化之后,就无法再改变属性的数据. 如: @property (cop ...
- 深入学习:Windows下Git入门教程(下)
声明:由于本人对于Git的学习还处于摸索阶段,对有些概念的理解或许只是我断章取义,有曲解误导的地方还请见谅指正! 一.分支 1.1分支的概念. 对于的分支的理解,我们可以用模块化这个词来解释:在日常工 ...
- Redis - 密码配置和主从复制
使用config set命令修改requirepass参数配置Redis密码config set requirepass password 也可以通过配置文件修改密码,重启后生效. 克隆虚拟机,分别运 ...
- 第一个Shader的更新,增加爆光度, 属性改为数值型(更直观,精确)
Shader "Castle/ColorMix" { Properties { // 基本贴图 _MainTex ("Texture Image", 2D) = ...
- 搬家后Magento前台只有产品的缩略图不显示
第一种可能:缓存不足 http://blog.csdn.net/ddjohn/article/details/6648199 最近发现一个怪异的现象,Magento前台只有产品的缩略图不显示.我检查了 ...
- magento的robots文件编写和判断是否是一个导航分类页面
magento是网店系统,我们突出的是我们的产品,所以,有很多路径我们不想让搜索引擎索引到,所以我们需要用robots文件进行限制 下面是麦神magento的robots.txt里面的内容,因为很多u ...