一道极其水的拓扑排序……但是我还是要把它发出来,原因很简单,连错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的更多相关文章

  1. HDU.3342 Legal or Not (拓扑排序 TopSort)

    HDU.3342 Legal or Not (拓扑排序 TopSort) 题意分析 裸的拓扑排序 根据是否成环来判断是否合法 详解请移步 算法学习 拓扑排序(TopSort) 代码总览 #includ ...

  2. hdu 3342 Legal or Not(拓扑排序)

    Legal or Not Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

  3. HDU 3342 Legal or Not(有向图判环 拓扑排序)

    Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  4. HDU 3342 Legal or Not (最短路 拓扑排序?)

    Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  5. HDU——3342 Legal or Not

    Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  6. HDU.1285 确定比赛名次 (拓扑排序 TopSort)

    HDU.1285 确定比赛名次 (拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 只不过这道的额外要求是,输出字典序最小的那组解.那么解决方案就是 ...

  7. HDU 3342 Legal or Not(拓扑排序判断成环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目大意:n个点,m条有向边,让你判断是否有环. 解题思路:裸题,用dfs版的拓扑排序直接套用即 ...

  8. HDU 3342 -- Legal or Not【裸拓扑排序 &amp;&amp;水题 &amp;&amp; 邻接表实现】

    Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  9. HDU 3342 Legal or Not (图是否有环)【拓扑排序】

    <题目链接> 题目大意: 给你 0~n-1 这n个点,然后给出m个关系 ,u,v代表u->v的单向边,问你这m个关系中是否产生冲突. 解题分析: 不难发现,题目就是叫我们判断图中是否 ...

随机推荐

  1. PowerDesigner 逆向工程 从SQL文件转换成PDM 从PDM转成CDM

    从SQL文件逆向工程到PDM: ①选择file -> Reverse Engineer - > Database ②在General选项卡中选择MySQL数据库,点击确定. ③using ...

  2. 2410中断中SRCPND和INTPND清零的疑问

    2410中断中SRCPND和INTPND清零的疑问SRCPND是中断源引脚寄存器,某个位被置1表示相应的中断被触发,但我们知道在同一时刻内系统可以触发若干个中断,只要中断被触发了,SRCPND的相应位 ...

  3. MIT算法导论——第一讲.Analysis of algorithm

    本栏目(Algorithms)下MIT算法导论专题是个人对网易公开课MIT算法导论的学习心得与笔记.所有内容均来自MIT公开课Introduction to Algorithms中Charles E. ...

  4. Hibernate逍遥游记-第12章 映射值类型集合-005对集合排序Map(<order-by>\<sort>)

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  5. Android:文件夹显示红色叹号

    有感叹号,说明有的文件损坏或丢失了 解决方法: 右击工程,Build Path..->Configure Build Path...->Java Build Path 可以看到引用的jar ...

  6. java:I/O流

    I/O是input/output的缩写,即输入输出端口. 从 文件.键盘.网络 等输入到java程序,再从java程序输出到 文件.显示器.网络等 分类: 1.输入流 和 输出流2.字节流 和 字符流 ...

  7. Yii2 的问题解决方案

    yii2 Class 'app\controllers\AccessControl' not found 一般是命名空间问题, 写成\yii\filters\AccessControl::classN ...

  8. python获取外网地址

    # coding=gbk import sys,urllib.request,re url = "http://www.3322.org/dyndns/getip" #网页地址 m ...

  9. POJ 3185 The Water Bowls(高斯消元-枚举变元个数)

    题目链接:http://poj.org/problem?id=3185 题意:20盏灯排成一排.操作第i盏灯的时候,i-1和i+1盏灯的状态均会改变.给定初始状态,问最少操作多少盏灯使得所有灯的状态最 ...

  10. Git for windows 中文乱码解决方案

    1.git status时显示乱码,如下: \316\304\261\276\316\304\265\265.txt 解决方案: $ git config --global core.quotepat ...