Submission #520878

# Submission time Handle Problem Language Result Execution time Memory
520878 2022-01-31T10:45:15 Z fatemetmhr Designated Cities (JOI19_designated_cities) C++17
100 / 100
471 ms 79892 KB
//  ~Be name khoda~  //

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define pb       push_back
#define mp       make_pair
#define all(x)   x.begin(), x.end()
#define fi       first
#define se       second
#define cl       clear
#define endll    '\n'

const int maxn  =  1e6   + 10;
const int maxn5 =  1e6   + 10;
const int maxnt =  4e6   + 10;
const int maxn3 =  1e3   + 10;
const int mod   =  1e9   +  7;
const ll  inf   =  2e18;

int n;
ll ans1 = -1;
ll ans = -1;
int ccnt = 0, den[maxn5];
bool mark[maxn5];
vector <pair<int, int>> adj[maxn5];
ll c[maxn5], best1[maxn5], up[maxn5];
int par[maxn5], ti = -1, st[maxn5], ft[maxn5], ver[maxn5];
ll edgepar[maxn5], sumall[maxn5], lazy[maxnt], out[maxn5];
ll ex[maxn5], neg[maxn5];
pair<ll, int> farr[maxn5], mx[maxnt];
pair<int, int> req[maxn5];


inline void dfsdown(int v, int par){
	best1[v] = 0;
	for(auto [u, id] : adj[v]) if(u != par){
		dfsdown(u, v);
		best1[u] += c[id];
		best1[v] += best1[u];
		ex[u] = c[id];
	}
	return;
}

inline void dfsup(int v, int par){
	ll sum = 0;
	for(auto [u, id] : adj[v]){
		if(u != par)
			sum += best1[u];
		else
			up[v] += c[id];
	}
	
	for(auto [u, id] : adj[v]) if(u != par){
		up[u] = up[v] + sum - best1[u];
		dfsup(u, v);
	}
	return;
}

inline void dfsfar1(int v, int par){
	mx[v] = {0, v};
	den[v] = v;
	for(auto [u, id] : adj[v]) if(u != par){
		dfsfar1(u, v);
		if(mx[v] <= mx[u]){
			mx[v] = mx[u];
			den[v] = u;
		}
	}
	for(auto [u, id] : adj[v]) if(u == par){
		mx[v].fi += c[id];
		neg[v] = c[id];
	}
	//cout << "so right " << v << ' ' << mx[v].fi << ' ' << mx[v].se << ' ' << den[v] << endl;
	return;
}

inline void dfsfar2(int v, int par){
	ll ex = 0;
	for(auto [u, id] : adj[v]){
		if(u != par && u != den[v]){
			farr[u] = max(farr[v], {mx[v].fi - neg[v], mx[v].se});
			farr[u].fi += c[id];
			dfsfar2(u, v);
		}
		else if(u == den[v])
			ex = c[id];
	}
	//cout << "ok " << v << ' ' << farr[v].fi << ' ' << farr[v].se << endl;
	if(den[v] == v)
		return;
	pair <ll, int> tmp = farr[v];
	for(auto [u, id] : adj[v]) if(u != par && u != den[v]){
		tmp = max(tmp, mx[u]);
	}
	farr[den[v]] = tmp;
	farr[den[v]].fi += ex;
	dfsfar2(den[v], v);

	return;
}

inline void dfs(int v, int parr){
	par[v] = parr;
	st[v] = ++ti;
	ver[ti] = v;
	for(auto [u, id] : adj[v]) if(u == parr){
		sumall[v] += c[id];
		edgepar[v] = c[id];
	}
	for(auto [u, id] : adj[v]){
		if(u != parr){
			sumall[u] += sumall[v];
			dfs(u, v);
		}
	}
	ft[v] = ti;
	return;
}

inline void build(int l, int r, int v){
	if(r - l == 1){
		mx[v].se = ver[l];
		mx[v].fi = 0;
		if(adj[ver[l]].size() == 1){
			mx[v].fi = sumall[ver[l]];
		}
		return;
	}
	int mid = (l + r) >> 1;
	build(l, mid, v * 2);
	build(mid, r, v * 2 + 1);
	mx[v] = max(mx[v * 2], mx[v * 2 + 1]);
	return;
}

inline void add(int l, int r, int lq, int rq, ll val, int v){
	if(rq <= l || r <= lq)
		return;
	if(lq <= l && r <= rq){
		lazy[v] += val;
		mx[v].fi += val;
		return;
	}
	int mid = (l + r) >> 1;
	add(l, mid, lq, rq, val, v * 2);
	add(mid, r, lq, rq, val, v * 2 + 1);
	mx[v] = max(mx[v * 2], mx[v * 2 + 1]);
	mx[v].fi += lazy[v];
	return;
}

inline void cons(int v){
	if(mark[v]) 
		return;
	ans += edgepar[v];
	add(0, n, st[v], ft[v] + 1, -edgepar[v], 1);
	mark[v] = true;
	cons(par[v]);
	return;
}

inline ll find_1(){
	dfsdown(0, -1);
	dfsup(0, -1);
	ans1 = 0;
	for(int i = 0; i < n; i++){
		//cout << i << ' ' << best1[i] << ' ' << up[i] << '\n';
		best1[i] += up[i] - ex[i];
		ans1 = max(ans1, best1[i]);
		//cout << best1[i] << '\n';
	}
	//cout << "WELL " << ans1 << '\n';
	return ans1;
}

inline void find_2(){
	int r = 0;
	for(int i = 0; i < n; i++) if(adj[i].size() != 1)
		r = i;
	//cout << "rrrrrrrr " << r << endl;
	dfsfar1(r, -1);
	farr[r] = {0, 0};
	dfsfar2(r, -1);
	int optz = 0;
	for(int i = 0; i < n; i++) if(adj[i].size() == 1){
		ll val = best1[i] + farr[i].fi;
		//cout << "& " << i << ' ' << best1[i] << ' ' << farr[i].fi << ' ' << farr[i].se << endl;
		if(val >= ans){
			optz = i;
			ans = val;
		}
	}
	//cout << ans << endl;
	dfs(optz, -1);
	build(0, n, 1);
	mark[optz] = true;
	cons(farr[optz].se);
	ans -= farr[optz].fi;
	//cout << "allright " << optz << endl;
	return;
}

inline void upd(){
	if(ccnt == 0)
		return;
	int v = mx[1].se;
	cons(v);
	ccnt--;
	return;
}

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	
	cin >> n;
	int ind = 0;
	ll tot = 0;
	for(int i = 0; i < n - 1; i++){
		int a, b; cin >> a >> b >> c[ind] >> c[ind + 1];
		a--; b--;
		adj[a].pb({b, ind + 1});
		adj[b].pb({a, ind});
		tot += c[ind] + c[ind + 1];
		ind += 2;
	}
	ccnt = 0;
	for(int i = 0; i < n; i++) if(adj[i].size() == 1)
		ccnt++;
	int q; cin >> q;
	for(int i = 0; i < q; i++){
		cin >> req[i].fi;
		req[i].se = i;
	}
	if(n == 2){
		for(int i = 0; i < q; i++){
			if(req[i].fi == 1){
				cout << min(c[0], c[1]) << '\n';
			}
			else{
				cout << 0 << '\n';
			}
		}
		return 0;
	}
	sort(req, req + q);
	find_1();
	find_2();
	int last = 2;
	//cout << ans << endl;
	for(int i = 0; i < q; i++){
		if(req[i].fi == 1){
			out[req[i].se] = ans1;
			//cout << "well done " << i << ' ' << req[i].se << ' ' << out[req[i].se] << ' ' << ans1 << '\n';
		}
		else{
			while(last < req[i].fi){
				last++;
				upd();
			}
			out[req[i].se] = ans;
		}
	}
	for(int i = 0; i < q; i++)
		cout << tot - out[i] << '\n';
}

/*
5
2 1 5 5
3 2 4 5
4 2 4 3
5 4 5 5
1
2
*/

















# Verdict Execution time Memory Grader output
1 Correct 12 ms 23756 KB Output is correct
2 Correct 14 ms 23908 KB Output is correct
3 Correct 12 ms 23952 KB Output is correct
4 Correct 12 ms 23924 KB Output is correct
5 Correct 12 ms 23884 KB Output is correct
6 Correct 16 ms 23824 KB Output is correct
7 Correct 12 ms 23932 KB Output is correct
8 Correct 12 ms 23884 KB Output is correct
9 Correct 12 ms 23940 KB Output is correct
10 Correct 12 ms 23884 KB Output is correct
11 Correct 12 ms 23840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23756 KB Output is correct
2 Correct 312 ms 62940 KB Output is correct
3 Correct 403 ms 74824 KB Output is correct
4 Correct 309 ms 64156 KB Output is correct
5 Correct 294 ms 65576 KB Output is correct
6 Correct 357 ms 70544 KB Output is correct
7 Correct 248 ms 65596 KB Output is correct
8 Correct 414 ms 75116 KB Output is correct
9 Correct 169 ms 66124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23788 KB Output is correct
2 Correct 350 ms 62984 KB Output is correct
3 Correct 380 ms 75908 KB Output is correct
4 Correct 321 ms 65084 KB Output is correct
5 Correct 293 ms 65564 KB Output is correct
6 Correct 336 ms 70656 KB Output is correct
7 Correct 200 ms 65752 KB Output is correct
8 Correct 394 ms 74256 KB Output is correct
9 Correct 167 ms 65588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23756 KB Output is correct
2 Correct 14 ms 23908 KB Output is correct
3 Correct 12 ms 23952 KB Output is correct
4 Correct 12 ms 23924 KB Output is correct
5 Correct 12 ms 23884 KB Output is correct
6 Correct 16 ms 23824 KB Output is correct
7 Correct 12 ms 23932 KB Output is correct
8 Correct 12 ms 23884 KB Output is correct
9 Correct 12 ms 23940 KB Output is correct
10 Correct 12 ms 23884 KB Output is correct
11 Correct 12 ms 23840 KB Output is correct
12 Correct 13 ms 23768 KB Output is correct
13 Correct 14 ms 24308 KB Output is correct
14 Correct 14 ms 24396 KB Output is correct
15 Correct 15 ms 24284 KB Output is correct
16 Correct 14 ms 24268 KB Output is correct
17 Correct 14 ms 24268 KB Output is correct
18 Correct 14 ms 24364 KB Output is correct
19 Correct 14 ms 24316 KB Output is correct
20 Correct 14 ms 24368 KB Output is correct
21 Correct 15 ms 24264 KB Output is correct
22 Correct 14 ms 24352 KB Output is correct
23 Correct 15 ms 24324 KB Output is correct
24 Correct 14 ms 24268 KB Output is correct
25 Correct 14 ms 24364 KB Output is correct
26 Correct 16 ms 24268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23756 KB Output is correct
2 Correct 312 ms 62940 KB Output is correct
3 Correct 403 ms 74824 KB Output is correct
4 Correct 309 ms 64156 KB Output is correct
5 Correct 294 ms 65576 KB Output is correct
6 Correct 357 ms 70544 KB Output is correct
7 Correct 248 ms 65596 KB Output is correct
8 Correct 414 ms 75116 KB Output is correct
9 Correct 169 ms 66124 KB Output is correct
10 Correct 13 ms 23788 KB Output is correct
11 Correct 350 ms 62984 KB Output is correct
12 Correct 380 ms 75908 KB Output is correct
13 Correct 321 ms 65084 KB Output is correct
14 Correct 293 ms 65564 KB Output is correct
15 Correct 336 ms 70656 KB Output is correct
16 Correct 200 ms 65752 KB Output is correct
17 Correct 394 ms 74256 KB Output is correct
18 Correct 167 ms 65588 KB Output is correct
19 Correct 14 ms 23760 KB Output is correct
20 Correct 403 ms 69628 KB Output is correct
21 Correct 402 ms 76104 KB Output is correct
22 Correct 383 ms 68204 KB Output is correct
23 Correct 328 ms 68008 KB Output is correct
24 Correct 319 ms 64516 KB Output is correct
25 Correct 383 ms 67504 KB Output is correct
26 Correct 310 ms 64520 KB Output is correct
27 Correct 340 ms 69572 KB Output is correct
28 Correct 384 ms 70600 KB Output is correct
29 Correct 337 ms 66132 KB Output is correct
30 Correct 300 ms 65340 KB Output is correct
31 Correct 278 ms 69776 KB Output is correct
32 Correct 398 ms 73892 KB Output is correct
33 Correct 187 ms 69252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23756 KB Output is correct
2 Correct 14 ms 23908 KB Output is correct
3 Correct 12 ms 23952 KB Output is correct
4 Correct 12 ms 23924 KB Output is correct
5 Correct 12 ms 23884 KB Output is correct
6 Correct 16 ms 23824 KB Output is correct
7 Correct 12 ms 23932 KB Output is correct
8 Correct 12 ms 23884 KB Output is correct
9 Correct 12 ms 23940 KB Output is correct
10 Correct 12 ms 23884 KB Output is correct
11 Correct 12 ms 23840 KB Output is correct
12 Correct 15 ms 23756 KB Output is correct
13 Correct 312 ms 62940 KB Output is correct
14 Correct 403 ms 74824 KB Output is correct
15 Correct 309 ms 64156 KB Output is correct
16 Correct 294 ms 65576 KB Output is correct
17 Correct 357 ms 70544 KB Output is correct
18 Correct 248 ms 65596 KB Output is correct
19 Correct 414 ms 75116 KB Output is correct
20 Correct 169 ms 66124 KB Output is correct
21 Correct 13 ms 23788 KB Output is correct
22 Correct 350 ms 62984 KB Output is correct
23 Correct 380 ms 75908 KB Output is correct
24 Correct 321 ms 65084 KB Output is correct
25 Correct 293 ms 65564 KB Output is correct
26 Correct 336 ms 70656 KB Output is correct
27 Correct 200 ms 65752 KB Output is correct
28 Correct 394 ms 74256 KB Output is correct
29 Correct 167 ms 65588 KB Output is correct
30 Correct 13 ms 23768 KB Output is correct
31 Correct 14 ms 24308 KB Output is correct
32 Correct 14 ms 24396 KB Output is correct
33 Correct 15 ms 24284 KB Output is correct
34 Correct 14 ms 24268 KB Output is correct
35 Correct 14 ms 24268 KB Output is correct
36 Correct 14 ms 24364 KB Output is correct
37 Correct 14 ms 24316 KB Output is correct
38 Correct 14 ms 24368 KB Output is correct
39 Correct 15 ms 24264 KB Output is correct
40 Correct 14 ms 24352 KB Output is correct
41 Correct 15 ms 24324 KB Output is correct
42 Correct 14 ms 24268 KB Output is correct
43 Correct 14 ms 24364 KB Output is correct
44 Correct 16 ms 24268 KB Output is correct
45 Correct 14 ms 23760 KB Output is correct
46 Correct 403 ms 69628 KB Output is correct
47 Correct 402 ms 76104 KB Output is correct
48 Correct 383 ms 68204 KB Output is correct
49 Correct 328 ms 68008 KB Output is correct
50 Correct 319 ms 64516 KB Output is correct
51 Correct 383 ms 67504 KB Output is correct
52 Correct 310 ms 64520 KB Output is correct
53 Correct 340 ms 69572 KB Output is correct
54 Correct 384 ms 70600 KB Output is correct
55 Correct 337 ms 66132 KB Output is correct
56 Correct 300 ms 65340 KB Output is correct
57 Correct 278 ms 69776 KB Output is correct
58 Correct 398 ms 73892 KB Output is correct
59 Correct 187 ms 69252 KB Output is correct
60 Correct 12 ms 23760 KB Output is correct
61 Correct 425 ms 75516 KB Output is correct
62 Correct 457 ms 79724 KB Output is correct
63 Correct 423 ms 74056 KB Output is correct
64 Correct 442 ms 75480 KB Output is correct
65 Correct 431 ms 74096 KB Output is correct
66 Correct 429 ms 75460 KB Output is correct
67 Correct 437 ms 74240 KB Output is correct
68 Correct 423 ms 75576 KB Output is correct
69 Correct 455 ms 76280 KB Output is correct
70 Correct 442 ms 75348 KB Output is correct
71 Correct 394 ms 74624 KB Output is correct
72 Correct 357 ms 76096 KB Output is correct
73 Correct 471 ms 79892 KB Output is correct
74 Correct 295 ms 76604 KB Output is correct