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

【题意】

题意

【题解】

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

    /* 枚举每个部分的总和,利用前缀和进行检验. 如果能分成4部分就一定能分成2部分,就筛了一边素数优化.清空数组!!! */ #include<bits/stdc++.h> #define ...

  2. [App Store Connect帮助]三、管理 App 和版本(6.3)转让 App:接受 App 转让

    您必须在转让发起的 60 天内接受转让. 必要职能:“帐户持有人”职能.请参见职能权限. 以具有“帐户持有人”职能用户的身份登录至 App Store Connect. 系统会显示一条通知,指示 Ap ...

  3. Elementui tabs组件内添加组件

    1. Elementui tabs组件内添加组件 1.1. 需求 今天的一个需求是在后台框架的基础上添加tab页,结果页面如下 原本上述红框内的内容是不存在的,直接点击左侧菜单栏进行页面跳转,现加了t ...

  4. [ZJOI2006]Book书架

    Description Sally有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号.Sally在看书的时候,每次取出一本书,看完后放回书柜 ...

  5. Java compiler level does not match the version of the installed Java project facet问题处理

    从SVN上下载应用后在Problems面板中提示以下错误信息: Java compiler level does not match the version of the installed Java ...

  6. HTML基础2——综合案例2——复杂的嵌套列表

    <html> <head> <title></title> </head> <body> <ul type="d ...

  7. CSS之选择符、链接、盒子模型、显示隐藏元素

    <html> <head> <meta charset="utf-8"> <title>选择符.链接.盒子模型.显示隐藏元素< ...

  8. ftp获取mysql数据库方法

    我说的这种情况是针对mysql数据库的,首先下载一个mysql通过ftp放到站点里面,然后通过配置文件找到数据库的名字和密码,然后通过浏览器访问数据库,直接在域名后面加上下载的mysql文件的名字就可 ...

  9. 修改 进程占用资源限制ulimit(限制服务器的链接数目)

    ulimit用于限制shell启动进程所占用的资源.其中ulimit -n用于限制进程能够打开的文件描述符的最大数目.因为任何设备在linux下都是文件,通信的接口也有专门的接口文件负责,所以linu ...

  10. html与html5 总结

    时间:于2017年12月3日 19:35:18开始 用于: 这文章用于个人技术总结(全文转发请标明出处,小段摘抄随意) HTML笔记1.html标签不区分大小写,但是建议小写2.常用标签: 可用在he ...