QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#338238#7103. Red Black TreeLainAC ✓803ms28832kbC++233.7kb2024-02-25 19:27:192024-02-25 19:27:19

Judging History

你现在查看的是最新测评结果

  • [2024-02-25 19:27:19]
  • 评测
  • 测评结果:AC
  • 用时:803ms
  • 内存:28832kb
  • [2024-02-25 19:27:19]
  • 提交

answer

#include "bits/stdc++.h"
using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;


template<class T>
struct RMQ {
	vector<vector<T>> jmp;
	RMQ(const vector<T>& V) : jmp(1, V) {
		for (int pw = 1, k = 1; pw * 2 <= sz(V); pw *= 2, ++k) {
			jmp.emplace_back(sz(V) - pw * 2 + 1);
			rep(j,0,sz(jmp[k]))
				jmp[k][j] = min(jmp[k - 1][j], jmp[k - 1][j + pw]);
		}
	}
	T query(int a, int b) {
		assert(a < b); // or return inf if a == b
		int dep = 31 - __builtin_clz(b - a);
		return min(jmp[dep][a], jmp[dep][b - (1 << dep)]);
	}
};

struct LCA {
	int T = 0;
	vi time, path, ret;
	RMQ<int> rmq;

	LCA(vector<vi>& C) : time(sz(C)), rmq((dfs(C,0,-1), ret)) {}
	void dfs(vector<vi>& C, int v, int par) {
		time[v] = T++;
		for (int y : C[v]) if (y != par) {
			path.push_back(v), ret.push_back(time[v]);
			dfs(C, y, v);
		}
	}

	int lca(int a, int b) {
		if (a == b) return a;
		tie(a, b) = minmax(time[a], time[b]);
		return path[rmq.query(a, b)];
	}
	//dist(a,b){return depth[a] + depth[b] - 2*depth[lca(a,b)];}
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int tt;
  cin >> tt;
  while(tt--) {
    int n, m, q;
    cin >> n >> m >> q;
    vector<bool> is_red(n);
    for (int i =0; i < m; i++) {
      int x;
      cin >> x;
      x--;
      is_red[x] = 1;
    }

    vector<vi> lca_g(n);
    vector<vector<pair<int, int>>> g(n);
    rep(i, 1, n) {
      int u, v, w;
      cin >>u  >> v >> w;
      u--, v--;
      lca_g[u].push_back(v);
      lca_g[v].push_back(u);
      g[u].push_back({v, w});
      g[v].push_back({u, w});
    }
    LCA L(lca_g);

    vector<int64_t> cost(n), total_weight(n);
    vi red_parent(n), depth(n);
    auto cost_dfs = [&](auto& self, int s, int p)->void {
      if (is_red[s]) {
        cost[s] = 0;
        red_parent[s] = s;
      }
      for (auto& [u, w] : g[s]) {
        if (u == p) continue;
        depth[u] = depth[s] + 1;
        cost[u] = cost[s] + w;
        total_weight[u] = total_weight[s] + w;
        red_parent[u] = red_parent[s];
        self(self, u, s);
      }
    };
    cost_dfs(cost_dfs, 0, 0);

    while(q--) {
      int k;
      cin >> k;
      vi v(k);
      for (auto& x : v) cin >> x, x--;
      sort(all(v), [&](int x, int y)->bool {
        return cost[x] > cost[y];
      });
      if (v.size() == 1) {
        cout << 0 << '\n';
        continue;
      }
      if (is_red[v[0]]) {
        cout << 0 << '\n';
        continue;
      }
      int64_t ans = cost[v[1]];
      int curr_lca = v[0];
      set<pair<int, int>> depths_and_parents;
      depths_and_parents.insert({depth[red_parent[v[0]]], 0});
      int largest_fixed = -1;
      rep(i, 1, k) {
        int new_lca = L.lca(curr_lca, v[i]);
        depths_and_parents.insert({depth[red_parent[v[i]]], i});
        while(depths_and_parents.size()) {
          auto it = depths_and_parents.rbegin();
          if (it->first >= depth[new_lca]) {
            if (largest_fixed == -1) largest_fixed = it->second;
            else largest_fixed = min(largest_fixed, it->second);
            depths_and_parents.erase(*it);
          } else break;
        }
        if (largest_fixed == 0) {
          // you're stuck at cost[v[0]]
          break;
        }

        int64_t newans = total_weight[v[0]] - total_weight[new_lca];
        newans = max(newans, cost[v[largest_fixed]]);
        if (i+1 != k)
          newans = max(newans, cost[v[i+1]]);
        ans = min(ans, newans);
        curr_lca = new_lca;
      }
      cout << ans << '\n';
    }
  }
}

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3660kb

input:

2
12 2 4
1 9
1 2 1
2 3 4
3 4 3
3 5 2
2 6 2
6 7 1
6 8 2
2 9 5
9 10 2
9 11 3
1 12 10
3 3 7 8
4 4 5 7 8
4 7 8 10 11
3 4 5 12
3 2 3
1 2
1 2 1
1 3 1
1 1
2 1 2
3 1 2 3

output:

4
5
3
8
0
0
0

result:

ok 7 lines

Test #2:

score: 0
Accepted
time: 803ms
memory: 28832kb

input:

522
26 1 3
1
1 4 276455
18 6 49344056
18 25 58172365
19 9 12014251
2 1 15079181
17 1 50011746
8 9 2413085
23 24 23767115
22 2 26151339
26 21 50183935
17 14 16892041
9 26 53389093
1 20 62299200
24 18 56114328
11 2 50160143
6 26 14430542
16 7 32574577
3 16 59227555
3 15 8795685
4 12 5801074
5 20 57457...

output:

148616264
148616264
0
319801028
319801028
255904892
317070839
1265145897
1265145897
1072765445
667742619
455103436
285643094
285643094
285643094
317919339
0
785245841
691421476
605409472
479058444
371688030
303203698
493383271
919185207
910180170
919185207
121535083
181713164
181713164
181713164
181...

result:

ok 577632 lines

Extra Test:

score: 0
Extra Test Passed