【链接】 我是链接,点我呀:)

【题意】

题意

【题解】

先求出来强连通分量。
每个联通分量里面,显然在联通块的尽头(没有出度)放一个捕鼠夹就ok了

【代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define ri(x) scanf("%d",&x)
#define rl(x) scanf("%lld",&x)
#define rs(x) scanf("%s",x)
#define oi(x) printf("%d",x)
#define ol(x) printf("%lld",x)
#define oc putchar(' ')
#define os(x) printf(x)
#define all(x) x.begin(),x.end()
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 2e5;//节点个数 vector <int> G[N+10],g[N+10];
int n,m,tot = 0,top = 0,dfn[N+10],low[N+10],z[N+10],totn,in[N+10];
int bh[N+10];
int mi[N+10];
bool bo[N+10];
int c[N+10];
int f[N+10],f_ans[N+10]; void dfs(int x){
dfn[x] = low[x] = ++ tot;
z[++top] = x;
in[x] = 1;
int len = G[x].size();
rep1(i,0,len-1){
int y = G[x][i];
if (!dfn[y]){
dfs(y);
low[x] = min(low[x],low[y]);
}else
if (in[y] && dfn[y]<low[x]){
low[x] = dfn[y];
}
}
if (low[x]==dfn[x]){
int v = 0;
totn++;
mi[totn] = c[z[top]];
while (v!=x){
v = z[top];
mi[totn] = min(mi[totn],c[v]);
in[v] = 0;
bh[v] = totn;
top--;
}
}
} int dfs1(int x){
if (bo[x]==true) return 0;
bo[x] = true;
int len = g[x].size();
if (len==0) return mi[x];
return dfs1(g[x][0]);
} int main(){
#ifdef LOCAL_DEFINE
freopen("rush_in.txt", "r", stdin);
#endif
ms(dfn,0);
ms(in,0);
tot = 0,totn = 0;
ri(n);
rep1(i,1,n) G[i].clear(),g[i].clear();
rep1(i,1,n){
cin >> c[i];
}
rep1(i,1,n){
int x,y;
ri(y);
x = i;
G[x].pb(y);
} rep1(i,1,n)
if (dfn[i]==0)
dfs(i); rep1(i,1,n){
int len = G[i].size();
int xx = bh[i];
rep1(j,0,len-1){
int y = G[i][j];
int yy = bh[y];
if (xx!=yy){
g[xx].pb(yy);
}
}
}
n = totn; int ans = 0;
rep1(i,1,n) ans += dfs1(i); cout<<ans<<endl;
return 0;
}

【Codeforces 1027D】Mouse Hunt的更多相关文章

  1. 【Edu49 1027D】 Mouse Hunt DFS 环

    1027D. Mouse Hunt:http://codeforces.com/contest/1027/problem/D 题意: 有n个房间,每个房间放置捕鼠器的费用是不同的,已知老鼠在一个房间x ...

  2. 【codeforces 807B】T-Shirt Hunt

    [题目链接]:http://codeforces.com/contest/807/problem/B [题意] 你在另外一场已经结束的比赛中有一个排名p; 然后你现在在进行另外一场比赛 然后你当前有一 ...

  3. 【Codeforces 979B】Treasure Hunt

    [链接] 我是链接,点我呀:) [题意] 每次你可以将一个字符变成一个不同于本身的字符. 每个人需要改变n次(且不能不改变) 设每个人的字符串中出现次数最多的字符出现的次数为cnt[0~2] 问你谁的 ...

  4. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  5. 【codeforces 762B】USB vs. PS/2

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  7. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  8. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  9. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

随机推荐

  1. Linux day01(二)虚拟机快照和克隆的用法介绍

    一:快照 优点:运行虚拟机后不用担心系统会被弄崩溃了,点击快照会立即恢复到初始状态 缺点:回滚会带来数据的丢失,所以要考虑数据恢复的成本和找回数据时进行操作的成本 1. 在导航栏中找虚拟机快照的小图标 ...

  2. 再谈 webpack build 及 加载优化

    之前项目多,事情忙,一直没时间写博客,现在空闲下来了,总结一下 之前讲过了关于 build 压缩文件的方法,有兴趣的可以看下: 点击查看 现在讲讲一个页面的首屏加载速度该如何提升 提前说明 需要 we ...

  3. 微信小程序获取自定义属性值

    写小程序的时候用到了自定义属性,特地来记录一下 特别是这个坑,必须得说一说 wxml <view class='box' bindtap='getValue'> <view clas ...

  4. loadrunner11 安装破解,汉化包

    说说自己的心痛史,好不容易安装了loadrunner 11 居然浏览器不支持,我的系统是win8.1,ie浏览器最低支持ie11,我还能说啥子...其他浏览器试过了依旧是不可以!!所以我安装了一个虚拟 ...

  5. Lightoj 1010 - Knights in Chessboard (胡搞)

    题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1010 题目描述: 有一个n*m的棋盘,根据象棋中马走日字的规则,问此棋盘最多 ...

  6. 启动tomcat报错:ImageFormatException

    启动某工程报错: java.lang.NoClassDefFoundError: com/sun/image/codec/jpeg/ImageFormatException 查找此类存在于jdk的rt ...

  7. 数据传递-------@ResponseBody

    1.导入jar包 jack-core-asl-1.9.11.jar jack-mapper-asl-1.9.11.jar 2.配置springmvc-servlet.xml文件 <?xml ve ...

  8. 385 Mini Parser 迷你解析器

    Given a nested list of integers represented as a string, implement a parser to deserialize it.Each e ...

  9. 在计算机视觉与人工智能领域,顶级会议比SCI更重要(内容转)

    很多领域,SCI是王道,尤其在中国,在教师科研职称评审和学生毕业条件中都对SCI极为重视,而会议则充当了补充者的身份.但是在计算机领域,尤其是人工智能与机器学习领域里,往往研究者们更加青睐于会议 我无 ...

  10. 微软MVC框架实战:开源的JS库Knockout

    [IT168 技术] Knowckout – 当MVC遭遇MVVM Knockout (或者Knockout.js ,KnockoutJS)是一个开源的JavaScript库,网址为www.knock ...