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个关系中是否产生冲突. 解题分析: 不难发现,题目就是叫我们判断图中是否 ...
随机推荐
- POJ2104 K-th Number Range Tree
又是区间第k大,这次选择这道题是为以后写线段树套平衡树铺路的.Range Tree可以理解成线段树套vector吧,相当于每个结点多存了对应区间的一个排好序的序列.画一下就会知道空间的消耗是nlogn ...
- POJ 1804
/* *由此题发现规律 就是冒泡排序的交换次数等于这个数列的逆序对 的对数! */ #include<iostream> #include<stdio.h> #include& ...
- shop++ 安装
1.安装tomcat后 ,G:\apache-tomcat-6.0.35\conf\server.xml 中设置tomcat 编码为utf-8. 增加URIEncoding = "UTF-8 ...
- DIV+CSS列表式布局(同意图片的应用)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- windows下eclipse远程连接hadoop错误“Exception in thread"main"java.io.IOException: Call to Master.Hadoop/172.20.145.22:9000 failed ”
在VMware虚拟机下搭建了hadoop集群,ubuntu-12.04,一台master,三台slave.hadoop-0.20.2版本.在 master机器上利用eclipse-3.3连接hadoo ...
- moto xt800 刷机到2.2.2
老机器啊,原来2.1的系统大多数软件都不能装sbf刷机包+工具+教程下载地址:http://u.115.com/file/bhdlwl2x 刷完之后如果RSD Lite显示刷机结果为失败,不要担心,手 ...
- java里int和Integer什么区别
Integer i=0; i是一个对象 int i=3; i是一个基础变量 Integer i=0; 这种写法如果没记错,在JAVA1.5之前是会报错的,自动的加解包是1.5的新特性 必须写成 Int ...
- eclipse怎么集成配置JDK和Tomcat
eclipse怎么集成配置JDK和Tomcat_电脑软件_百度经验http://jingyan.baidu.com/album/db55b609ab6a1f4ba30a2f2c.html?picind ...
- ossec 常用命令及目录说明
1. /var/www/html/analogi -> ossec 第三方的web界面的安装目录 [root@ossec-server ~]# cd /var/www/html/analogi/ ...
- linux内核链表分析
一.常用的链表和内核链表的区别 1.1 常规链表结构 通常链表数据结构至少应包含两个域:数据域和指针域,数据域用于存储数据,指针域用于建立与下一个节点的联系.按照指针域的组织以及各个节 ...