結果

問題 No.2523 Trick Flower
ユーザー ecotteaecottea
提出日時 2023-10-28 02:40:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 9,201 bytes
コンパイル時間 5,251 ms
コンパイル使用メモリ 276,076 KB
実行使用メモリ 26,172 KB
最終ジャッジ日時 2023-10-28 02:40:09
合計ジャッジ時間 8,306 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 2 ms
4,372 KB
testcase_03 AC 2 ms
4,372 KB
testcase_04 AC 2 ms
4,372 KB
testcase_05 AC 2 ms
4,372 KB
testcase_06 AC 2 ms
4,372 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef HIDDEN_IN_VS // 折りたたみ用

// 警告の抑制
#define _CRT_SECURE_NO_WARNINGS

// ライブラリの読み込み
#include <bits/stdc++.h>
using namespace std;

// 型名の短縮
using ll = long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9)
using pii = pair<int, int>;	using pll = pair<ll, ll>;	using pil = pair<int, ll>;	using pli = pair<ll, int>;
using vi = vector<int>;		using vvi = vector<vi>;		using vvvi = vector<vvi>;	using vvvvi = vector<vvvi>;
using vl = vector<ll>;		using vvl = vector<vl>;		using vvvl = vector<vvl>;	using vvvvl = vector<vvvl>;
using vb = vector<bool>;	using vvb = vector<vb>;		using vvvb = vector<vvb>;
using vc = vector<char>;	using vvc = vector<vc>;		using vvvc = vector<vvc>;
using vd = vector<double>;	using vvd = vector<vd>;		using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;

// 定数の定義
const double PI = acos(-1);
const vi DX = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)
const vi DY = { 0, 1, 0, -1 };
int INF = 1001001001; ll INFL = 4004004003104004004LL; // (int)INFL = 1010931620;

// 入出力高速化
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;

// 汎用マクロの定義
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), x))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), x))
#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}
#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順
#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順
#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順
#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)
#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)
#define repb(set, d) for(int set = 0; set < (1 << int(d)); ++set) // d ビット全探索(昇順)
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)
#define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去
#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了
#define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) // 半開矩形内判定

// 汎用関数の定義
template <class T> inline ll pow(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)
template <class T> inline T get(T set, int i) { return (set >> i) & T(1); }

// 演算子オーバーロード
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }

#endif // 折りたたみ用


#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;

#ifdef _MSC_VER
#include "localACL.hpp"
#endif

//using mint = modint1000000007;
using mint = modint998244353;
//using mint = modint; // mint::set_mod(m);

namespace atcoder {
	inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
	inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
}
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>;
#endif


#ifdef _MSC_VER // 手元環境(Visual Studio)
#include "local.hpp"
#else // 提出用(gcc)
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : -1; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
#define dump(...)
#define dumpel(v)
#define dump_list(v)
#define dump_mat(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) while (1) cout << "OLE"; }
#endif


//【参照付きグラフの辺】
/*
* int from : 始点
* int to : 終点
* int id : 辺番号
* bool dir : 順方向か
*/
struct IEdge {
	// verify : https://judge.yosupo.jp/problem/cycle_detection_undirected

	int from; // 始点
	int to; // 終点
	int id; // 辺番号
	bool dir; // 順方向か

	IEdge() : from(-1), to(-1), id(-1), dir(true) {}
	IEdge(int from, int to, int id, bool dir = true) : from(from), to(to), id(id), dir(dir) {}

	// プレーングラフで呼ばれたとき用
	operator int() const { return to; }

#ifdef _MSC_VER
	friend ostream& operator<<(ostream& os, const IEdge& e) {
		os << '(' << e.from << "→" << e.to << ',' << "id:" << e.id << ',' << (e.dir ? "fwd" : "rev") << ')';
		return os;
	}
#endif
};


//【参照付きグラフ】
/*
* IGraph g
* g[v] : 頂点 v から出る辺を並べたリスト
*
* verify : https://judge.yosupo.jp/problem/cycle_detection_undirected
*/
using IGraph = vector<vector<IEdge>>;


//【閉路抽出(有向グラフ)】O(n + m)
/*
* 有向グラフ g から極大個数の単純閉路を抽出し,各閉路に含まれる辺のリストを返す.
*/
vvi directed_cycles_detection(IGraph g) {
	// verify : https://yukicoder.me/problems/no/2464

	int n = sz(g);

	vvi cycles;

	// 0 : 未探索,1 : 探索済,2 : 閉路の端点
	vi seen(n);

	// 0 : 探索モード,2 : 閉路抽出モード
	int tp = 0;

	function<void(int)> dfs = [&](int s) {
		seen[s] = 1;

		while (!g[s].empty()) {
			// 同じ辺を二度見ないように削除しておく.
			auto t = g[s].back(); g[s].pop_back();

			// 自己ループは単独の閉路として記録する.
			if (t == s) {
				cycles.push_back(vi{t.id});
				continue;
			}

			// 探索済の頂点にたどり着いたなら閉路を検出できた.
			if (seen[t.to] == 1) {
				// t が閉路の端点であることを覚えておく.
				seen[t.to] = 2;

				// 閉路抽出モードに移行する.
				tp = 2;
				cycles.push_back(vi());
				cycles.back().push_back(t.id);
				break;
			}

			dfs(t.to);

			// 閉路抽出モードなら,探索は一休みして閉路を抽出する.
			if (tp == 2) {
				cycles.back().push_back(t.id);

				// 閉路の端点まで抽出しきったのなら探索モードに移行する.
				if (seen[s] == 2) {
					reverse(all(cycles.back()));
					seen[s] = 1;
					tp = 0;
					continue;
				}
				break;
			}
		}

		// 頂点に関しては二度以上見る必要があるのでバックトラッキングする.
		seen[s] = 0;
	};

	// 各頂点 s を通る閉路をできるかぎり抽出する.
	rep(s, n) while (!g[s].empty()) dfs(s);

	return cycles;
}


//【めぐる式二分探索】O(log|ok - ng|)
/*
* 条件 okQ() を満たす要素 ok と満たさない要素 ng との境界を二分探索する.
* 境界に隣り合うような条件を満たす要素(ok 側)の位置を返す.
*/
template <class T, class FUNC>
T meguru_search(T ok, T ng, const FUNC& okQ) {
	// 参考 : https://twitter.com/meguru_comp/status/697008509376835584
	// verify : https://atcoder.jp/contests/typical90/tasks/typical90_a

	// 境界が決定するまで
	while (abs(ok - ng) > 1) {
		// 区間の中間
		T mid = (ok + ng) / 2;

		// 中間が OK かどうかに応じて区間を縮小する.
		if (okQ(mid)) ok = mid;
		else ng = mid;
	}
	return ok;

	/* okQ の定義の雛形
	auto okQ = [&](ll x) {
		return true || false;
	};
	*/
}


int main() {
//	input_from_file("input.txt");
//	output_to_file("output.txt");

	int n;
	cin >> n;

	vl a(n), b(n); vi c(n);
	cin >> a >> b >> c;
	--c;

	IGraph g(n);
	rep(i, n) g[i].push_back({ i, c[i], i, true });
	dumpel(g);

	auto cy = directed_cycles_detection(g)[0];
	dump(cy);

	vb ison(n);
	repe(v, cy) ison[v] = true;
	dump(ison);

	Graph gR(n);
	rep(s, n) if (!ison[s]) gR[c[s]].push_back(s);
	dumpel(gR);

	auto okQ = [&](__int128 x) {
		vector<__int128> bx(n);
		rep(i, n) bx[i] = b[i] * x;

		function<void(int)> dfs = [&](int s) {
			repe(t, gR[s]) {
				bx[s] += max<__int128>(bx[t], 0);
			}
			bx[s] -= a[s];
		};
		rep(s, n) if (ison[s]) dfs(s);

		__int128 bxsum = 0;
		rep(s, n) if (ison[s]) bxsum += bx[s];

		return bxsum <= 0;
	};

	auto x = meguru_search<__int128>(0, (ll)1e18 + 100, okQ);

	cout << (ll)x << endl;
}
0