#洛谷 1262 图论 tarjan

并不感觉把这道题目放在图的遍历中很合适,虽然思路比较简单但是代码还是有点多的,,

将可收买的间谍的cost值设为它的价格,不可购买的设为inf,按照控制关系连图,Tarjan缩点,得到的新图中,入度为0的点是必须购买的,如果这些点中存在inf,则不成立


#include <cstdio>
#include <algorithm>
#include <cstring> const int inf = 0x3f3f3f3f;
const int maxn = (3000 + 500) * 2;
const int maxm = 50000;
int dfn[maxn], low[maxn], vis[maxn];
int sta[maxn];
int stackTop = 0;
int tim = 0;
int cost[maxn];
int costNew[maxn];
int from[maxn];
int cur = 0;
int indo[maxn];
int outdo[maxn];
int n, p, r;
int x, y;
int last[maxm], other[maxm], pre[maxm];
int totEdge = 0; void add(int x, int y) {
totEdge++;
pre[totEdge] = last[x];
last[x] = totEdge;
other[totEdge] = y;
}
void tarjan(int x) {
tim++;
dfn[x] = low[x] = tim;
vis[x] = 1;
stackTop++;
sta[stackTop] = x;
for (int p = last[x]; p; p = pre[p]) {
int q = other[p];
if (!dfn[q]) {
tarjan(q);
low[x] = std :: min(low[x], low[q]);
} else if (vis[q]) {
low[x] = std :: min(low[x], dfn[q]);
}
}
if (dfn[x] == low[x]) {
cur++;
costNew[cur] = inf;
while (sta[stackTop] != x) {
vis[sta[stackTop]] = 0;
from[sta[stackTop]] = cur;
costNew[cur] = std :: min(costNew[cur], cost[sta[stackTop]]);
stackTop--;
}
vis[x] = 0;
from[x] = cur;
costNew[cur] = std :: min(costNew[cur], cost[x]);
stackTop--;
}
} int main () {
scanf("%d", &n);
scanf("%d", &p);
for (int i = 1; i <= p; i++) {
scanf("%d", &x);
scanf("%d", &cost[x]);
}
for (int i = 1; i <= n; i++)
if (cost[i] == 0) cost[i] = inf;
scanf("%d", &r);
while (r--) {
scanf("%d %d", &x, &y);
add(x, y);
}
for (int i = 1; i <= n; i++) {
if (!dfn[i]) tarjan(i);
}
for (int i = 1; i <= n; i++)
for (int j = last[i]; j; j = pre[j]) {
int q = other[j];
if (from[i] != from[q]) {
indo[from[q]]++;
outdo[from[i]]++;
}
}
long long ans = 0;
bool flag = 0;
for (int i = 1; i <= cur; i++) {
if (indo[i] == 0) {
if (costNew[i] == inf) {
flag = 1;
break;
} else {
ans += costNew[i];
}
}
}
if (!flag) {
printf("YES\n%lld\n", ans);
} else {
printf("NO\n");
for (int i = 1; i <= n; i++) {
if (indo[from[i]] == 0 && costNew[from[i]] == inf) {
printf("%d", i);
break;
}
}
}
return 0;
}

最新文章

  1. JS组件系列——Bootstrap文件上传组件:bootstrap fileinput
  2. .NET 实现并行的几种方式(一)
  3. C/C++实践笔记 007
  4. html 涂改图片功能实现
  5. Ant编译提示“Unsupported major.minor version 52.0”
  6. vs出现“已经在解决方案中打开了具有该名称的项目”问题的解决方案
  7. html中input文本框,初始里边有文字提示,当点击时,文字消失,怎么设置?
  8. oracle学习笔记&mdash;&mdash;配置环境
  9. ios frame、bound和center定义及使用场景总结
  10. C语言位运算符:与、或、异或、取反,左移和右移
  11. mkdir、whoami、touch
  12. Adobe DreamweaverCS6安装及破解(序列号+破解补丁)
  13. JAVA WEB开发环境搭建教程
  14. 解决Fedora升级时nvidia显卡问题
  15. ActionBarSherlock,SlidingMenu
  16. ABAP 7.52 中的Open SQL新特性
  17. 查看dmp文件
  18. 【CF242E】Xor Segment
  19. Centos wget命令 not found解决方法
  20. 第 6 章 存储 - 040 - docker managed volume

热门文章

  1. CLLocationManagerDelegate的解说
  2. 6581 Number Triangle
  3. 单片机显示原理(LCD1602)
  4. 基于Torndb的简易ORM
  5. xcode Automatic signing is unable to resolve an issue with the &quot;ShowCar-IOS&quot; target&#39;s entitlements
  6. TensorRT加速 ——NVIDIA终端AI芯片加速用,可以直接利用caffe或TensorFlow生成的模型来predict(inference)
  7. nyoj--252--01串(水题)
  8. Node.js:路由
  9. 通过ip获取地址
  10. SMTP协议详解