2021.07.17 题解 CF1385E Directing Edges(拓扑排序)

CF1385E Directing Edges - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

题意:

给定一个由有向边与无向边组成的图,现在需要你把所有的无向边变成有向边,使得形成的图中没有环

如果可以做到请输出该图,否则直接输出"NO"。

注意多组询问

分析:

对于一个有向无环图,一条边,拓扑序小的一端指向拓扑序大的一端,因此确定无向边的方向。

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
const int N=2e5+10;
int n,m,t,xh[N],cnt,head[N],ru[N];
struct node{
int to,next;
}a[N];
struct nodei{
int from,to;
}ai[N];
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')w=-1;
ch=getchar();
}
while(ch<='9'&&ch>='0'){
s=s*10+ch-'0';
ch=getchar();
}
return s*w;
}
void add(int u,int v){
++cnt;
a[cnt].to=v;
a[cnt].next=head[u];
head[u]=cnt;
}
int topo(){
queue<int>q;
int ind=0;
for(int i=1;i<=n;i++)if(!ru[i])q.push(i);
while(!q.empty()){
int x=q.front();
q.pop();
xh[x]=++ind;
for(int i=head[x];i;i=a[i].next){
int v=a[i].to;
--ru[v];
if(!ru[v])q.push(v);
}
}
if(ind==n)return 1;//如果序号不等于n,有向边中一定有环啊,pass!
else return 0;
}
int main(){
t=read();
while(t--){
memset(head,0,sizeof(head));
//memset(&a,0,sizeof(a));
memset(xh,0x3f,sizeof(xh));
memset(ru,0,sizeof(ru));
cnt=0;
int tot=0;
n=read();m=read();
for(int i=1;i<=m;i++){
int op,u,v;
op=read();u=read();v=read();
if(!op){
++tot;
ai[tot].from=u;
ai[tot].to=v;
}else{
add(u,v);
++ru[v];
}
}
if(topo()){
cout<<"YES"<<endl;
for(int i=1;i<=n;i++)for(int j=head[i];j;j=a[j].next)
cout<<i<<" "<<a[j].to<<endl;
//for(int i=1;i<=cnt;i++)cout<<a[i].from<<" "<<a[i].to<<endl;
for(int i=1;i<=tot;i++){
if(xh[ai[i].from]>xh[ai[i].to])cout<<ai[i].to<<" "<<ai[i].from<<endl;
else cout<<ai[i].from<<" "<<ai[i].to<<endl;
}
}else cout<<"NO"<<endl;
}
return 0;
}

2021.07.17 题解 CF1385E Directing Edges(拓扑排序)的更多相关文章

  1. 2021.07.17 P4170 染色(区间DP)

    2021.07.17 P4170 染色(区间DP) [P4170 CQOI2007]涂色 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 重点: 1.目标状态可以由哪些状态转移过来. ...

  2. 2021.07.17 P3177 树上染色(树形DP)

    2021.07.17 P3177 树上染色(树形DP) [P3177 HAOI2015]树上染色 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 重点: 1.dp思想是需要什么,维护 ...

  3. DMOJ IOI '17 P3 - Toy Train【拓扑排序】

    传送:https://dmoj.ca/problem/ioi17p3 参考:https://blog.csdn.net/qq_27327327/article/details/80711824 妙啊- ...

  4. 动态规划 洛谷P4017 最大食物链计数——图上动态规划 拓扑排序

    洛谷P4017 最大食物链计数 这是洛谷一题普及/提高-的题目,也是我第一次做的一题 图上动态规划/拓扑排序 ,我认为这题是很好的学习拓扑排序的题目. 在这题中,我学到了几个名词,入度,出度,及没有环 ...

  5. hdu 5438 Ponds 拓扑排序

    Ponds Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/contests/contest_showproblem ...

  6. [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 D ...

  7. HDU 5438 拓扑排序+DFS

    Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Sub ...

  8. Legal or Not(拓扑排序判环)

    http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Others)   ...

  9. HDU 5195 DZY Loves Topological Sorting 拓扑排序

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5195 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

随机推荐

  1. 【文件系统】dumpe2fs命令

    dumpe2fs - dump ext2/ext3/ext4 filesystem information dumpe2fs prints the super block and blocks gro ...

  2. 仿真pda,部署时出现问题

    为什么部署到基于 Windows Mobile 的 Pocket PC 设备或模拟器会因共享冲突错误而失败 自己遇到了网上找到的解决方案http://hi.baidu.com/yeflower/blo ...

  3. 10年.NET老程序员推荐的7个开发类工具

    做.NET软件工作已经10年了,从程序员做 到高级程序员,再到技术主管,技术总监.见证了Visual Studio .NET 2003,Visul Studio 2005, Visual Studio ...

  4. 深度优先算法--对DFS的一些小小的总结(一)

    提到DFS,我们首先想到的是对树的DFS,例如下面的例子:求二叉树的深度 int TreeDepth(BinaryTreeNode* root){ if(root==nullptr)return 0; ...

  5. 面试之Java String 编码相关

    实话说,作为一个多年Java老年程序员,直到近来,在没有决心花时间搞清楚Java String的编码相关问题之前, 自己也都还是似懂非懂,一脸懵逼的.设想如果在面试中,有同学能够条理清晰的回答下面的问 ...

  6. SpringBoot:自定义注解实现后台接收Json参数

    0.需求 在实际的开发过程中,服务间调用一般使用Json传参的模式,SpringBoot项目无法使用@RequestParam接收Json传参 只有@RequestBody支持Json,但是每次为了一 ...

  7. 使用过 Redis 分布式锁么,它是什么回事?

    先拿 setnx 来争抢锁,抢到之后,再用 expire 给锁加一个过期时间防止锁忘记了 释放. 这时候对方会告诉你说你回答得不错,然后接着问如果在 setnx 之后执行 expire 之前进程意外  ...

  8. Postgres数据库知识点

    1.批量插入: test_list = []for i in range(100): test_list.append(models.Testmodel(name=i, message="{ ...

  9. 在 Spring 框架中如何更有效地使用 JDBC?

    使用 SpringJDBC 框架,资源管理和错误处理的代价都会被减轻.所以开发者只 需写 statements 和 queries 从数据存取数据,JDBC 也可以在 Spring 框架提供 的模板类 ...

  10. 客户端回调 Watcher?

    客户端 SendThread 线程接收事件通知,交由 EventThread 线程回调 Watcher. 客户端的 Watcher 机制同样是一次性的,一旦被触发后,该 Watcher 就失效了.