QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#74443#5439. Meet in the Middleheno239RE 7ms11728kbC++2010.9kb2023-02-01 19:38:342023-02-01 19:38:36

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-01 19:38:36]
  • 评测
  • 测评结果:RE
  • 用时:7ms
  • 内存:11728kb
  • [2023-02-01 19:38:34]
  • 提交

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;
constexpr ll mod = 998244353;
//constexpr ll mod = 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;

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>
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;
}
using ld = double;
//typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-10;
const ld pi = acosl(-1.0);
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 };
//-----------------------------------------

//verified with https://yukicoder.me/problems/no/1197
//O(NlogN)



struct edge {
	int to;
	ll cost;
};
struct pdata {
	int g, id;
	ll dist;
};
struct Centroid_Decomposition {
private:
	int n;
	vector<vector<edge>> G;


public:
	vector<vector<pdata>> vpd;
	vector<vector<int>> fG; int root;
	vector<vector<P>> oriv;
	vector<vector<ll>> orid;
	Centroid_Decomposition(int n_) {
		n = n_;
		G.resize(n);

		fG.resize(n); root = -1;
		vpd.resize(n);
		oriv.resize(n);
		orid.resize(n);
	}
	void add_edge(int a, int b,int c) {
		G[a].push_back({ b,c });
		G[b].push_back({ a,c });
	}

	void complete() {
		vector<int> exi(n, 0);
		vector<int> ori(n); rep(i, n)ori[i] = i;


		int tmp = 0;
		function<int(int, int, int&, int&)> szdfs = [&](int id, int fr, int& g, int& sz)->int {
			int res = 1;
			int ma = 0;
			for (edge e : G[id]) {
				if (tmp != exi[e.to])continue;
				if (e.to == fr)continue;
				int nex = szdfs(e.to, id, g, sz);
				ma = max(ma, nex);
				res += nex;
			}
			if (ma <= sz / 2 && sz - res <= sz / 2)g = id;
			return res;
		};

		function<int(vector<int>)> cdfs = [&](vector<int> v)->int {
			tmp++;
			if (v.empty())return 0;

			for (int id : v) {
				exi[id]++;
			}
			int g;
			int sz = v.size();
			szdfs(v[0], -1, g, sz);

			oriv[g].push_back({ g,-1 });
			orid[g].push_back(0);

			int ctmp = 0;
			for (edge e : G[g]) {
				if (!exi[e.to])continue;
				if (exi[e.to] != tmp)continue;
				vector<int> nex;
				function<void(int, int, ll)> dfs = [&](int id, int fr, ll d) {
					nex.push_back(id);
					vpd[id].push_back({ g,ctmp,d });
					oriv[g].push_back({ id,ctmp });
					orid[g].push_back(d);
					for (edge e : G[id]) {
						if (e.to == fr)continue;
						if (exi[e.to] != tmp)continue;
						dfs(e.to, id, d + e.cost);
					}
				};
				dfs(e.to, g,e.cost);


				int ng = cdfs(nex);
				fG[g].push_back(ng);
				ctmp++;
			}

			for (int id : v) {
				exi[id]--;
			}
			tmp--;
			return g;
		};
		root = cdfs(ori);

	}

};

using ar = array<ll, 3>;
/*
bool canadd(vector<ar>& v, ar& a) {
	if (v.empty())return true;
	rep(i, v.size()) {
		if (v[i][1] == a[1] && v[i][2] == a[2])return false;
	}
	bool s[2] = { true,true };
	rep1(i, v.size() - 1) {
		rep1(j, 2) {
			if (v[i][j] != v[0][j]) {
				s[j-1] = false;
			}
		}
	}
	if (s[0] && s[1]) {
		//assert(v.size()==1);
		return true;
	}
	else if (s[0]||s[1]) {
		int id = 0;
		if (s[1])id = 1;
		return v[0][id + 1] != a[id + 1];
	}
	else {

	}
	int c1[2] = {};
	{
		rep1(j, 2) {
			if (v[i][j] == a[j])c1[j - 1]++;
		}
	}
	rep(j, 2)if (c1[j] == 0 && s[j])return true;

}*/

struct ste {
	int lev;
	int x, y;
	vector<P> cp;
	ste() {
		lev = 0;
	}
};
bool add(ste &s, ar a) {
	if (s.lev == 0) {
		s.lev = 1;
		s.x = a[1]; s.y = a[2];
		return true;
	}
	else if (s.lev == 1) {
		if (s.x == a[1] && s.y == a[2])return false;
		if (s.x == a[1]) {
			s.lev = 2; return true;
		}
		else if (s.y == a[2]) {
			s.lev = 3; return true;
		}
		else {
			s.lev = 4;
			s.cp.push_back({ s.x,a[2] });
			s.cp.push_back({ a[1],s.y });
			return true;
		}
	}
	else if (s.lev == 2) {
		if (s.x == a[1])return false;
		s.lev = 4;
		s.cp.push_back({ s.x,a[2] });
	}
	else if (s.lev == 3) {
		if (s.y == a[2])return false;
		s.lev = 4;
		s.cp.push_back({ a[1],s.y });
	}
	else {
		vector<P> ncp;
		rep(i, s.cp.size()) {
			if (s.cp[i].first == a[1] || s.cp[i].second == a[2])ncp.push_back(s.cp[i]);
		}
		swap(s.cp, ncp);
		return s.cp.size() == ncp.size();
	}
}
void upd(vector<ar> &v, ar &a) {
	int chk = v.size();
	rep(i, v.size()) {
		if (v[i][0] <= a[0])chk = i;
	}
	vector<ar> res;
	ste s;
	rep(i, chk) {
		if (add(s, v[i]))res.push_back(v[i]);
	}
	if (add(s, a))res.push_back(a);
	Rep(i, chk, v.size()) {
		if (add(s, v[i]))res.push_back(v[i]);
	}
	swap(v, res);
}
ll query(vector<ar>& v,int c0,int c1) {
	rep(i, v.size()) {
		if (v[i][1] != c0 && v[i][2] != c1)return v[i][0];
	}
	return -INF;
}

void solve() {
	int n, q; cin >> n >> q;
	Centroid_Decomposition G(n), H(n);
	rep(i, n - 1) {
		int a, b, c; cin >> a >> b >> c; a--; b--;
		G.add_edge(a, b, c);
	}
	rep(i, n - 1) {
		int a, b, c; cin >> a >> b >> c; a--; b--;
		H.add_edge(a, b, c);
	}
	G.complete();
	H.complete();
	vector<int> qa(q), qb(q);
	rep(i, q) {
		cin >> qa[i] >> qb[i];
		qa[i]--; qb[i]--;
	}
	vector<vector<int>>qs(n);
	rep(i, q)qs[qb[i]].push_back(i);
	auto& fG = H.fG;
	auto& oriv = H.oriv;
	auto& orid = H.orid;
	vector<vector<ar>> mem(n);
	auto addmem = [&](int id, int col, ll d) {
		ar nw;
		nw = { d,-1,col };
		upd(mem[id], nw);
		for (auto pre : G.vpd[id]) {
			int g = pre.g;
			int c = pre.id;
			ll nd = d + pre.dist;
			nw = { nd,c,col };
			upd(mem[g], nw);
		}
	};
	auto ermem = [&](int id) {
		mem[id].clear();
		for (auto pre : G.vpd[id]) {
			mem[pre.g].clear();
		}
	};
	auto memquery = [&](int id, int col) {
		ll res = -INF;
		chmax(res, query(mem[id], -2, col));
		for (auto pre : G.vpd[id]) {
			int g = pre.g;
			int c = pre.id;
			chmax(res, query(mem[g], c, col)+pre.dist);
		}
		return res;
	};
	vector<ll> ans(q, 0);
	rep(i, n) {
		rep(j, oriv[i].size()) {
			int id = oriv[i][j].first;
			int col = oriv[i][j].second;
			ll d = orid[i][j];
			addmem(id, col, d);
		}
		rep(j, oriv[i].size()) {
			int id = oriv[i][j].first;
			int col = oriv[i][j].second;
			if (col < 0)col--;
			for (auto qid : qs[id]) {
				ll val = memquery(qa[qid], col);
				val += orid[i][j];
				//cout << "?? " << val << "\n";
				chmax(ans[qid], val);
			}
		}
		rep(j, oriv[i].size()) {
			int id = oriv[i][j].first;
			ermem(id);
		}
	}
	rep(i, q)cout << ans[i] << "\n";
}


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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 11572kb

input:

3 4
1 2 1
2 3 2
1 2 2
2 3 1
1 1
1 2
2 1
2 2

output:

6
4
5
3

result:

ok 4 number(s): "6 4 5 3"

Test #2:

score: 0
Accepted
time: 7ms
memory: 11612kb

input:

2 1
1 2 1
1 2 1
1 1

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 3ms
memory: 11728kb

input:

2 1
1 2 1
1 2 1
1 2

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: -100
Runtime Error

input:

10000 50000
8101 5996 108427744
5996 7605 870838849
5996 5599 603303696
8101 3006 339377417
8101 6463 442516687
6463 5560 109174079
5560 4063 127596224
3006 1682 947915262
5996 1986 130416311
6463 5455 279771516
6463 2634 516688796
4063 3034 217886863
7605 5092 742375061
5599 2266 193804402
5092 140...

output:


result: