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

【题意】

题意

【题解】

先求出来强连通分量。
每个联通分量里面,显然在联通块的尽头(没有出度)放一个捕鼠夹就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. FastDFS的介绍

    FastDFS的介绍 FastDFS FastDFS是由国人余庆所开发,其项目地址: https://github.com/happyfish100 FastDFS是一个轻量级的开源分布式文件系统,主 ...

  2. [Swift通天遁地]一、超级工具-(17)自定义的CVCalendar日历

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  3. 制作并发布个人CocoaPods库

    随着对 CocoaPods 越来越多的依赖,我们也可以尝试把自己的库发布到它上面. 1.在Github上新建一个项目(名字我随便取了一个,其他步骤截图为WCUIKit).自己做相应修改即可. 2.克隆 ...

  4. laravel ORM 只开启created_at的方法

    class User extends Model { //重写setUpdatedAt方法 public function setUpdatedAt($value) { // Do nothing. ...

  5. Linux文件详解

    一.Linux文件类型分:普通文件.目录文件.链接文件.设备文件.管道文件. 1.普通文件:由ls -al显示属性时,第一个属性为 [-],例如 [-rwxrwxrwx].包括: 纯文本文件(ASCI ...

  6. POJ 2773 欧几里得

    思路: 若a和b互素的话,则b*t+a和b一定互素 用周期性做就好了 //By SiriusRen #include <cstdio> using namespace std; ],m,k ...

  7. POJ 2194 2850 计算几何

    题意: 给你了n个圆,让你摞起来,问顶层圆心的坐标 (数据保证间隔两层的圆不会挨着) 思路: 按照题意模拟. 假设我们已经知道了一层两个相邻圆的坐标a:(x1,y1)和b:(x2,y2) 很容易求出来 ...

  8. 1、DOS基本命令

    命令dir能给列出当前目录下面的所有文件.程序和子目录.所有目录(Windows 中称为文件夹)的目录名前面都有一个<DIR>标记.文件和程序名前面显示有这些文件和程序的大小. 想说的是, ...

  9. nginx connect failed (110- Connection timed out) 问题排查

    首先排查 ping 下 nginx 与代理服务是否ping 的通,带端口的,telnet 下端口号是否是通的,本次遇到问题为 telnet 发现有台服务器不通,原因是端口未开放

  10. WordPress个性页面制作教程

    写在前面的话: 有很多WordPress小伙伴想制作不同风格的页面来满足自己的个性需求 但是大多数模板提供的页面模板非常有限,该如何手动制作属于自己风格的模板页呢? 其实,正如以上所说的,每个人都想拥 ...