[HDOJ6115] Factory(lca, 暴力乱搞)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6115
这题数据太弱了,看到群里说这题暴力就行,就打了一个暴力。
预处理每个点到根的距离,查的时候O(n^2logn)就行了。。。
当然不排除我的暴力姿势好(1e6^3姿势再好也白搭)
1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛┗┛┃ 9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓┃ 14 ┃┃┃┃┃┃ 15 ┻┻┻┻┻┻ 16 */ 17 #include <bits/stdc++.h> 18 using namespace std; 19 #define fr first 20 #define sc second 21 #define cl clear 22 #define BUG puts("here!!!") 23 #define W(a) while(a--) 24 #define pb(a) push_back(a) 25 #define Rint(a) scanf("%d", &a) 26 #define Rll(a) scanf("%I64d", &a) 27 #define Rs(a) scanf("%s", a) 28 #define Cin(a) cin >> a 29 #define FRead() freopen("in", "r", stdin) 30 #define FWrite() freopen("out", "w", stdout) 31 #define Rep(i, len) for(int i = 0; i < (len); i++) 32 #define For(i, a, len) for(int i = (a); i < (len); i++) 33 #define Cls(a) memset((a), 0, sizeof(a)) 34 #define Clr(a, x) memset((a), (x), sizeof(a)) 35 #define Full(a) memset((a), 0x7f7f7f, sizeof(a)) 36 #define lrt rt << 1 37 #define rrt rt << 1 | 1 38 #define pi 3.14159265359 39 #define RT return 40 #define lowbit(x) x & (-x) 41 #define onenum(x) __builtin_popcount(x) 42 typedef long long LL; 43 typedef long double LD; 44 typedef unsigned long long ULL; 45 typedef pair<int, int> pii; 46 typedef pair<string, int> psi; 47 typedef pair<LL, LL> pll; 48 typedef map<string, int> msi; 49 typedef vector<int> vi; 50 typedef vector<LL> vl; 51 typedef vector<vl> vvl; 52 typedef vector<bool> vb; 53 54 const int maxn = 200100; 55 int n, k, CNT, tot; 56 int dp[2*maxn][22],vis[maxn]; 57 int b[2*maxn], used[maxn], f[2*maxn], pos[maxn]; 58 vector<pii> G[2*maxn]; 59 vi rG[2*maxn]; 60 int val[maxn]; 61 62 void st(int n,int x[]) { 63 for(int i = 1; i <= n; i++) dp[i][0] = x[i]; 64 int k = int(log(n+1.0)/log(2.0)); 65 for(int j = 1; j <= k; j++) { 66 for(int i = 1; i + (1 << j) - 1 <= n; i++) { 67 dp[i][j] = min(dp[i][j-1], dp[i+(1<<(j-1))][j-1]); 68 } 69 } 70 } 71 72 int query(int l,int r) { 73 int k = int(log(r-l+1.0)/log(2.0)); 74 return min(dp[l][k], dp[r-(1<<k)+1][k]); 75 } 76 77 void dfs(int s) { 78 int t; 79 used[s] = 1; 80 int w = ++CNT; 81 b[++tot] = w; f[w] = s; 82 pos[s] = tot; 83 Rep(i, G[s].size()) { 84 t=G[s][i].first; 85 if(used[t]) continue; 86 val[t] = val[s] + G[s][i].second; 87 dfs(t); 88 b[++tot] = w; 89 } 90 } 91 int lca(int a,int b) { 92 if(pos[a] > pos[b]) swap(a,b); 93 return f[query(pos[a], pos[b])]; 94 } 95 96 signed main() { 97 // FRead(); 98 int u, v; 99 int n,m,w; 100 int T; 101 Rint(T); 102 W(T) { 103 Rint(n); Rint(m); 104 Rep(i, maxn) { 105 G[i].clear(); 106 rG[i].clear(); 107 } 108 For(i, 1, n) { 109 Rint(u); Rint(v); Rint(w); 110 G[u].push_back(make_pair(v,w)); 111 G[v].push_back(make_pair(u,w)); 112 } 113 For(i, 1, m+1) { 114 int cnt; 115 Rint(cnt); 116 W(cnt) { 117 int f; 118 Rint(f); 119 rG[i].push_back(f); 120 } 121 } 122 val[1] = 0; CNT=0; tot = 0; 123 Cls(used); 124 dfs(1); st(tot,b); 125 Rint(k); 126 int ret; 127 W(k) { 128 int a, b; 129 Rint(a); Rint(b); 130 ret = 0x3f3f3f3f; 131 Rep(i, rG[a].size()) { 132 Rep(j, rG[b].size()) { 133 u = rG[a][i]; v = rG[b][j]; 134 w = val[u] + val[v] - 2 * val[lca(u,v)]; 135 ret = min(w, ret); 136 } 137 } 138 printf("%d\n", ret); 139 } 140 } 141 return 0; 142 }