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