首页 技术 正文
技术 2022年11月20日
0 收藏 811 点赞 3,910 浏览 1344 个字

洛谷传送门


看着这道题给人感觉就是tarjan求SCC,然而还得判断是否能控制全部间谍,这就得先从可以贿赂的点dfs一遍。

如果没有全部被标记了,就输出NO,再从没被标记的点里找最小的标号。

如果全被标记,就输出YES,再从入度为0的缩点里找最小的价格,加到ans中,最后输出ans。

——代码

 #include <cstdio>
#include <stack>
#include <cstring> using namespace std; int n, p, r1, cnt, idx, ans = , minn;
int a[], next[], to[], head[], low[], dfn[], belong[], r[];
bool ins[], vis[], mey[];
stack <int> s; inline void add(int x, int y)
{
to[cnt] = y;
next[cnt] = head[x];
head[x] = cnt++;
} void dfs(int u)
{
int i, v;
vis[u] = ;
mey[u] = ;
for(i = head[u]; i != -; i = next[i])
{
v = to[i];
if(!vis[v]) dfs(v);
}
} void tarjan(int u)
{
low[u] = dfn[u] = ++idx;
s.push(u);
ins[u] = ;
int i, v;
for(i = head[u]; i != -; i = next[i])
{
v = to[i];
if(!dfn[v])
{
tarjan(v);
low[u] = min(low[u], low[v]);
}
else if(ins[v]) low[u] = min(low[u], dfn[v]);
}
if(low[u] == dfn[u])
{
cnt++;
do
{
v = s.top();
s.pop();
ins[v] = ;
belong[v] = cnt;
}while(u != v);
}
} int main()
{
int i, j, k, x, y, u, v;
scanf("%d %d", &n, &p);
for(i = ; i <= p; i++)
{
scanf("%d", &x);
scanf("%d", &a[x]);
}
memset(head, -, sizeof(head));
scanf("%d", &r1);
for(i = ; i <= r1; i++)
{
scanf("%d %d", &x, &y);
add(x, y);
}
for(i = ; i <= n; i++)
if(!vis[i] && a[i])
dfs(i);
for(i = ; i <= n; i++)
if(!mey[i])
ans = min(ans, i);
if(ans != )
{
printf("NO\n%d", ans);
return ;
}
cnt = ;
for(i = ; i <= n; i++)
if(!dfn[i])
tarjan(i);
for(u = ; u <= n; u++)
for(i = head[u]; i != -; i = next[i])
{
v = to[i];
if(belong[u] != belong[v]) r[belong[v]]++;
}
ans = ;
for(i = ; i <= cnt; i++)
if(r[i] == )
{
minn = ;
for(j = ; j <= n; j++)
if(belong[j] == i && a[j])
minn = min(minn, a[j]);
ans += minn;
}
printf("YES\n%d", ans);
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,999
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,357
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,140
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848