Codeforces 1100E 拓扑排序
题意及思路:https://blog.csdn.net/mitsuha_/article/details/86482347
如果一条边(u, v),v的拓扑序小于u, 那么(u, v)会形成环,要反向。
代码:
#include <bits/stdc++.h>
#define pii pair<int, int>
using namespace std;
const int maxn = 100010;
vector<int> G[maxn];
vector<int> ans;
int limit;
int deg[maxn], s[maxn];
queue<int> q;
struct edge {
int x, y, val, id;
};
edge a[maxn];
void add(int x, int y) {
G[x].push_back(y);
deg[y]++;
}
int n, m;
bool topsort() {
ans.clear();
int tot = 0;
memset(deg, 0, sizeof(deg));
for (int i = 1; i <= n; i++)
G[i].clear();
for (int i = 1; i <= m; i++) {
if(a[i].val > limit) {
add(a[i].x, a[i].y);
}
}
for (int i = 1; i <= n; i++)
if(deg[i] == 0) {
q.push(i);
s[i] = ++tot;
}
while(q.size()) {
int x = q.front();
q.pop();
for (auto y : G[x]) {
deg[y]--;
if(deg[y] == 0) {
q.push(y);
s[y] = ++tot;
}
}
}
for (int i = 1; i <= n; i++) if(deg[i] > 0) return 0;
for (int i = 1; i <= m; i++) {
if(a[i].val <= limit) {
if(s[a[i].x] > s[a[i].y])
ans.push_back(a[i].id);
}
}
return 1;
}
int main() {
int x, y, z;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &x, &y, &z);
a[i] = (edge){x, y, z, i};
}
int l = 0, r = 1e9;
while(l < r) {
limit = (l + r) >> 1;
bool flag = topsort();
if(!flag) l = limit + 1;
else r = limit;
}
limit = l;
topsort();
printf("%d %d\n", l, ans.size());
for (int i = 0; i < ans.size(); i++)
printf("%d ", ans[i]);
printf("\n");
}
Codeforces 1100E 拓扑排序的更多相关文章
- CF思维联系--CodeForces -214C (拓扑排序+思维+贪心)
ACM思维题训练集合 Furik and Rubik love playing computer games. Furik has recently found a new game that gre ...
- CodeForces - 721C 拓扑排序+dp
题意: n个点m条边的图,起点为1,终点为n,每一条单向边输入格式为: a,b,c //从a点到b点耗时为c 题目问你最多从起点1到终点n能经过多少个不同的点,且总耗时小于等于t 题解: 这道 ...
- National Property CodeForces - 875C (拓扑排序)
大意: n个字符串, 每次操作选出一种字符全修改为大写, 求判断能否使n个字符串字典序非降. 建源点s, 汇点t, s与所有必须转大写的连边, 必须不转大写的与t连边. #include <io ...
- Codeforces 1159E 拓扑排序
题意及思路:https://www.cnblogs.com/dd-bond/p/10859864.html 代码: #include <bits/stdc++.h> #define LL ...
- E - E CodeForces - 1100E(拓扑排序 + 二分)
E - E CodeForces - 1100E 一个n个节点的有向图,节点标号从1到n,存在m条单向边.每条单向边有一个权值,代表翻转其方向所需的代价.求使图变成无环图,其中翻转的最大边权值最小的方 ...
- CodeForces - 1100E 二分+拓扑排序
题意: 一个n个节点的有向图,节点标号从1到n,存在m条单向边.每条单向边有一个权值,代表翻转其方向所需的代价.求使图变成无环图,其中翻转的最大边权值最小的方案,以及该方案翻转的最大的边权. Inpu ...
- Codeforces Round #292 (Div. 1) B. Drazil and Tiles 拓扑排序
B. Drazil and Tiles 题目连接: http://codeforces.com/contest/516/problem/B Description Drazil created a f ...
- Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 离散化拓扑排序
C. Mail Stamps Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
随机推荐
- Vue的计算属性缓存和method的区别在哪?
一.先看一个例子 <div id="example"> {{ message.split('').reverse().join('') }} </div> ...
- MySQL不支持的特性
MySQL 1.不支持物化视图. 2.不支持位图索引. 3.不支持并行查询. 4.不支持哈希关联,MySQL的所有关联都是嵌套循环关联.不过,可以通过建立一个哈希索引来曲线实现. 5.不允许对同一表同 ...
- ssm框架错误展示-1
1.xmlParse错误+找不到bean ,一般会出现在导入别的地方拷来的项目时发生,报了以下错误: 查看自己的项目配置文件,发现spring的配置文件和sql映射xml文件都有个黄颜色的感叹号,鼠标 ...
- HDU 6055 Regular polygon —— 2017 Multi-University Training 2
Regular polygon Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- FS,FT,DFS,DTFT,DFT,FFT的联系和区别 数字信号处理
DCT变换的原理及算法 文库介绍 对于初学数字信号处理(DSP)的人来说,这几种变换是最为头疼的,它们是数字信号处理的理论基础,贯穿整个信号的处理. 学习过<高等数学>和<信号与系统 ...
- JAVA(JDK,JRE)更改目录安装及环境变量配置
重温一下 JAVA(JDK,JRE)更改目录安装及环境变量配置 https://jingyan.baidu.com/article/e2284b2b5b7ae5e2e7118d11.html 备注:随 ...
- [CSP-S模拟测试]:beauty(搜索)
题目描述 距离产生美.一棵包含$n$个点的树,有$2k$个不同的关键点,我们现在需要将这些点两两配对,对于一种形如:$$(u_1,v_1),(u_2,v_2),...,(u_k,v_k)$$的配对方案 ...
- Autoresize UIView to fit subviews
@interface UIView (resizeToFit) -(void)resizeToFitSubviews; -(void)resizeHightToFitSubviews; -(void) ...
- (转)ping: www.baidu.com: Name or service not known centos7 -bash: ifconfig: command not found
[root@mysqlcentos01 ~]# ping www.baidu.com ping: www.baidu.com: Name or service not known [root@mysq ...
- (转)超详细java中的ClassLoader详解
转:https://blog.csdn.net/briblue/article/details/54973413 ClassLoader翻译过来就是类加载器,普通的java开发者其实用到的不多,但对于 ...