Submission #1296615

#TimeUsernameProblemLanguageResultExecution timeMemory
1296615SzymonKrzywdaDesignated Cities (JOI19_designated_cities)C++20
6 / 100
2096 ms23660 KiB
#include <iostream> #include <vector> using namespace std; using ll = long long; const ll MAXN = 2e5 + 7; vector<int> graf[MAXN]; ll koszt[MAXN][2]; bool wziete[MAXN][2]; int parent[MAXN]; ll ans[MAXN]; ll ile[MAXN]; void dfs(int v, int p) { parent[v] = p; for (int s : graf[v]) { if (s != p) dfs(s, v); } } void dfs2(int v, int p, ll aktkoszt) { ile[v] = aktkoszt; for (int s : graf[v]) { if (s != p) { aktkoszt -= !wziete[s][0] * koszt[s][0]; aktkoszt += !wziete[s][1] * koszt[s][1]; dfs2(s, v, aktkoszt); aktkoszt -= !wziete[s][1] * koszt[s][1]; aktkoszt += !wziete[s][0] * koszt[s][0]; } } } void zaznacz(int v, int p, int nr) { wziete[v][nr] = 1; for (int s : graf[v]) { if (s != p) { zaznacz(s, v, s == parent[v]); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll n, a, b, c , d; cin >> n; vector<pair<pair<ll, ll>, pair<ll, ll>>> kr; for (ll i = 0; i < n - 1; i++) { cin >> a >> b >> c >> d; graf[a].push_back(b); graf[b].push_back(a); kr.push_back({{a, b}, {c, d}}); } dfs(1, 1); for (auto & k : kr) { if (parent[k.first.second] == k.first.first) { swap(k.first.second, k.first.first); swap(k.second.first, k.second.second); } //cout << k.first.first << ' ' << k.first.second << ' ' << k.second.first << ' ' << k.second.second << '\n'; koszt[k.first.first][0] = k.second.first; koszt[k.first.first][1] = k.second.second; } for (int v = 1; v <= n; v++) { for (int i = 1; i <= n; i++) { wziete[i][0] = 0; wziete[i][1] = 0; } ll aktv1 = 0, aktkoszt = 0; for (int j = 2; j <= n; j++) { aktv1 += koszt[j][0] * !wziete[j][0]; } dfs2(1, 1, aktv1); aktkoszt += ile[v]; zaznacz(v, v, 1); ans[1] = max(ans[1], aktkoszt); for (int i = 2; i <= n - 1; i++) { ll aktv1 = 0; for (int j = 2; j <= n; j++) { aktv1 += koszt[j][0] * !wziete[j][0]; } dfs2(1, 1, aktv1); ll maxi = 2; for (int j = 2; j <= n; j++) { //cout << i << ' ' << j << ' ' << ile[j] << ' ' << wziete[j][0] << ' ' << wziete[j][1] << '\n'; if (ile[j] > ile[maxi]) maxi = j; } //cout << maxi << ' ' << ile[maxi] << '\n'; aktkoszt += ile[maxi]; ans[i] = max(ans[i], aktkoszt); zaznacz(maxi, maxi, 1); } } ll suma = 0; for (int i = 1; i <= n; i++) suma += koszt[i][0] + koszt[i][1]; ans[n] = suma; //for (int i = 1; i <= n; i++) cout << suma - ans[i] << ' '; int q, val; cin >> q; while (q--) { cin >> val; cout << suma - ans[val] << '\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...