답안 #202639

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
202639 2020-02-17T12:31:06 Z theStaticMind Designated Cities (JOI19_designated_cities) C++14
23 / 100
2000 ms 73608 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<pair<int,ii> > P(200005, {0,{0, 0}});
vector<int> up(200005, 0);
vector<bool> mark(200005, false);
vector<bool> vis(200005, false);
unordered_map<int, int> cost;
priority_queue<pair<int, ii> > Q;
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,int& sum){
      for(int i = 0; i < adj[x].size(); i++){
            int y = adj[x][i];
            if(y == pre) continue;
            dfs5(y, x, sum);
            anc[y] = x;
            sum += cost[_hash({x, y})];
      }
}
void dfs6(int x, int pre, int value){
      for(auto y : adj[x]){
            if(pre == y)continue;
            up[y] = up[x] + cost[_hash({x, y})];
            dfs6(y, x, value + cost[_hash({x, y})]);
            if(P[y].first > P[x].first) P[x] = P[y];
            if(pre == -1)Q.push(P[y]);
      }
      P[x].second.first = x;
      if(adj[x].size() == 1 && pre != -1){
            P[x] = {value, {x, 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;
            }
      }
      int last = 0;
      dfs5(X, 0, last);
      dfs6(X, -1, 0);
      vis[0] = true;
      for(int q = 2; Ans[q - 1] != 0; q++){
            while(vis[Q.top().second.first]){
                  Q.pop();
                  assert(!Q.empty());
            }
            Ans[q] = last - Q.top().first;
            last = Ans[q];
            int t = Q.top().second.second;
            Q.pop();
            while(!vis[t]){
                  vis[t] = true;
                  for(auto y : adj[t]){
                        Q.push({P[y].first - up[t], P[y].second});
                  }
                  t = anc[t];
            }
      }
      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:28: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:37: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:45: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:56: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, long long int&)':
designated_cities.cpp:63: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:85: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:97:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for(int i = 0; i < adj[x].size(); i++){
                      ~~^~~~~~~~~~~~~~~
designated_cities.cpp:112: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:140:14: warning: variable 'Y' set but not used [-Wunused-but-set-variable]
       int X, Y;
              ^
designated_cities.cpp:150:11: warning: 'X' may be used uninitialized in this function [-Wmaybe-uninitialized]
       dfs6(X, -1, 0);
       ~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 22392 KB Output is correct
2 Correct 20 ms 22392 KB Output is correct
3 Correct 21 ms 22392 KB Output is correct
4 Correct 19 ms 22392 KB Output is correct
5 Correct 18 ms 22392 KB Output is correct
6 Correct 18 ms 22264 KB Output is correct
7 Correct 20 ms 22264 KB Output is correct
8 Correct 18 ms 22264 KB Output is correct
9 Correct 19 ms 22264 KB Output is correct
10 Correct 18 ms 22264 KB Output is correct
11 Correct 18 ms 22392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 22392 KB Output is correct
2 Correct 1721 ms 55564 KB Output is correct
3 Execution timed out 2091 ms 73608 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 22264 KB Output is correct
2 Correct 1753 ms 55432 KB Output is correct
3 Execution timed out 2085 ms 68744 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 22392 KB Output is correct
2 Correct 20 ms 22392 KB Output is correct
3 Correct 21 ms 22392 KB Output is correct
4 Correct 19 ms 22392 KB Output is correct
5 Correct 18 ms 22392 KB Output is correct
6 Correct 18 ms 22264 KB Output is correct
7 Correct 20 ms 22264 KB Output is correct
8 Correct 18 ms 22264 KB Output is correct
9 Correct 19 ms 22264 KB Output is correct
10 Correct 18 ms 22264 KB Output is correct
11 Correct 18 ms 22392 KB Output is correct
12 Correct 18 ms 22264 KB Output is correct
13 Correct 22 ms 22776 KB Output is correct
14 Correct 23 ms 23288 KB Output is correct
15 Correct 24 ms 22648 KB Output is correct
16 Correct 24 ms 22776 KB Output is correct
17 Correct 23 ms 22776 KB Output is correct
18 Correct 24 ms 22776 KB Output is correct
19 Correct 22 ms 22776 KB Output is correct
20 Correct 22 ms 22776 KB Output is correct
21 Correct 23 ms 22776 KB Output is correct
22 Correct 23 ms 22776 KB Output is correct
23 Correct 23 ms 22776 KB Output is correct
24 Correct 21 ms 22776 KB Output is correct
25 Correct 25 ms 23160 KB Output is correct
26 Correct 21 ms 22776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 22392 KB Output is correct
2 Correct 1721 ms 55564 KB Output is correct
3 Execution timed out 2091 ms 73608 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 22392 KB Output is correct
2 Correct 20 ms 22392 KB Output is correct
3 Correct 21 ms 22392 KB Output is correct
4 Correct 19 ms 22392 KB Output is correct
5 Correct 18 ms 22392 KB Output is correct
6 Correct 18 ms 22264 KB Output is correct
7 Correct 20 ms 22264 KB Output is correct
8 Correct 18 ms 22264 KB Output is correct
9 Correct 19 ms 22264 KB Output is correct
10 Correct 18 ms 22264 KB Output is correct
11 Correct 18 ms 22392 KB Output is correct
12 Correct 17 ms 22392 KB Output is correct
13 Correct 1721 ms 55564 KB Output is correct
14 Execution timed out 2091 ms 73608 KB Time limit exceeded
15 Halted 0 ms 0 KB -