2021.07.17 题解 CF1385E Directing Edges(拓扑排序)
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(拓扑排序)的更多相关文章
- 2021.07.17 P4170 染色(区间DP)
2021.07.17 P4170 染色(区间DP) [P4170 CQOI2007]涂色 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 重点: 1.目标状态可以由哪些状态转移过来. ...
- 2021.07.17 P3177 树上染色(树形DP)
2021.07.17 P3177 树上染色(树形DP) [P3177 HAOI2015]树上染色 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 重点: 1.dp思想是需要什么,维护 ...
- DMOJ IOI '17 P3 - Toy Train【拓扑排序】
传送:https://dmoj.ca/problem/ioi17p3 参考:https://blog.csdn.net/qq_27327327/article/details/80711824 妙啊- ...
- 动态规划 洛谷P4017 最大食物链计数——图上动态规划 拓扑排序
洛谷P4017 最大食物链计数 这是洛谷一题普及/提高-的题目,也是我第一次做的一题 图上动态规划/拓扑排序 ,我认为这题是很好的学习拓扑排序的题目. 在这题中,我学到了几个名词,入度,出度,及没有环 ...
- hdu 5438 Ponds 拓扑排序
Ponds Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/contests/contest_showproblem ...
- [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10161 Accepted: 2810 D ...
- HDU 5438 拓扑排序+DFS
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...
- Legal or Not(拓扑排序判环)
http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 5195 DZY Loves Topological Sorting 拓扑排序
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5195 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
随机推荐
- C# 将CSV转为Excel
CSV(Comma Separated Values)文件是一种纯文本文件,包含用逗号分隔的数据,常用于将数据从一个应用程序导入或导出到另一个应用程序.通过将CSV文件转为EXCEL,可执行更多关于数 ...
- Nextcloud fpm 版在 Dokcer 下安装踩坑
安装 首先到 https://registry.hub.docker.com/_/nextcloud 获取 Nextcloud 的示例 docker-compose version: '2' volu ...
- dp:找零问题
C代表币的种类,n代表钱数 #include<iostream> using namespace std; #define C 4 void main( ) { int coin[4]={ ...
- 面试问题之计算机网络:HTTP和HTTPS的区别
https://blog.csdn.net/qq_38289815/article/details/80969419
- 手撕代码之线程:thread类简单使用
转载于:https://blog.csdn.net/qq_22494029/article/details/79273127 简单多线程例子: detch()启动线程: 1 #include < ...
- 什么是 spring 装配?
当 bean 在 Spring 容器中组合在一起时,它被称为装配或 bean 装配.Spring 容器需要知道需要什么 bean 以及容器应该如何使用依赖注入来将 bean 绑定 在一起,同时装配 b ...
- jvm性能调优工具
1.jstat 命令 jstat: 查看类装载,内存,垃圾收集,gc相关信息 命令参数 # jstat -option -t #option:参数选项,-t:显示系统的时间 # jstat -opti ...
- 面试题目:手写一个LRU算法实现
一.常见的内存淘汰算法 FIFO 先进先出 在这种淘汰算法中,先进⼊缓存的会先被淘汰 命中率很低 LRU Least recently used,最近最少使⽤get 根据数据的历史访问记录来进⾏淘汰 ...
- C语言类型转换原理
C语言类型转换 int a; a=1.23 这里把1.23赋值给a发生了隐式转换,原理如下: int a; float b=3.14; a=b; b赋值给a的过程:首先找一个中间变量是a的类型(该例中 ...
- 移动端比1px还小的border
巧用border 在移动端 经常出现border,细边框但有的时候 产品大大1px甚至乎会觉得不够细那么要如何写出比1px还要小的border下面是代码 希望对大家有所帮助 .thinner-bord ...