题意:有一张N点M边的有向图,求最小的K使根据前K条边就能够确定图是否有唯一的拓扑序,

若没有唯一拓扑序输出-1

思路:二分答案再拓扑排序,以入度为0的节点作为新的一层,若某一层的节点个数<>1则没有唯一拓扑序

 var head,vet,next,d,x,y,b:array[..]of longint;
n,m,i,l,r,mid,last,tot:longint; procedure add(a,b:longint);
begin
inc(tot);
next[tot]:=head[a];
vet[tot]:=b;
head[a]:=tot;
end; function isok(len:longint):boolean;
var s,e,i,st,v:longint;
begin
fillchar(head,sizeof(head),);
fillchar(b,sizeof(b),);
fillchar(d,sizeof(d),);
tot:=;
for i:= to len do
begin
add(x[i],y[i]);
inc(d[y[i]]);
end;
s:=;
for i:= to n do
if d[i]= then begin st:=i; b[i]:=; inc(s); end;
if s<> then exit(false);
for i:= to n- do
begin
e:=head[st];
while e<> do
begin
v:=vet[e];
dec(d[v]);
e:=next[e];
end;
e:=head[st]; s:=;
while e<> do
begin
v:=vet[e];
if (d[v]=)and(b[v]=) then
begin
inc(s); b[v]:=; st:=v;
end;
e:=next[e];
end;
if s<> then exit(false);
end;
exit(true);
end; begin
// assign(input,'cf645D.in'); reset(input);
// assign(output,'cf645D.out'); rewrite(output);
readln(n,m);
for i:= to m do readln(x[i],y[i]);
l:=; r:=m; last:=m+;
while l<=r do
begin
mid:=(l+r)>>;
if isok(mid) then begin last:=mid; r:=mid-; end
else l:=mid+;
end;
if last=m+ then writeln(-)
else writeln(last);
//close(input);
// close(output);
end.

最新文章

  1. CodeForces 520B Two Buttons(用BFS)
  2. 分支合并git checkout adview git merge adview3
  3. Web 项目下载图片简单处理方式
  4. if分支练习
  5. 转战farbox
  6. 【jQuery基础学习】12 jQuery学习感想
  7. 【干货分享】Node.js 中文资料导航
  8. Effective Java 阅读笔记——枚举和注解
  9. Redis 配置文件 redis.conf 项目详解
  10. enumerate
  11. html5 video.js 使用及兼容所有浏览器
  12. JNI 从C文件向Java文件传递多个参数
  13. DotNet中的计时器线程计时器
  14. oracle 如何搜索当前用户下所有表里含某个值的字段?(转)
  15. WCF学习心得
  16. hdu_5761_Rower Bo(xjb猜公式)
  17. lucene全文搜索之四:创建索引搜索器、6种文档搜索器实现以及搜索结果分析(结合IKAnalyzer分词器的搜索器)基于lucene5.5.3
  18. 【JS】 Javascript与HTML DOM的互动 寻路
  19. Linux操作系统进程模型分析进程
  20. oh forever love~

热门文章

  1. 【点分治】luoguP2664 树上游戏
  2. JWT的使用流程
  3. 在Linux系统中重现黑客帝国经典画面
  4. Vim如何显示和关闭行号
  5. windows 2008r2+php5.6.28环境搭建详细过程
  6. 【js】window.onscroll 无效问题
  7. 用Python设置matplotlib.plot的坐标轴刻度间隔以及刻度范围
  8. bin、hex、elf、axf文件的区别
  9. init_bootmem_node
  10. LeetCode(123) Best Time to Buy and Sell Stock III