結果

問題 No.2523 Trick Flower
ユーザー 👑 kmjpkmjp
提出日時 2023-10-28 01:20:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,698 bytes
コンパイル時間 2,629 ms
コンパイル使用メモリ 218,188 KB
実行使用メモリ 125,488 KB
最終ジャッジ日時 2023-10-28 01:20:40
合計ジャッジ時間 7,055 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
106,308 KB
testcase_01 AC 36 ms
106,308 KB
testcase_02 AC 34 ms
106,308 KB
testcase_03 AC 35 ms
106,308 KB
testcase_04 AC 35 ms
106,300 KB
testcase_05 AC 34 ms
106,300 KB
testcase_06 AC 35 ms
106,308 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 34 ms
106,352 KB
testcase_11 AC 34 ms
106,368 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 187 ms
125,488 KB
testcase_16 AC 188 ms
124,408 KB
testcase_17 AC 90 ms
120,316 KB
testcase_18 AC 96 ms
123,560 KB
testcase_19 AC 124 ms
121,916 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 113 ms
120,388 KB
testcase_26 WA -
testcase_27 AC 110 ms
121,188 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 96 ms
115,340 KB
testcase_31 WA -
testcase_32 AC 65 ms
111,564 KB
testcase_33 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N;
ll A[101010],B[101010];
int C[101010];

class SCC {
public:
	static const int MV = 2025000;
	vector<vector<int> > SC; int NV,GR[MV];
private:
	vector<int> E[MV], RE[MV], NUM; int vis[MV];
public:
	void init(int NV) { this->NV=NV; for(int i=0;i<NV;i++) { E[i].clear(); RE[i].clear();}}
	void add_edge(int x,int y) { E[x].push_back(y); RE[y].push_back(x); }
	void dfs(int cu) { vis[cu]=1; for(int i=0;i<E[cu].size();i++) if(!vis[E[cu][i]]) dfs(E[cu][i]); NUM.push_back(cu); }
	void revdfs(int cu, int ind) { int i; vis[cu]=1; GR[cu]=ind; SC[ind].push_back(cu);
		FOR(i,RE[cu].size()) if(!vis[RE[cu][i]]) revdfs(RE[cu][i],ind);}
	void scc() {
		int c=0,i; SC.clear(); SC.resize(NV); NUM.clear();
		assert(NV);
		FOR(i,NV) vis[i]=0; FOR(i,NV) if(!vis[i]) dfs(i); FOR(i,NV) vis[i]=0;
		for(int i=NUM.size()-1;i>=0;i--) if(!vis[NUM[i]]){
			SC[c].clear(); revdfs(NUM[i],c); sort(SC[c].begin(),SC[c].end()); c++;
		}
		SC.resize(c);
	}
};
SCC scc;
ll SA[101010],SB[101010];
int E[101010];
int in[101010];
vector<int> order;

int ok(ll a) {
	ll CA[101010];
	int i;
	FOR(i,N) CA[i]=SA[i];
	FORR(i,order) {
		if(CA[i]/a<SB[i]) return 0;
		if(E[i]>=0) CA[E[i]]+=CA[i]-SB[i]*a;
	}
	return 1;
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	ll sa=0,sb=0;
	FOR(i,N) {
		cin>>A[i];
		sa+=A[i];
	}
	FOR(i,N) {
		cin>>B[i];
		sb+=B[i];
	}
	scc.init(N);
	FOR(i,N) {
		cin>>C[i];
		C[i]--;
		scc.add_edge(C[i],i);
	}
	scc.scc();
	MINUS(E);
	FOR(i,N) {
		SA[scc.GR[i]]+=A[i];
		SB[scc.GR[i]]+=B[i];
		if(scc.GR[i]!=scc.GR[C[i]]) {
			E[scc.GR[C[i]]]=scc.GR[i];
			in[scc.GR[i]]++;
		}
	}
	N=scc.SC.size();
	queue<int> Q;
	FOR(i,N) if(in[i]==0) Q.push(i);
	while(Q.size()) {
		x=Q.front();
		Q.pop();
		order.push_back(x);
		if(E[x]>=0) {
			in[E[x]]--;
			if(in[E[x]]==0) Q.push(E[x]);
		}
	}
	
	
	ll L=0,R=sa/sb+1;
	while(L+1<R) {
		ll M=(L+R)/2;
		if(ok(M)) L=M;
		else R=M;
	}
	cout<<L<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0