Submission #202633

# Submission time Handle Problem Language Result Execution time Memory
202633 2020-02-17T11:36:49 Z theStaticMind Designated Cities (JOI19_designated_cities) C++14
23 / 100
1992 ms 63756 KB
#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;
            }
      }
      if(n < 3000){
      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

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++){
                      ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 15 ms 17656 KB Output is correct
2 Correct 15 ms 17656 KB Output is correct
3 Correct 15 ms 17656 KB Output is correct
4 Correct 15 ms 17656 KB Output is correct
5 Correct 14 ms 17656 KB Output is correct
6 Correct 14 ms 17660 KB Output is correct
7 Correct 14 ms 17656 KB Output is correct
8 Correct 15 ms 17660 KB Output is correct
9 Correct 15 ms 17656 KB Output is correct
10 Correct 15 ms 17656 KB Output is correct
11 Correct 16 ms 17656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 17656 KB Output is correct
2 Correct 1279 ms 41124 KB Output is correct
3 Correct 1940 ms 60296 KB Output is correct
4 Correct 1382 ms 41096 KB Output is correct
5 Execution timed out 333 ms 38536 KB Time limit exceeded (wall clock)
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 17656 KB Output is correct
2 Correct 1367 ms 40972 KB Output is correct
3 Correct 1992 ms 63756 KB Output is correct
4 Correct 1383 ms 41096 KB Output is correct
5 Correct 860 ms 40844 KB Output is correct
6 Correct 1562 ms 44172 KB Output is correct
7 Correct 526 ms 41468 KB Output is correct
8 Execution timed out 424 ms 47808 KB Time limit exceeded (wall clock)
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 17656 KB Output is correct
2 Correct 15 ms 17656 KB Output is correct
3 Correct 15 ms 17656 KB Output is correct
4 Correct 15 ms 17656 KB Output is correct
5 Correct 14 ms 17656 KB Output is correct
6 Correct 14 ms 17660 KB Output is correct
7 Correct 14 ms 17656 KB Output is correct
8 Correct 15 ms 17660 KB Output is correct
9 Correct 15 ms 17656 KB Output is correct
10 Correct 15 ms 17656 KB Output is correct
11 Correct 16 ms 17656 KB Output is correct
12 Correct 16 ms 17656 KB Output is correct
13 Correct 58 ms 17912 KB Output is correct
14 Correct 20 ms 18040 KB Output is correct
15 Correct 64 ms 17912 KB Output is correct
16 Correct 63 ms 17912 KB Output is correct
17 Correct 55 ms 17912 KB Output is correct
18 Correct 55 ms 17912 KB Output is correct
19 Correct 60 ms 18040 KB Output is correct
20 Correct 61 ms 17912 KB Output is correct
21 Correct 58 ms 17912 KB Output is correct
22 Correct 50 ms 17912 KB Output is correct
23 Correct 74 ms 17912 KB Output is correct
24 Correct 93 ms 17912 KB Output is correct
25 Correct 26 ms 18040 KB Output is correct
26 Correct 103 ms 18040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 17656 KB Output is correct
2 Correct 1279 ms 41124 KB Output is correct
3 Correct 1940 ms 60296 KB Output is correct
4 Correct 1382 ms 41096 KB Output is correct
5 Execution timed out 333 ms 38536 KB Time limit exceeded (wall clock)
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 17656 KB Output is correct
2 Correct 15 ms 17656 KB Output is correct
3 Correct 15 ms 17656 KB Output is correct
4 Correct 15 ms 17656 KB Output is correct
5 Correct 14 ms 17656 KB Output is correct
6 Correct 14 ms 17660 KB Output is correct
7 Correct 14 ms 17656 KB Output is correct
8 Correct 15 ms 17660 KB Output is correct
9 Correct 15 ms 17656 KB Output is correct
10 Correct 15 ms 17656 KB Output is correct
11 Correct 16 ms 17656 KB Output is correct
12 Correct 15 ms 17656 KB Output is correct
13 Correct 1279 ms 41124 KB Output is correct
14 Correct 1940 ms 60296 KB Output is correct
15 Correct 1382 ms 41096 KB Output is correct
16 Execution timed out 333 ms 38536 KB Time limit exceeded (wall clock)
17 Halted 0 ms 0 KB -