結果
問題 |
No.898 tri-βutree
|
ユーザー |
![]() |
提出日時 | 2022-08-12 03:21:16 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,380 bytes |
コンパイル時間 | 2,081 ms |
コンパイル使用メモリ | 196,592 KB |
最終ジャッジ日時 | 2025-01-30 20:19:23 |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 1 WA * 20 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:75:14: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘ll’ {aka ‘long long int’} [-Wformat=] 75 | printf("%d\n",len[x]+len[y]+len[z]-len[lca(x,y)]-len[lca(y,z)]-len[lca(z,x)]); | ~^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | int ll {aka long long int} | %lld
ソースコード
#define rep(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(v) v.begin(), v.end() typedef long long ll; #include <bits/stdc++.h> using namespace std; const int MAX=100010; const int MAX_LOG=17; vector<pair<int,ll>> G[MAX]; int root; int parent[MAX_LOG][MAX]; int depth[MAX]; ll len[MAX]; void dfs(int v,int p,int d,ll l){ parent[0][v]=p; depth[v]=d; len[v]=l; for(auto x:G[v]){ if(x.first==p) continue; dfs(x.first,v,d+1,l+x.second); } } void init(int u){ dfs(root,-1,0,0); for(int k=0;k+1<MAX_LOG;k++){ for(int v=0;v<u;v++){ if(parent[k][v]<0) parent[k+1][v]=-1; else parent[k+1][v]=parent[k][parent[k][v]]; } } } int lca(int u,int v){ if(depth[u]>depth[v]) swap(u,v); for(int k=0;k<MAX_LOG;k++){ if((depth[v]-depth[u])&(1<<k)) v=parent[k][v]; } if(u==v) return u; for(int k=MAX_LOG-1;k>=0;k--){ if(parent[k][u]!=parent[k][v]){ u=parent[k][u]; v=parent[k][v]; } } return parent[0][u]; } int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); int n; cin>>n; rep(i,n-1){ int a,b; ll c; cin>>a>>b>>c; G[a].push_back({b,c}); G[b].push_back({a,c}); } root=0; init(n); int q; cin>>q; while(q--){ int x,y,z; cin>>x>>y>>z; printf("%d\n",len[x]+len[y]+len[z]-len[lca(x,y)]-len[lca(y,z)]-len[lca(z,x)]); } return 0; }