제출 #410280

#제출 시각아이디문제언어결과실행 시간메모리
410280amoo_safarDesignated Cities (JOI19_designated_cities)C++17
100 / 100
713 ms59544 KiB
// vaziat meshki-ghermeze !
#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'

using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;

const ll Mod = 1000000007LL;
const int N = 2e5 + 10;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;

set<pll> G[N];
ll ans[N];

set<pll> st;
ll mn = Inf;
ll sum = 0;
void DFS(int u, int p, ll in){
	mn = min(mn, in);
	for(auto [adj, w] : G[u]){
		if(adj == p) continue;
		int rw = G[adj].lower_bound(pll(u, -1)) -> S;
		sum += rw;
		DFS(adj, u, in + w - rw);
	}
}

int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int n;
	cin >> n;
	int u, v, c, d;
	for(int i = 1; i < n; i++){
		cin >> u >> v >> c >> d;
		swap(c, d);
		G[u].insert({v, c});
		G[v].insert({u, d});
	}
	int cnt = 0;
	for(int i = 1; i <= n; i++){
		if((int) G[i].size() == 1){
			cnt ++;
			st.insert({G[i].begin() -> S, i});
			// cerr << "! " << G[i].begin() -> S << ' ' << i << '\n';
		}
	}
	DFS(1, -1, 0);
	// debug(sum);
	ans[1] = sum + mn;
	ll sm = 0;
	for(int i = n; i >= 2; i--){
		while(cnt > i){
			pll fr = *st.begin();
			// cerr << "### " << fr.F << ' ' << fr.S << '\n';
			st.erase(fr);
			int adj = G[fr.S].begin() -> F;
			G[fr.S].clear();
			auto it = G[adj].lower_bound(pll(fr.S, -1));
			G[adj].erase(it);
			if(G[adj].size() == 1){

				st.insert({fr.F + G[adj].begin() -> S, adj});
			} else {
				cnt --;
				sm += fr.F;
			}
		}
		ans[i] = sm;
	}
	// cout << '\n';
	int q;
	cin >> q;
	for(int i = 1; i <= q; i++){
		cin >> v;
		// assert(v > 1);
		cout << ans[v] << '\n';
	}
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...