HDU——T 3342 Legal or Not
http://acm.hdu.edu.cn/showproblem.php?pid=3342
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9122 Accepted Submission(s): 4230
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 <algorithm>
#include <cstring>
#include <cstdio> using namespace std; const int N(+);
const int M(+);
int head[N],sumedge;
struct Edge
{
int v,next;
Edge(int v=,int next=):v(v),next(next){}
}edge[M];
inline void ins(int u,int v)
{
edge[++sumedge]=Edge(v,head[u]);
head[u]=sumedge;
} int tim,dfn[N],low[N];
int top,Stack[N],instack[N];
int sumcol,col[N],point[N];
void DFS(int now)
{
low[now]=dfn[now]=++tim;
Stack[++top]=now;instack[now]=;
for(int v,i=head[now];i;i=edge[i].next)
{
v=edge[i].v;
if(!dfn[v]) DFS(v),low[now]=min(low[now],low[v]);
else if(instack[v]) low[now]=min(low[now],dfn[v]);
}
if(low[now]==dfn[now])
{
col[now]=++sumcol;
point[sumcol]++;
for(;Stack[top]!=now;top--)
{
col[Stack[top]]=sumcol;
point[sumcol]++;
instack[Stack[top]]=;
}
top--; instack[now]=;
}
} bool jud()
{
for(int i=;i<=sumcol;i++)
if(point[i]>) return false;
return true;
} int AC()
{
for(int n,m;scanf("%d%d",&n,&m)&&n;)
{
for(int u,v;m--;)
scanf("%d%d",&u,&v),ins(u,v);
DFS();
if(jud()) puts("YES");
else puts("NO");
memset(instack,,sizeof(instack));
memset(point,,sizeof(point));
memset(Stack,,sizeof(Stack));
memset(edge,,sizeof(edge));
memset(head,,sizeof(head));
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
memset(col,,sizeof(col));
sumedge=sumcol=tim=top=;
}
return ;
} int I_want_AC=AC();
int main() {;}
在不知廉耻的附上只A掉样例的Tarjan
Top_sort
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue> using namespace std; const int N();
int head[N],sumedge;
struct Edge
{
int v,next;
Edge(int v=,int next=):v(v),next(next){}
}edge[N];
inline void ins(int u,int v)
{
edge[++sumedge]=Edge(v,head[u]);
head[u]=sumedge;
} int AC()
{
for(int ans,n,m,rd[N];scanf("%d%d",&n,&m)&&n;)
{
ans=n;
queue<int>que;sumedge=;
memset(rd,,sizeof(rd));
memset(head,,sizeof(head));
memset(edge,,sizeof(edge));
for(int u,v;m--;)
{
scanf("%d%d",&u,&v);
ins(u+,v+); rd[v+]++;
}
for(int i=;i<=n;i++)
if(!rd[i]) que.push(i);
for(int u,v;!que.empty();)
{
ans--;
u=que.front();que.pop();
for(int i=head[u];i;i=edge[i].next)
{
v=edge[i].v;
if(--rd[v]==) que.push(v);
}
}
if(!ans) puts("YES");
else puts("NO");
}
return ;
}
int I_want_AC=AC(); int main() {;}
HDU——T 3342 Legal or Not的更多相关文章
- 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 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/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 3342 -- Legal or Not【裸拓扑排序 &&水题 && 邻接表实现】
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
随机推荐
- sharding-jdbc,轻量级数据库分库分表中间件
Sharding-JDBC是当当应用框架ddframe中,从关系型数据库模块dd-rdb中分离出来的数据库水平分片框架,实现透明化数据库分库分表访问.Sharding-JDBC是继dubbox和ela ...
- windows py-faster-rcnn配置
[原创帖!转载请注明] windows faster-rcnn安装一直会出现各种错误,本人在实验室电脑成功安装后,在自己电脑上同样的安装步骤也成功.所以做个总结,希望对大家有帮助. 一:安装环境 1. ...
- vue项目input的placeholder根据用户的选择改变
html部分 <el-input :placeholder="holder" v-model="searchKey"> <el-select ...
- 版本号比较[versionCompare]
/*** * 版本号比较 * @param v1 版本号a * @param v2 版本号b * @return -1代表不是合格版本号:0代表一样大.1 代表版本号a大于版本号b.2代表版本号b大于 ...
- Vue组件通信之Bus
关于组件通信我相信小伙伴们肯定也都很熟悉,就不多说了,对组件通信还不熟悉的小伙伴移步这里. 在vue2.0中 $dispatch 和 $broadcast 已经被弃用.官方文档中给出的解释是: 因为基 ...
- Swift编写的一些完整的app
收集了一些实用swift编写的app,这些demo都是不错的值得学习的. 知乎日报 Swift-ZhihuDaily Swift版知乎日报 参照了YANGReal的糗事百科和uitableview的例 ...
- C++ Primer笔记13_运算符重载_总结
总结: 1.不能重载的运算符: . 和 .* 和 ?: 和 :: 和 sizeof 和 typeid 2.重载运算符有两种基本选择: 类的成员函数或者友元函数, 建议规则例如以下: 运算符 建议使用 ...
- oracle树操作(select .. start with .. connect by .. prior)
oracle中的递归查询能够使用:select .. start with .. connect by .. prior 以下将会讲述oracle中树形查询的经常使用方式.仅仅涉及到一张表. star ...
- NSURLConnection和NSRunLoop
主线程中创建一个NSURLConnection并异步运行 [self performSelectorOnMainThread:@selector(start) withObject:nil waitU ...
- CorePlot学习六---点击scatterPlot中的symbol点时弹出对应的凝视
因为项目须要用到用户点击 symbol时,弹出对应的具体信息,发现国内解说的比較少,经过一番搜索验证最终解决,先看效果图: 详细须要改动的代码例如以下: 首先要引用托付方法:CPTScatterPlo ...