Submission #376147

#TimeUsernameProblemLanguageResultExecution timeMemory
376147Kevin_Zhang_TWDesignated Cities (JOI19_designated_cities)C++17
7 / 100
293 ms25580 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l) == r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
const int MAX_N = 200010;
const ll inf = 1ll << 59;

int n, q;
ll dprt[MAX_N], dpson[MAX_N], res[MAX_N];
int qv[MAX_N];
vector<pair<int,int>> edge[MAX_N];

ll dfs1(int x, int lst = -1) {
	for (auto [u, w] : edge[x]) if (u != lst) 
		dpson[x] += w + dfs1(u, x);
	return dpson[x];
}
void dfs2(int x, int lst = -1, ll upson = 0) {
	for (auto [u, w] : edge[x]) if (u == lst)
		upson += w;
	dprt[x] = upson + dpson[x];
	DE(x, dprt[x]);
	for (auto [u, w] : edge[x]) if (u != lst) {
		ll val = dprt[x] - w - dpson[u];
		dfs2(u, x, val);
	}
}

int32_t main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	cin >> n;
	for (int a, b, c, d, i = 1;i < n;++i) {
		cin >> a >> b >> c >> d;
		edge[a].pb(b, c);
		edge[b].pb(a, d);
	}

	dfs1(1);
	dfs2(1);

	cin >> q;
	for (int i = 0;i < q;++i)
		cin >> qv[i];

	if (q > 1) return -1;

	for (int i = 0;i < q;++i) {
		res[i] = inf;
	   	if (qv[i] == 1)
			res[i] = *min_element(dprt+1, dprt+1+n);
		if (qv[i] == 2) {
			for (int x = 1;x <= n;++x)
				for (auto [u, w] : edge[x])
					chmin(res[i], res[u] + w);
		}
	}

	for (int i = 0;i < q;++i)
		cout << res[i] << '\n';

}

Compilation message (stderr)

designated_cities.cpp: In function 'void dfs2(int, int, ll)':
designated_cities.cpp:14:17: warning: statement has no effect [-Wunused-value]
   14 | #define DE(...) 0
      |                 ^
designated_cities.cpp:34:2: note: in expansion of macro 'DE'
   34 |  DE(x, dprt[x]);
      |  ^~
#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...