Codeforces Round #660 (Div. 2) D. Captain Flint and Treasure
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #660 (Div. 2) D. Captain Flint and Treasure相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e8%a6%81%e6%b1%82' title='瑕佹眰'>瑕佹眰
close span sum 鍥剧墖 amp col tps clu棰樼洰閾炬帴锛歨ttps://codeforc.es/contest/1388/problem/D
棰樻剰锛氫竴绉嶆搷浣滀负 閫変竴涓笅鏍?浣垮緱ans+=a[i] 涓?鎶奱[i]+鍒癮[b[i]]涓?nbsp; 瑕佹眰姣忎釜涓嬫爣閮借繘琛屼竴绉嶈繖鏍风殑鎿嶄綔锛岄棶鎬庝箞鏍风殑鎿嶄綔椤哄簭鎵嶈兘浣垮緱ans鏈€澶?/p>
鎬濊矾锛氳浣垮緱ans鏈€澶э紝閭d箞鑲畾鏄痑[i]涓烘鏁扮殑閮藉敖閲忔棭鐨勭疮鍔狅紝涓鸿礋鏁扮殑閮藉敖閲忔櫄鐨勭疮鍔狅紝閭d箞鐜板湪鍙渶瑕佽€冭檻濡備綍閬嶅巻灏辫浜嗭紝棰樼洰宸茬粡璇存槑鏄?/p>
鏈夊悜鏃犵幆鍥撅紝閭d箞棣栧厛鎯冲埌鐨勫氨搴旇鏄嫇鎵戞帓搴忥紝浠庡叆搴︿负0寮€濮嬶紝杩欐牱鎵嶈兘鏈€澶ч檺搴︾殑绱姞
閬嶅巻瀹屼箣鍚庯紝 鍐嶄粠鍑哄害涓?鐨勫厛杩涜鎿嶄綔锛岃繖鏍峰墿涓嬬殑璐熸暟灏变笉浼氱疮鍔犲埌鍏朵粬鏁颁笂
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define ull unsigned long long 5 #define pb push_back 6 const int maxn=2e5+10; 7 const int mod=1e9+7; 8 ll a[maxn]; 9 ll b[maxn]; 10 int ru[maxn]; 11 int chu[maxn]; 12 vector<int>E[maxn]; 13 vector<int>G[maxn]; 14 int vis[maxn]; 15 16 17 18 int main() 19 { 20 ios::sync_with_stdio(false); 21 cin.tie(0); 22 int n; 23 cin>>n; 24 ll sum=0; 25 for(int i=1;i<=n;i++) 26 { 27 cin>>a[i]; 28 } 29 for(int i=1;i<=n;i++) 30 { 31 cin>>b[i]; 32 } 33 vector<int>ans; 34 for(int i=1;i<=n;i++) 35 { 36 if(b[i]==-1) 37 continue; 38 ru[b[i]]++; 39 chu[i]++; 40 E[i].pb(b[i]); 41 G[b[i]].pb(i); 42 } 43 queue<int>q; 44 for(int i=1;i<=n;i++) 45 { 46 if(ru[i]==0) 47 { 48 q.push(i); 49 } 50 } 51 //cout<<sum<<鈥榎n鈥? 52 while(!q.empty()) 53 { 54 int u=q.front(); 55 q.pop(); 56 for(auto &v:E[u]) 57 { 58 ru[v]--; 59 if(a[u]>=0) 60 { 61 a[v]+=a[u]; 62 sum+=a[u]; 63 ans.pb(u); 64 vis[u]=1; 65 } 66 if(ru[v]==0) 67 { 68 q.push(v); 69 } 70 } 71 } 72 //cout<<sum<<鈥榎n鈥? 73 queue<int>qh; 74 for(int i=1;i<=n;i++) 75 { 76 if(chu[i]==0&&vis[i]==0) 77 { 78 qh.push(i); 79 } 80 } 81 //cout<<sum<<鈥榎n鈥? 82 while(!qh.empty()) 83 { 84 int u=qh.front(); 85 qh.pop(); 86 if(vis[u]==0) 87 { 88 sum+=a[u]; 89 ans.pb(u); 90 } 91 for(auto &v:G[u]) 92 { 93 chu[v]--; 94 if(chu[v]==0) 95 qh.push(v); 96 } 97 } 98 cout<<sum<<鈥?/span> 鈥?/span>; 99 for(auto &v:ans) 100 { 101 cout<<v<<" "; 102 } 103 104 105 106 107 }
以上是关于Codeforces Round #660 (Div. 2) D. Captain Flint and Treasure的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #436 E. Fire(背包dp+输出路径)
[ACM]Codeforces Round #534 (Div. 2)
CodeForces - 660F:Bear and Bowling 4(DP+斜率优化)
Codeforces Round #726 (Div. 2) B. Bad Boy(贪心)