1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
#include <bits/stdc++.h> using namespace std; #define resetIO(x) \ freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout) #define debug(fmt, ...) \ fprintf(stderr, "[%s:%d] " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__) template <class _Tp> inline _Tp& read(_Tp& x) { bool sign = false; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) sign |= (ch == '-'); for (x = 0; isdigit(ch); ch = getchar()) x = x * 10 + (ch ^ 48); return sign ? (x = -x) : x; } template <class _Tp> inline void write(_Tp x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar((x % 10) ^ 48); } bool m_be; using ll = long long; const int MAXN = 1e5 + 10; const int INF = 0x3f3f3f3f; int n, m, tp, fa[MAXN], lp[MAXN], rp[MAXN], ans[MAXN]; vector<int> g[MAXN]; inline void chkmin(int& x, int y) { (x > y) && (x = y); } inline void chkmax(int& x, int y) { (x < y) && (x = y); } int dfs(int u, int mxl, int mnr) { int ret = max({0, lp[u] - mnr, mxl - rp[u]}); chkmax(mxl, lp[u]), chkmin(mnr, rp[u]); for (auto v : g[u]) chkmax(ret, dfs(v, mxl, mnr)); return ret; } bool m_ed; signed main() { int cas; read(cas), read(tp); while (cas--) { read(n), m = 0; for (int i = 1; i <= n; ++i) g[i].clear(); for (int i = 2; i <= n; ++i) read(fa[i]), g[fa[i]].push_back(i); for (int i = 1; i <= n; ++i) read(lp[i]), read(rp[i]); int mxl = 0, mnr = INF; for (int i = 1; i <= n; ++i) chkmax(mxl, lp[i]), chkmin(mnr, rp[i]); int mn = dfs(1, 0, INF), ansp = 0, answ = INF; for (int i = (mxl + mnr) / 2; i <= (mxl + mnr + 1) / 2; ++i) { int curw = mn; if (i < mxl) chkmax(curw, mxl - i); if (i > mnr) chkmax(curw, i - mnr); if (curw < answ) answ = curw, ansp = i; } write(answ), putchar('\n'); if (tp) { for (int i = 1; i <= n; ++i) { if (lp[i] <= ansp && ansp <= rp[i]) ans[i] = ansp; else if (ansp < lp[i]) ans[i] = lp[i]; else if (rp[i] < ansp) ans[i] = rp[i]; write(ans[i]), putchar(" \n"[i == n]); } } } return 0; }
|