hdu 3342 Legal or Not(拓扑排序) HDOJ Monthly Contest – 2010.03.06
一道极其水的拓扑排序……但是我还是要把它发出来,原因很简单,连错12次……
题意也很裸,前面的废话不用看,直接看输入
输入n, m表示从0到n-1共n个人,有m组关系
截下来m组,每组输入a, b表示a指向b,或者b指向a也行。
输入n == 0时结束
如果可以拓扑排序,输出"YES",否则输出"NO"。每组输出占一行。
给两种代码吧,一种用邻接矩阵,另一种我也不知道叫什么好……
邻接矩阵——
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std; const int N = ; int mp[N][N];
int n, m;
int a, b;
int dg[N]; int main()
{
//freopen("test.txt", "r", stdin);
while(~scanf("%d%d", &n, &m) && n)
{
memset(dg, , sizeof(dg));
memset(mp, , sizeof(mp));
for(int i = ; i < m; i++)
{
scanf("%d%d", &a, &b);
if(mp[a][b] == && a != b)
{
dg[b]++;
mp[a][b] = ;
}
} queue<int> que;
for(int i = ; i < n; i++)
{
if(!dg[i]) que.push(i);
} int sum = ;
while(!que.empty())
{
int p = que.front();
que.pop();
sum++;
dg[p]--;
for(int i = ; i < n; i++)
{
if(mp[p][i])
{
dg[i]--;
if(!dg[i])
{
que.push(i);
}
} }
}
if(sum == n) printf("YES\n");
else printf("NO\n"); }
return ;
}
不知名——
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std; const int N = ; struct Node
{
int to;
int next;
}node[N]; int head[N];
int dg[N];
int n, m;
int a, b; int main()
{
//freopen("test.txt", "r", stdin);
while(~scanf("%d%d", &n, &m) && n)
{
memset(head, -, sizeof(head));
memset(dg, , sizeof(dg));
for(int i = ; i < m; i++)
{
scanf("%d%d", &a, &b);
node[i].to = b;
node[i].next = head[a];
head[a] = i;
dg[b]++;
} int sum = ;
queue<int>que;
for(int i = ; i < n; i++)
{
if(!dg[i])
{
que.push(i);
}
} while(!que.empty())
{
int p = que.front();
que.pop();
dg[p]--;
sum++;
for(int i = head[p]; i != -; i = node[i].next)
{
int v = node[i].to;
dg[v]--;
if(!dg[v])
{
que.push(v);
}
}
}
if(sum == n) printf("YES\n");
else printf("NO\n");
}
return ;
}
知道名字了,叫前向星标……
hdu 3342 Legal or Not(拓扑排序) HDOJ Monthly Contest – 2010.03.06的更多相关文章
- HDU.3342 Legal or Not (拓扑排序 TopSort)
HDU.3342 Legal or Not (拓扑排序 TopSort) 题意分析 裸的拓扑排序 根据是否成环来判断是否合法 详解请移步 算法学习 拓扑排序(TopSort) 代码总览 #includ ...
- hdu 3342 Legal or Not(拓扑排序)
Legal or Not Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- HDU 3342 Legal or Not(有向图判环 拓扑排序)
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 3342 Legal or Not (最短路 拓扑排序?)
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU——3342 Legal or Not
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- HDU.1285 确定比赛名次 (拓扑排序 TopSort)
HDU.1285 确定比赛名次 (拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 只不过这道的额外要求是,输出字典序最小的那组解.那么解决方案就是 ...
- HDU 3342 Legal or Not(拓扑排序判断成环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目大意:n个点,m条有向边,让你判断是否有环. 解题思路:裸题,用dfs版的拓扑排序直接套用即 ...
- HDU 3342 -- Legal or Not【裸拓扑排序 &&水题 && 邻接表实现】
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- HDU 3342 Legal or Not (图是否有环)【拓扑排序】
<题目链接> 题目大意: 给你 0~n-1 这n个点,然后给出m个关系 ,u,v代表u->v的单向边,问你这m个关系中是否产生冲突. 解题分析: 不难发现,题目就是叫我们判断图中是否 ...
随机推荐
- Corner case
A corner case (or pathological case) is a problem or situation that occurs only outside of normal op ...
- POJ 3270 Cow Sorting(置换群)
题目链接 题意 : N头牛,每个牛的坏脾气都有一个值,每个值都不相同,把这个值按照从小到大排序,如果两个值交换,那么会花掉这两个值之和的时间,让你花最少的时间将每个值从小到大排好序,求最小的总时间. ...
- TopCoder 603 div1 & div2
div2 250pts MiddleCode 题意:s串长度为奇数时,将中间字符取掉并添加到t末尾:长度为偶数时,将中间两个较小的字符取掉并添加到末尾. 分析:直接做,学习了一下substr(s, p ...
- 使用getJSON()方法异步加载JSON格式数据
使用getJSON()方法异步加载JSON格式数据 使用getJSON()方法可以通过Ajax异步请求的方式,获取服务器中的数组,并对获取的数据进行解析,显示在页面中,它的调用格式为: jQuery. ...
- 李洪强iOS开发之OC语言@property @synthesize和id
OC语言@property @synthesize和id 一.@property @synthesize关键字 注意:这两个关键字是编译器特性,让xcode可以自动生成getter和setter的声明 ...
- C++:运算符重载函数
5.运算符重载 5.1 在类外定义的运算符重载函数 C++为运算符重载提供了一种方法,即在运行运算符重载时,必须定义一个运算符重载函数,其名字为operator,后随一个要重载的运算符.例如,要重载& ...
- POJ3009——Curling 2.0(DFS)
Curling 2.0 DescriptionOn Planet MM-21, after their Olympic games this year, curling is getting popu ...
- Intellij IDEA 新建一个EJB工程(三)
之前都是用IDEA启动JBoss服务器,并在启动的同时将EJB项目部署上去.在构建 artifacts 时遇到很多问题,明明是EJB项目却不能用EJB导出,真是奇怪~~ 后来用Web Applicat ...
- opencv 图像阴影检测
参数说明: IplImage *workImg-当前全局变量,表示正在显示的图片. downleft, upright- 检测出的阴影部分矩形框的两个对角顶点. /****************** ...
- VB 语言学习笔记.
暂时用到,学习学习. 变量声明 Dim 变量名 as 数据类型类型 Set 实例 = new 类名 自定义数据类型 Type 数据类型标识符 域名 As 数据类型; 域名 As 数据类型; 域名 As ...