結果

問題 No.1212 Second Path
ユーザー SSRSSSRS
提出日時 2020-08-30 16:48:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 6,153 bytes
コンパイル時間 3,409 ms
コンパイル使用メモリ 239,228 KB
実行使用メモリ 72,264 KB
最終ジャッジ日時 2024-11-15 11:29:04
合計ジャッジ時間 133,168 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,275 ms
51,452 KB
testcase_01 TLE -
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 AC 1,128 ms
51,188 KB
testcase_18 AC 367 ms
51,904 KB
testcase_19 AC 367 ms
11,252 KB
testcase_20 AC 361 ms
51,896 KB
testcase_21 AC 286 ms
11,060 KB
testcase_22 AC 341 ms
51,892 KB
testcase_23 AC 169 ms
10,752 KB
testcase_24 AC 194 ms
37,944 KB
testcase_25 AC 197 ms
11,196 KB
testcase_26 AC 194 ms
31,672 KB
testcase_27 AC 191 ms
10,992 KB
testcase_28 AC 192 ms
44,864 KB
testcase_29 TLE -
testcase_30 TLE -
testcase_31 TLE -
testcase_32 TLE -
testcase_33 TLE -
testcase_34 TLE -
testcase_35 AC 1,449 ms
14,260 KB
testcase_36 TLE -
testcase_37 TLE -
testcase_38 TLE -
testcase_39 TLE -
testcase_40 AC 659 ms
5,964 KB
testcase_41 TLE -
testcase_42 AC 2 ms
5,248 KB
testcase_43 AC 2 ms
5,248 KB
testcase_44 AC 2 ms
5,248 KB
testcase_45 TLE -
testcase_46 TLE -
testcase_47 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
const int LOG = 18;
struct lowest_common_ancestor{
	vector<int> d;
	vector<vector<int>> p;
	lowest_common_ancestor(){
	}
	lowest_common_ancestor(vector<int> &P, vector<vector<int>> &C){
		int N = P.size();
		d = vector<int>(N, 0);
		queue<int> Q;
		Q.push(0);
		while (!Q.empty()){
			int v = Q.front();
			Q.pop();
			for (int w : C[v]){
				d[w] = d[v] + 1;
				Q.push(w);
			}
		}
		p = vector<vector<int>>(LOG, vector<int>(N, -1));
		for (int i = 0; i < N; i++){
			p[0][i] = P[i];
		}
		for (int i = 1; i < LOG; i++){
			for (int j = 0; j < N; j++){
				if (p[i - 1][j] != -1){
					p[i][j] = p[i - 1][p[i - 1][j]];
				}
			}
		}
	}
	int query(int u, int v){
		if (d[u] > d[v]){
			swap(u, v);
		}
		for (int k = 0; k < LOG; k++){
			if ((d[v] - d[u]) >> k & 1){
				v = p[k][v];
			}
		}
		if (u == v){
		    return u;
		}
		for (int k = LOG - 1; k >= 0; k--){
			if (p[k][u] != p[k][v]){
				u = p[k][u];
				v = p[k][v];
    		}
		}
		return p[0][u];
	}
};
template <typename T>
struct binary_indexed_tree{
	int N;
	vector<T> BIT;
	binary_indexed_tree(){
	}
	binary_indexed_tree(int n){
		N = 1;
		while (N < n){
			N *= 2;
		}
		BIT = vector<T>(N + 1, 0);
	}
	void add(int i){
		i++;
		while (i <= N){
			BIT[i]++;
			i += i & -i;
		}
	}
	T sum(int i){
		T ans = 0;
		while (i > 0){
			ans += BIT[i];
			i -= i & -i;
		}
		return ans;
	}
	T query(int L, int R){
		return sum(R) - sum(L);
	}
};
template <typename T>
struct heavy_light_decomposition{
	lowest_common_ancestor LCA;
	vector<int> p;
	vector<int> sz, in, next;
	int t;
	binary_indexed_tree<T> BIT;
	void dfs1(vector<vector<int>> &c, int v = 0){
		sz[v] = 1;
		for (int &w : c[v]){
			dfs1(c, w);
			sz[v] += sz[w];
			if (sz[w] > sz[c[v][0]]){
				swap(w, c[v][0]);
			}
		}
	}
	void dfs2(vector<vector<int>> &c, int v = 0){
		in[v] = t;
		t++;
		for (int w : c[v]){
			if (w == c[v][0]){
				next[w] = next[v];
			} else {
				next[w] = w;
			}
			dfs2(c, w);
		}
	}
	heavy_light_decomposition(vector<int> &p, vector<vector<int>> &c): p(p){
		LCA = lowest_common_ancestor(p, c);
		int N = p.size();
		in = vector<int>(N, -1);
		sz = vector<int>(N, -1);
		next = vector<int>(N, 0);
		BIT = binary_indexed_tree<T>(N);
		t = 0;
		dfs1(c);
		dfs2(c);
	}
	void add(int v){
		BIT.add(in[v]);
	}
	T query(int u, int v){
		int w = LCA.query(u, v);
		T ans = 0;
		while (next[u] != next[w]){
			ans += BIT.query(in[next[u]], in[u] + 1);
			u = p[next[u]];
		}
		ans += BIT.query(in[w], in[u] + 1);
		while (next[v] != next[w]){
			ans += BIT.query(in[next[v]], in[v] + 1);
			v = p[next[v]];
		}
		ans += BIT.query(in[w] + 1, in[v] + 1);
		return ans;
	}
};
template <typename T>
struct weighted_tree_distance{
  vector<int> d;
  vector<T> s;
  vector<vector<int>> next;
  weighted_tree_distance(vector<vector<pair<T, int>>> &E){
    int N = E.size();
    d = vector<int>(N, 0);
    s = vector<T>(N, 0);
    next = vector<vector<int>>(LOG, vector<int>(N, -1));
    queue<int> Q;
    Q.push(0);
    while (!Q.empty()){
      int v = Q.front();
      Q.pop();
      for (auto e : E[v]){
        T c = e.first;
        int w = e.second;
        if (w != next[0][v]){
          next[0][w] = v;
          d[w] = d[v] + 1;
          s[w] = s[v] + c;
          Q.push(w);
        }
      }
    }
    for (int i = 1; i < LOG; i++){
      for (int j = 0; j < N; j++){
        if (next[i - 1][j] != -1){
          next[i][j] = next[i - 1][next[i - 1][j]];
        }
      }
    }
  }
  int lca(int u, int v){
    if (d[u] < d[v]){
      swap(u, v);
    }
    for (int i = 0; i < LOG; i++){
      if ((d[u] - d[v]) >> i & 1){
        u = next[i][u];
      }
    }
    if (u == v){
      return u;
    } else {
      for (int i = LOG - 1; i >= 0; i--){
        if (next[i][u] != next[i][v]){
          u = next[i][u];
          v = next[i][v];
        }
      }
      return next[0][u];
    }
  }
  int dist1(int u, int v){
    return d[u] + d[v] - 2 * d[lca(u, v)];
  }
  T dist2(int u, int v){
    return s[u] + s[v] - 2 * s[lca(u, v)];
  }
};
int main(){
  int N;
  cin >> N;
  vector<vector<pair<long long, int>>> E(N);
  vector<tuple<int, int, int>> edges;
  for (int i = 0; i < N - 1; i++){
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    E[u].push_back(make_pair(w, v));
    E[v].push_back(make_pair(w, u));
    edges.push_back(make_tuple(w, u, v));
  }
  vector<int> p(N, -1);
  vector<vector<int>> c(N);
  vector<long long> a(N, 0);
  queue<int> q;
  q.push(0);
  while (!q.empty()){
    int v = q.front();
    q.pop();
    for (auto e : E[v]){
      int d = e.first;
      int w = e.second;
      if (w != p[v]){
        p[w] = v;
        c[v].push_back(w);
        a[w] = d;
        q.push(w);
      }
    }
  }
  int Q;
  cin >> Q;
  vector<int> x(Q), y(Q);
  for (int i = 0; i < Q; i++){
    cin >> x[i] >> y[i];
    x[i]--;
    y[i]--;
  }
  sort(edges.begin(), edges.end());
  vector<int> tv(Q, N);
  vector<int> fv(Q, 0);
  while (1){
    bool upd = false;
    vector<vector<int>> check(N - 1);
    for (int i = 0; i < Q; i++){
      if (tv[i] - fv[i] > 1){
        upd = true;
        int mid = (tv[i] + fv[i]) / 2;
        check[mid - 1].push_back(i);
      }
    }
    if (!upd){
      break;
    }
    heavy_light_decomposition<int> T1(p, c), T2(p, c);
    vector<bool> used(N, false);
    for (int i = 0; i < N - 1; i++){
      int u = get<1>(edges[i]);
      int v = get<2>(edges[i]);
      T1.add(u);
      T1.add(v);
      if (u == p[v]){
        T2.add(v);
        used[v] = true;
      } else {
        T2.add(u);
        used[u] = true;
      }
      for (int id : check[i]){
        int a = T1.query(x[id], y[id]);
        int b = T2.query(x[id], y[id]);
        int l = T1.LCA.query(x[id], y[id]);
        if (used[l]){
          b--;
        }
        if (a > b * 2){
          tv[id] = i + 1;
        } else {
          fv[id] = i + 1;
        }
      }
    }
  }
  weighted_tree_distance<long long> T(E);
  for (int i = 0; i < Q; i++){
    if (tv[i] == N){
      cout << -1 << endl;
    } else {
      cout << T.dist2(x[i], y[i]) + get<0>(edges[tv[i] - 1]) * 2 << endl;
    }
  }
}
0