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 ...
随机推荐
- c# 多线程创建 ---简单
Thread t = new Thread(new ParameterizedThreadStart(UploadCard)); t.IsBackground = false;//后台线程 前台线程 ...
- 测试V模型
一:测试V模型 RAD(Rap Application Development 快速引用开发)模型是软件开发过程中的一个重要模型,由于模型构图形似字母V,所以又称软件开发的V模型.他通过开发和测试同时 ...
- web安全测试-AppScan使用分享
这里主要分享如何使用AppScan对一大项目的部分功能进行安全扫描. ----------------------------------------------------------------- ...
- 迭代器(Iterator)模式
转自:http://blog.csdn.net/lilu_leo/article/details/7609496 概述 迭代器(Iterator)模式,又叫做游标(Cursor)模式.GOF ...
- namenode 无法启动之每次开机需要重新格式化-tmp
最近遇到了一个问题,执行start-all.sh的时候发现JPS一下namenode没有启动 每次开机都得重新格式化一下namenode才可以 其实问题就出在tmp文件,默 ...
- JDBC体会
1.把mysql-connector XXXX版本导入buildpath 2.通过DriverManager 的getConnection 方法获得一个Connnection引用,方法的参数是 url ...
- Javascript之clipBoard操作
1.clipBoard 是网页上剪贴板,可以获取剪切板上值,可能类似物键-值对这种模式取值\赋值,如果在copy网上的某篇博客时,往往会带有“转载自xxxx地方 http://www.xxx.com/ ...
- hdu4417 划分树+二分
//Accepted 14796 KB 453 ms //划分树 //把查询的次数m打成n,也是醉了一晚上!!! //二分l--r区间第k大的数和h比较 #include <cstdio> ...
- maven学习之一本地(windows)安装
step1.检查jdk安装: maven的运行需要jdk的支持,至少是jdk1.4,按如下步骤: 开始>cmd>java -version,如成功出现jdk信息,如下图,则表示已安装jdk ...
- pyMagic:用python控制的Geek入门神器
大学四年快要结束了,这几年也学习了一点新的姿势.最近一直在跟国外的micropython项目,这个项目是由剑桥大学的理论物理学家(theoretical physicist)Damien George ...