一道极其水的拓扑排序……但是我还是要把它发出来,原因很简单,连错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. gcd 最大公约数 模版!

    1: #define ll long long ll gcd(ll a,ll b) { ) { return b; }else { return gcd(b % a,a); } } 2: int64 ...

  2. C#的cs文件中Response.Clear();Response.ClearHeaders()的作用

    在学习一个CS文件,如下:public partial class GetPic : System.Web.UI.Page{    protected void Page_Load(object se ...

  3. Project Euler 95:Amicable chains 亲和数链

    Amicable chains The proper divisors of a number are all the divisors excluding the number itself. Fo ...

  4. 格林治时间,也就是返回从 UTC 1970 年 1 月 1 日午夜开始经过的毫秒数。

    格林治时间,也就是返回从 UTC 1970 年 1 月 1 日午夜开始经过的毫秒数. (* Delphi获取13位格林治时间实现方法, 与java中的java.lang.System.currentT ...

  5. 41. First Missing Positive

    题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...

  6. HDU 4358 Boring counting 树状数组+思路

    研究了整整一天orz……直接上官方题解神思路 #include <cstdio> #include <cstring> #include <cstdlib> #in ...

  7. NYOJ-253 凸包

    LK的旅行 时间限制:2000 ms  |  内存限制:65535 KB 难度:5   描述 LK最近要去某几个地方旅行,她从地图上计划了几个点,并且用笔点了出来,准备在五一假期去这几个城市旅行.现在 ...

  8. HDU 4675 GCD of Sequence(容斥)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4675 题意:给出n,m,K,一个长度为n的数列A(1<=A[i]<=m).对于d(1< ...

  9. 【设计模式】—— 单例模式Singleton

    前言:[模式总览]——————————by xingoo 模式意图 保证类仅有一个实例,并且可以供应用程序全局使用.为了保证这一点,就需要这个类自己创建自己的对象,并且对外有公开的调用方法. 模式结构 ...

  10. 方法Equals和操作符==的区别

    http://www.codeproject.com/Articles/584128/What-is-the-difference-between-equalsequals-and-Eq When w ...