Submission #202049

#TimeUsernameProblemLanguageResultExecution timeMemory
202049theStaticMindDesignated Cities (JOI19_designated_cities)C++14
16 / 100
1831 ms67084 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<pair<ii, int> > V(200005); vector<bool> mark(200005, false); unordered_map<int, int> cost; 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); } } 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; for(int i = 1; i <= n; i++)Ans[2] = min(Ans[2], (S - val[i]) - V[i].second); 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:23: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:32: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:40: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:51:25: 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:58: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:70:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for(int i = 0; i < adj[x].size(); i++){
                      ~~^~~~~~~~~~~~~~~
designated_cities.cpp:85:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for(int i = 0; i < adj[x].size(); i++){
                      ~~^~~~~~~~~~~~~~~
#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...