Legal or Not HDU
Legal or Not
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3060 Accepted Submission(s): 1386
We all know a master can have many prentices and a prentice may have a lot of masters too, it's legal. Nevertheless,some cows are not so honest, they hold illegal relationship. Take HH and 3xian for instant, HH is 3xian's master and, at the same time, 3xian is HH's master,which is quite illegal! To avoid this,please help us to judge whether their relationship is legal or not.
Please note that the "master and prentice" relation is transitive. It means that if A is B's master ans B is C's master, then A is C's master.
TO MAKE IT SIMPLE, we give every one a number (0, 1, 2,..., N-1). We use their numbers instead of their names.
If it is legal, output "YES", otherwise "NO".
0 1
1 2
2 2
0 1
1 0
0 0
NO
简单版
#include <stdio.h>
#include <string.h> const int MAX = 100 + 10;
int n,m,G[MAX][MAX],c[MAX];
bool DFS(int u)
{
c[u] = -1;
for(int v = 0;v < n;v++)if(G[u][v])
{
if(c[v]<0) return false;
if(!c[v]&&!DFS(v)) return false;
}
c[u] = 1;
return true;
}
bool TopSort()
{
memset(c,0,sizeof(c));
for(int v = 0;v < n;v++)if(!c[v])
if(!DFS(v)) return false;
return true;
}
int main()
{
while(scanf("%d%d",&n,&m),n)
{
int x,y;
memset(G,0,sizeof(G));
for(int i = 0;i < m;i++)
{
scanf("%d%d",&x,&y);
G[x][y] = 1;
}
if(TopSort())
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
复杂版(邻接表)
#include <iostream>
#include <cstdlib>
#include <queue>
using namespace std; const int MaxVertexNum = 100;
typedef int VertexType;
typedef struct node
{
int adjvex;
node* next;
}EdgeNode;
typedef struct
{
VertexType vertex;
EdgeNode* firstedge;
int count;
}VertexNode;
typedef VertexNode AdjList[MaxVertexNum];
typedef struct
{
AdjList adjlist;
int n,e;
}ALGraph;
bool CreatALGraph(ALGraph *G)
{
int i,j,k;
EdgeNode* s;
cin>>G->n>>G->e;
if(G->n == 0)
return false;
for(i = 0;i < G->n;i++)
{
G->adjlist[i].vertex = i;
G->adjlist[i].firstedge = NULL;
}
for(k = 0;k < G->e;k++)
{
cin>>i>>j;
s = (EdgeNode *)malloc(sizeof(EdgeNode));
s->adjvex = j;
s->next = G->adjlist[i].firstedge;
G->adjlist[i].firstedge = s;
}
return true;
} void TopSort(ALGraph *G)
{
EdgeNode *p;
queue<int> Q;
int i,j,cnt = 0;
for(i = 0;i < G->n;i++)
G->adjlist[i].count = 0;
for(i = 0;i < G->n;i++)
{
p = G->adjlist[i].firstedge;
while(p != NULL)
{
G->adjlist[p->adjvex].count++;
p = p->next;
}
}
for(i = 0;i < G->n;i++)
if(G->adjlist[i].count == 0)
Q.push(i);
while(!Q.empty())
{
i = Q.front();
Q.pop();
cnt++;
p = G->adjlist[i].firstedge;
while(p != NULL)
{
j = p->adjvex;
G->adjlist[j].count--;
if(G->adjlist[j].count == 0)
Q.push(j);
p = p->next;
}
}
if(cnt != G->n)
cout<<"NO"<<endl;
else
cout<<"YES"<<endl;
} int main()
{
ALGraph G;
while(CreatALGraph(&G))
TopSort(&G);
return 0;
}
Legal or Not HDU的更多相关文章
- Legal or Not HDU - 3342 (拓扑排序)
注意点: 输入数据中可能有重复,需要进行处理! #include <stdio.h> #include <iostream> #include <cstring> ...
- HDU 3342 Legal or Not(判断是否存在环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Othe ...
- hdu 3342 Legal or Not
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Description ACM-DIY is a large QQ g ...
- HDU.3342 Legal or Not (拓扑排序 TopSort)
HDU.3342 Legal or Not (拓扑排序 TopSort) 题意分析 裸的拓扑排序 根据是否成环来判断是否合法 详解请移步 算法学习 拓扑排序(TopSort) 代码总览 #includ ...
- HDU——T 3342 Legal or Not
http://acm.hdu.edu.cn/showproblem.php?pid=3342 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- 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) Tot ...
- HDU 3342 Legal or Not(拓扑排序判断成环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目大意:n个点,m条有向边,让你判断是否有环. 解题思路:裸题,用dfs版的拓扑排序直接套用即 ...
随机推荐
- hdu 2647 (拓扑排序 邻接表建图的模板) Reward
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2647 老板给员工发工资,每个人的基本工资都是888,然后还有奖金,然后员工之间有矛盾,有的员工希望比某员 ...
- poj 1182 (关系并查集) 食物链
题目传送门:http://poj.org/problem?id=1182 这是一道关系型并查集的题,对于每个动物来说,只有三种情况:同类,吃与被吃: 所以可以用0,1,2三个数字代表三种情况,在使用并 ...
- error C4996: 'GetVersionExW': 被声明为已否决
1.Project Properties > Configuration Properties > C/C++ > General > SDL checks关掉 其他方法:2. ...
- serial -1
#include <reg52.h>#include <stdio.h>#define uchar unsigned charsbit LED = P2^2;uchar rec ...
- jquery全选反选
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Jquery中$.each()与$().each()的使用与区别。
在jquery中,遍历对象和数组,经常会用到$().each和$.each(),两个方法.两个方法是有区别的,从而这两个方法在针对不同的操作上,显示了各自的特点. $().each,对于这个方法,在d ...
- jmeter脚本录制的两种方式
完成一次完整的性能测试: 1.创建用户: 2.选择协议(HTTP) 3.使用工具去模拟协议操作(1.手工编写(抓包工具):2.工具自带录制) 4.运行工具进行压力测试
- Django 创建一个应用程序
1. 认识Django Django是一个高级的Python Web框架,它鼓励快速开发和清洁,务实的设计. 由经验丰富的开发人员构建,它负责Web开发的许多麻烦,因此您可以专注于编写应用程序,而无需 ...
- canvas 实现掉落效果
var canvas = document.getElementById('canvas'); var cxt = canvas.getContext('2d'); cxt.strokeStyle = ...
- 对SVC和SVR的理解
首先: support vector classify(SVC)支持分类机做二分类的,找出分类面,解决分类问题 support vector regression(SCR)支持回归机做曲线拟合.函数回 ...