QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#158767#7103. Red Black Treeucup-team088#AC ✓861ms61624kbC++179.4kb2023-09-02 16:59:152023-09-02 16:59:16

Judging History

你现在查看的是最新测评结果

  • [2023-09-02 16:59:16]
  • 评测
  • 测评结果:AC
  • 用时:861ms
  • 内存:61624kb
  • [2023-09-02 16:59:15]
  • 提交

answer

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
#include<chrono>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
//ll mod = 1;
constexpr ll mod = 998244353;
//constexpr ll mod = 1000000007;
const int mod17 = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;

using ld = double;
typedef pair<ld, ld> LDP;
const ld eps = 1e-10;
const ld pi = acosl(-1.0);

template<typename T>
void chmin(T& a, T b) {
	a = min(a, b);
}
template<typename T>
void chmax(T& a, T b) {
	a = max(a, b);
}
template<typename T>
vector<T> vmerge(vector<T>& a, vector<T>& b) {
	vector<T> res;
	int ida = 0, idb = 0;
	while (ida < a.size() || idb < b.size()) {
		if (idb == b.size()) {
			res.push_back(a[ida]); ida++;
		}
		else if (ida == a.size()) {
			res.push_back(b[idb]); idb++;
		}
		else {
			if (a[ida] < b[idb]) {
				res.push_back(a[ida]); ida++;
			}
			else {
				res.push_back(b[idb]); idb++;
			}
		}
	}
	return res;
}
template<typename T>
void cinarray(vector<T>& v) {
	rep(i, v.size())cin >> v[i];
}
template<typename T>
void coutarray(vector<T>& v) {
	rep(i, v.size()) {
		if (i > 0)cout << " "; cout << v[i];
	}
	cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	//if (x == 0)return 0;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
//mod should be <2^31
struct modint {
	int n;
	modint() :n(0) { ; }
	modint(ll m) {
		if (m < 0 || mod <= m) {
			m %= mod; if (m < 0)m += mod;
		}
		n = m;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= (int)mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += (int)mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b) {
	a = abs(a); b = abs(b);
	if (a < b)swap(a, b);
	while (b) {
		ll r = a % b; a = b; b = r;
	}
	return a;
}
template<typename T>
void addv(vector<T>& v, int loc, T val) {
	if (loc >= v.size())v.resize(loc + 1, 0);
	v[loc] += val;
}
/*const int mn = 2000005;
bool isp[mn];
vector<int> ps;
void init() {
	fill(isp + 2, isp + mn, true);
	for (int i = 2; i < mn; i++) {
		if (!isp[i])continue;
		ps.push_back(i);
		for (int j = 2 * i; j < mn; j += i) {
			isp[j] = false;
		}
	}
}*/

//[,val)
template<typename T>
auto prev_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	if (res == st.begin())return st.end();
	res--; return res;
}

//[val,)
template<typename T>
auto next_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	return res;
}
using mP = pair<modint, modint>;
mP operator+(mP a, mP b) {
	return { a.first + b.first,a.second + b.second };
}
mP operator+=(mP& a, mP b) {
	a = a + b; return a;
}
mP operator-(mP a, mP b) {
	return { a.first - b.first,a.second - b.second };
}
mP operator-=(mP& a, mP b) {
	a = a - b; return a;
}
LP operator+(LP a, LP b) {
	return { a.first + b.first,a.second + b.second };
}
LP operator+=(LP& a, LP b) {
	a = a + b; return a;
}
LP operator-(LP a, LP b) {
	return { a.first - b.first,a.second - b.second };
}
LP operator-=(LP& a, LP b) {
	a = a - b; return a;
}

mt19937 mt(time(0));

const string drul = "DRUL";
string senw = "SENW";
//DRUL,or SENW
//int dx[4] = { 1,0,-1,0 };
//int dy[4] = { 0,1,0,-1 };

//------------------------------------

//verify: https://codeforces.com/contest/1827/submission/205921803

struct sparse_table {
private:
	int n;
	int tmp;
	vector<int> transtmp;
	vector<vector<P>> nodel;
public:
	void init(vector<P> ori) {
		n = ori.size();
		int cop = n;
		tmp = 0;
		while (cop > 0) {
			tmp++; cop >>= 1;
		}
		nodel.resize(tmp);
		rep(i, tmp)nodel[i].resize(n);
		rep(i, n) {
			nodel[0][i] = ori[i];
		}
		rep(j, tmp - 1) {
			rep(i, n) {
				if (i + (1 << j) < n) {
					nodel[j + 1][i] = min(nodel[j][i], nodel[j][i + (1 << j)]);
				}
			}
		}
		transtmp.resize(n + 1);
		int x = 1;
		int cnt = 0;
		rep1(i, n) {
			while (x * 2 <= i) {
				x *= 2; cnt++;
			}
			transtmp[i] = cnt;
		}
	}
	//[l,r)
	P query(int l, int r) {
		if (r - l == 0)return { mod,1 };
		int cnt = transtmp[r - l];
		return min(nodel[cnt][l], nodel[cnt][r - (1 << cnt)]);
	}
};

struct edge {
	int to; ll cost;
};
struct fastlca {
private:
	int n;
	vector<int> d;
	vector<P> vvs;
	vector<int> trans;
	sparse_table st;
public:
	fastlca(vector<vector<edge>>& G, int r = 0) {
		n = G.size();
		d.resize(n);
		trans.resize(n);
		function<void(int, int)> dfs = [&](int id, int fr) {
			trans[id] = vvs.size();
			vvs.push_back({ d[id],id });
			for (edge e : G[id])if (e.to != fr) {
				d[e.to] = d[id] + 1;
				dfs(e.to, id);
				vvs.push_back({ d[id],id });
			}
		};
		dfs(r, -1);
		st.init(vvs);
	}
	int lca(int a, int b) {
		a = trans[a], b = trans[b];
		if (a > b)swap(a, b);
		P p = st.query(a, b + 1);
		return p.second;
	}
	int dep(int a) {
		return d[a];
	}
};

void solve() {
	int n, m; cin >> n >> m;
	int q; cin >> q;
	vector<bool> isr(n);
	rep(i, m) {
		int x; cin >> x; x--;
		isr[x] = true;
	}
	vector<vector<edge>> G(n);
	rep(i, n - 1) {
		int a, b, c; cin >> a >> b >> c; a--; b--;
		G[a].push_back({ b,c });
		G[b].push_back({ a,c });
	}
	fastlca lc(G);
	vector<ll> dep(n);
	vector<ll> s(n);
	int tmp = 0;
	vector<int> trans(n);
	function<void(int, int,ll)> initdfs = [&](int id, int fr,ll cur) {
		trans[id] = tmp; tmp++;
		if (isr[id])cur = 0;
		s[id] = cur;
		for (edge e : G[id])if (e.to != fr) {
			dep[e.to] = dep[id] + e.cost;
			initdfs(e.to, id, cur + e.cost);
		}
	};
	initdfs(0, -1,0);
	rep(i, q) {
		int len; cin >> len;
		vector<int> v(len);
		rep(j, len) {
			cin >> v[j]; v[j]--;
		}
		auto comp = [&](int x, int y) {
			return trans[x] < trans[y];
		};
		//cout << "??\n";
		//coutarray(v);
		sort(all(v), comp);
		vector<ll> vl(len + 1);
		vector<ll> vr(len + 1);
		rep(j, len)vl[j + 1] = max(vl[j], s[v[j]]);
		per(j, len)vr[j] = max(vr[j + 1], s[v[j]]);
		int chk = -1;
		ll ma = -1;
		rep(i, len)if (ma < s[v[i]]) {
			ma = s[v[i]];
			chk = i;
		}
		ll cd = 0;
		int curv = v[chk];
		int cl = chk;
		int cr = chk + 1;
		ll ans = max(vl[cl], vr[cr]);
		ll adma = 0;
		while (cl > 0 || cr < len) {
			//cout << "!! " << curv << "\n";
			bool isl;
			if (cl == 0)isl = false;
			else if (cr == len)isl = true;
			else {
				int l1 = lc.lca(v[cl - 1], curv);
				int l2 = lc.lca(v[cr], curv);
				if (lc.dep(l1) < lc.dep(l2)) {
					isl = false;
				}
				else {
					isl = true;
				}
			}
			int adv;
			if (isl) {
				adv = v[cl - 1]; cl--;
			}
			else {
				adv = v[cr]; cr++;
			}
			//cout << "? " << adv << "\n";
			int nl = lc.lca(adv, curv);
			cd += dep[curv] - dep[nl];
			if (s[curv] != s[nl] + dep[curv] - dep[nl])break;
			if (s[adv] - s[nl] == dep[adv] - dep[nl]) {
				chmax(cd, dep[adv] - dep[nl]);
			}
			else {
				chmax(adma, s[adv]);
			}
			chmin(ans, max({ adma,cd,vl[cl],vr[cr] }));
			curv = nl;
		}
		//cout << "ans is ";
		cout << ans << "\n";
	}
}




signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed<<setprecision(10);
	//init_f();
	//init();
	//init2();
	//while(true)
	//expr();
	int t; cin >> t; rep(i, t)
		solve();
	return 0;
}

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

score: 100
Accepted
time: 3ms
memory: 11928kb

input:

2
12 2 4
1 9
1 2 1
2 3 4
3 4 3
3 5 2
2 6 2
6 7 1
6 8 2
2 9 5
9 10 2
9 11 3
1 12 10
3 3 7 8
4 4 5 7 8
4 7 8 10 11
3 4 5 12
3 2 3
1 2
1 2 1
1 3 1
1 1
2 1 2
3 1 2 3

output:

4
5
3
8
0
0
0

result:

ok 7 lines

Test #2:

score: 0
Accepted
time: 861ms
memory: 61624kb

input:

522
26 1 3
1
1 4 276455
18 6 49344056
18 25 58172365
19 9 12014251
2 1 15079181
17 1 50011746
8 9 2413085
23 24 23767115
22 2 26151339
26 21 50183935
17 14 16892041
9 26 53389093
1 20 62299200
24 18 56114328
11 2 50160143
6 26 14430542
16 7 32574577
3 16 59227555
3 15 8795685
4 12 5801074
5 20 57457...

output:

148616264
148616264
0
319801028
319801028
255904892
317070839
1265145897
1265145897
1072765445
667742619
455103436
285643094
285643094
285643094
317919339
0
785245841
691421476
605409472
479058444
371688030
303203698
493383271
919185207
910180170
919185207
121535083
181713164
181713164
181713164
181...

result:

ok 577632 lines

Extra Test:

score: 0
Extra Test Passed