Submission #202629

#TimeUsernameProblemLanguageResultExecution timeMemory
202629theStaticMindDesignated Cities (JOI19_designated_cities)C++14
23 / 100
2088 ms47428 KiB
#include<bits/stdc++.h> #define pb push_back #define ii pair<int,int> #define all(x) (x).begin(),(x).end() #define INF 100000000000000000 #define modulo 1000000007 #define mod 998244353 #define int long long int using namespace std; vector<int> adj[200005]; vector<int> val(200005); vector<int> Ans(200005, 0); vector<int> sub(200005, 1); vector<int> anc(200005, 0); vector<pair<ii, int> > V(200005); vector<int> P(200005, 0); vector<bool> mark(200005, false); vector<bool> vis(200005, false); unordered_map<int, int> cost; ii Q = {-INF, -1}; int S = 0; int _hash(ii a){ return a.first * 1e6 + a.second; } void dfs1(int x, int pre, int& v){ for(int i = 0; i < adj[x].size(); i++){ int y = adj[x][i]; if(y == pre) continue; v += cost[_hash({y, x})]; dfs1(y, x, v); } } void dfs2(int x, int pre, int v){ val[x] = v; for(int i = 0; i < adj[x].size(); i++){ int y = adj[x][i]; if(y == pre) continue; dfs2(y, x, v - cost[_hash({y, x})] + cost[_hash({x, y})]); } } void dfs3(int x, int pre){ sub[x] = 1; for(int i = 0; i < adj[x].size(); i++){ int y = adj[x][i]; if(y == pre || mark[y]) continue; dfs3(y, x); sub[x] += sub[y]; } } void dfs4(int x, int pre, int sum, ii& mx){ if(sum > mx.first){ mx = {sum, x}; } for(int i = 0; i < adj[x].size(); i++){ int y = adj[x][i]; if(y == pre || mark[y]) continue; dfs4(y, x, sum + cost[_hash({x, y})], mx); } } void dfs5(int x, int pre){ for(int i = 0; i < adj[x].size(); i++){ int y = adj[x][i]; if(y == pre) continue; dfs5(y, x); anc[y] = x; } } void dfs6(int x, int pre, int value){ for(auto y : adj[x]){ if(vis[y] || pre == y)continue; dfs6(y, x, value + cost[_hash({x, y})]); } if(adj[x].size() == 1 && pre != -1){ if(Q.first < value){ Q = {value, x}; } } } int find_centroid(int x, int pre, int sum){ for(int i = 0; i < adj[x].size(); i++){ int y = adj[x][i]; if(y == pre || mark[y])continue; if(sub[y] > sum / 2)return find_centroid(y, x, sum); } return x; } void solve(int x){ dfs3(x, -1); x = find_centroid(x, -1, sub[x]); ii mx = {0, x}; pair <ii, int> ans = {{x, x}, 0}; for(int i = 0; i < adj[x].size(); i++){ int y = adj[x][i]; if(mark[y])continue; ii temp = {cost[_hash({x, y})], y}; dfs4(y, x, cost[_hash({x, y})], temp); if(ans.second < mx.first + temp.first){ ans.first = {mx.second, temp.second}; ans.second = mx.first + temp.first; } if(temp.first > mx.first){ swap(temp, mx); } } V[x] = ans; mark[x] = true; for(int i = 0; i < adj[x].size(); i++){ int y =adj[x][i]; if(mark[y])continue; solve(y); } } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); // freopen("q.gir","r",stdin); // freopen("q.cik","w",stdout); int n; cin >> n; for(int i = 0; i < n - 1; i++){ int x, y, a, b; cin >> x >> y >> a >> b; adj[x].pb(y); adj[y].pb(x); cost[_hash({x, y})] = a; cost[_hash({y, x})] = b; S += a + b; } int w = 0; dfs1(1, 0, w); dfs2(1, 0, w); Ans[1] = S - *max_element(all(val)); solve(1); Ans[2] = INF; int X, Y; for(int i = 1; i <= n; i++){ if((S - val[i]) - V[i].second < Ans[2]){ X = V[i].first.first; Y = V[i].first.second; Ans[2] = (S - val[i]) - V[i].second; } } dfs5(X, 0); int t = Y; while(t != X){ vis[t] = true; t = anc[t]; } vis[X] = true; for(int i = 1; i <= n; i++){ if(vis[i]) dfs6(i, -1, 0); } for(int q = 3; Ans[q - 1] != 0; q++){ Ans[q] = Ans[q - 1] - Q.first; int x = Q.second; Q = {-INF, -1}; t = x; while(!vis[t]){ vis[t] = true; t = anc[t]; } for(int i = 1; i <= n; i++){ if(vis[i]) dfs6(i, -1, 0); } } int q; cin >> q; while(q--){ int k; cin >> k; cout << Ans[k] << "\n"; } }

Compilation message (stderr)

designated_cities.cpp: In function 'void dfs1(long long int, long long int, long long int&)':
designated_cities.cpp:27:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for(int i = 0; i < adj[x].size(); i++){
                      ~~^~~~~~~~~~~~~~~
designated_cities.cpp: In function 'void dfs2(long long int, long long int, long long int)':
designated_cities.cpp:36:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for(int i = 0; i < adj[x].size(); i++){
                      ~~^~~~~~~~~~~~~~~
designated_cities.cpp: In function 'void dfs3(long long int, long long int)':
designated_cities.cpp:44:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for(int i = 0; i < adj[x].size(); i++){
                      ~~^~~~~~~~~~~~~~~
designated_cities.cpp: In function 'void dfs4(long long int, long long int, long long int, std::pair<long long int, long long int>&)':
designated_cities.cpp:55:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for(int i = 0; i < adj[x].size(); i++){
                      ~~^~~~~~~~~~~~~~~
designated_cities.cpp: In function 'void dfs5(long long int, long long int)':
designated_cities.cpp:62:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for(int i = 0; i < adj[x].size(); i++){
                      ~~^~~~~~~~~~~~~~~
designated_cities.cpp: In function 'long long int find_centroid(long long int, long long int, long long int)':
designated_cities.cpp:81:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for(int i = 0; i < adj[x].size(); i++){
                      ~~^~~~~~~~~~~~~~~
designated_cities.cpp: In function 'void solve(long long int)':
designated_cities.cpp:93:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for(int i = 0; i < adj[x].size(); i++){
                      ~~^~~~~~~~~~~~~~~
designated_cities.cpp:108:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for(int i = 0; i < adj[x].size(); i++){
                      ~~^~~~~~~~~~~~~~~
designated_cities.cpp: In function 'int32_t main()':
designated_cities.cpp:146:15: warning: 'X' may be used uninitialized in this function [-Wmaybe-uninitialized]
       while(t != 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...