Lightoj 1003 - Drunk(拓扑排序判断是否有环 Map离散化)
2024-10-12 18:22:13
题目链接:http://lightoj.com/volume_showproblem.php?problem=1003
题意是有m个关系格式是a b;表示想要和b必须喝a,问一个人是否喝醉就看一个人是否能把所有种类的饮料喝完,能输出Yes,不能输出No;
其实就是有向图判断是否存在环,可以用拓扑排序来求
拓扑排序:每次找到一个入度为0的点加入队列,删除与之相连的边,循环操作,得到的序列就是拓扑序(前面的必须出现在后面的前面);
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<stack>
#include<map>
using namespace std;
#define N 21010
#define INF 0x3f3f3f3f
#define met(a, b) memset(a, b, sizeof(a)) vector<vector<int> >G;///有向图;
int cnt, du[N];///cnt表示共有多少种饮料,du[i]表示map离散化后的饮料编号i的入度; int topo()
{
queue<int>Q; for(int i=; i<=cnt; i++)
{
if(du[i] == )
Q.push(i);
}
int num = ;///已得到的拓扑序序列中的个数;
while(Q.size())
{
num++;
int u = Q.front(); Q.pop();
int len = G[u].size(), v;
for(int i=; i<len; i++)
{
v = G[u][i];
du[v] --;
if(du[v] == )
Q.push(v);
}
}
if(num == cnt)///如果得到的和总的相等则不存在环,否则存在;
return ;
return ;
} int main()
{
int T, n, t = ; scanf("%d", &T); while(T--)
{
scanf("%d", &n); G.clear();
G.resize(n*+); cnt = ;
met(du, ); char s1[], s2[]; map<string, int> M;
M.clear(); for(int i=; i<=n; i++)
{
scanf("%s %s", s1, s2); if(M[s1] == ) M[s1] = ++cnt;
if(M[s2] == ) M[s2] = ++cnt; G[M[s1]].push_back(M[s2]); du[M[s2]] ++;
} if( topo() )
printf("Case %d: Yes\n", t++);
else
printf("Case %d: No\n", t++);
}
return ;
}
最新文章
- jmeter接口测试
- django权限控制
- CSS简单布局总结
- 将Eclipse代码导入到AndroidStudio的两种方式
- 学写了一段LINQ
- 改变edittext边框颜色
- Matlab绘制透明平面(二元函数)
- 基于Theano的DNN框架Blocks使用简要总结
- 2016.7.13final 修饰符使用
- 重新开始学习javase_多态(动态绑定、推迟绑定或者运行期绑定)
- Loadrunner脚本录制注意事项(七)
- python的while嵌套 99乘法表 三角形和正方形
- 团队作业2——需求分析&;原型设计
- java分割字符串用法
- mysql左连接右连接(查询两张表不同的数据)
- [HDU6146]Pok&#233;mon GO
- Ajax-创建ajax
- Python SSH爆破以及Python3线程池控制线程数
- jquery更改表格行顺序实例
- selenium之数据驱动框架应用WPS个人中心自动签到
热门文章
- devstack install attributeError: &#39;module&#39; object has no attribute &#39;__version__&#39;
- 【Java面试题】51 什么时候用assert。
- 经常使用的CSS Hack技术集锦
- listView解决滑动时黑色背景问题
- EHcache经典配置
- 在properties.xml中定义变量,在application.xml中取值问题
- 【RF库Collections测试】combine lists
- PHP防止跨站表单提交与同站跨页伪造表单的攻击
- redis学习之集群报错Node is not empty
- Computer Science Theory for the Information Age-5: 学习理论——VC维的定义以及一些例子