結果
| 問題 |
No.2337 Equidistant
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-06-02 22:57:00 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,664 ms / 4,000 ms |
| コード長 | 6,659 bytes |
| コンパイル時間 | 5,372 ms |
| コンパイル使用メモリ | 246,448 KB |
| 実行使用メモリ | 91,908 KB |
| 最終ジャッジ日時 | 2024-12-28 21:21:04 |
| 合計ジャッジ時間 | 26,074 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 28 |
ソースコード
#include <bits/stdc++.h>
#include <atcoder/all>
// url
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=b-1;i>=a;i--)
#define all(x) (x).begin(),(x).end()
#define pb(x) push_back(x);
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
typedef long long ll;
typedef long double lld;
using namespace std;
using namespace atcoder;
using mint = static_modint<998244353>;
const ll mod=998244353;
// using mint = static_modint<1000000007>;
//const ll mod=1e9+7;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
const string zton="0123456789";
const string atoz="abcdefghijklmnopqrstuvwxyz";
const string ATOZ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const ll inf=(1ll<<60);
// const int inf=(1<<30);
lld dist(lld x1,lld x2,lld y1,lld y2){
lld res=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
res=sqrt(abs(res));
return res;
}
lld arg(lld x,lld y){
const lld eps=1e-8;
lld res=0;
if(abs(x)+abs(y)<=eps)return 0.0;
else if(abs(x)<=eps){
if(y>=0.0)return (M_PI/2);
else return (M_PI/2+M_PI);
}
else if(abs(y)<=eps){
if(x>=0.0)return 0.0;
else return M_PI;
}
res=atan2(abs(y),abs(x));
if(x<=0&&y>=0)res=(M_PI-res);
else if(x<=0&&y<=0)res+=(M_PI);
else if(x>=0&&y<=0)res=(M_PI*2-res);
return res;
}
ll gcd(ll a,ll b){
if(a==0||b==0)return a+b;
ll r;
r=a%b;
if(r==0){
return b;
}
else{
return gcd(b,r);
}
}
typedef pair<ll,int> P;
struct Edge {
long long to;
};
using Graph = vector<vector<Edge>>;
/* LCA(G, root): 木 G に対する根を root として Lowest Common Ancestor を求める構造体
query(u,v): u と v の LCA を求める。計算量 O(logn)
前処理: O(nlogn)時間, O(nlogn)空間
*/
struct LCA {
vector<vector<int>> parent; // parent[k][u]:= u の 2^k 先の親
vector<int> dist; // root からの距離
LCA(const Graph &G, int root = 0) { init(G, root); }
// 初期化
void init(const Graph &G, int root = 0) {
int V = G.size();
int K = 1;
while ((1 << K) < V) K++;
parent.assign(K, vector<int>(V, -1));
dist.assign(V, -1);
dfs(G, root, -1, 0);
for (int k = 0; k + 1 < K; k++) {
for (int v = 0; v < V; v++) {
if (parent[k][v] < 0) {
parent[k + 1][v] = -1;
} else {
parent[k + 1][v] = parent[k][parent[k][v]];
}
}
}
}
// 根からの距離と1つ先の頂点を求める
void dfs(const Graph &G, int v, int p, int d) {
parent[0][v] = p;
dist[v] = d;
for (auto e : G[v]) {
if (e.to != p) dfs(G, e.to, v, d + 1);
}
}
int query(int u, int v) {
if (dist[u] < dist[v]) swap(u, v); // u の方が深いとする
int K = parent.size();
// LCA までの距離を同じにする
for (int k = 0; k < K; k++) {
if ((dist[u] - dist[v]) >> k & 1) {
u = parent[k][u];
}
}
// 二分探索で LCA を求める
if (u == v) return u;
for (int k = K - 1; k >= 0; k--) {
if (parent[k][u] != parent[k][v]) {
u = parent[k][u];
v = parent[k][v];
}
}
return parent[0][u];
}
int get_post(int v,int u){
int K=parent.size();
int distu=dist[u];
int distv=dist[v]+1;
for(int k=K-1;k>=0;k--){
if(dist[u]-distv>=(1<<k)){
u=parent[k][u];
}
}
return u;
}
int get_half(int s,int t){
int u=query(s,t);
int K=parent.size();
int target_dist=dist[t]-get_dist(s,t)/2;
for(int k=K-1;k>=0;k--){
if(dist[t]-target_dist>=(1<<k)){
t=parent[k][t];
}
}
return t;
}
int get_dist(int u, int v) { return dist[u] + dist[v] - 2 * dist[query(u, v)]; }
};
// int n;cin >> n;
// Graph g(n);
// rep(i,0,n){
// int k;cin >> k;
// rep(j,0,k){
// Edge c;
// cin >> c.to;
// g[i].push_back(c);
// }
// }
// LCA aa=LCA(g,0);
// int q;cin >> q;
// rep(i,0,q){
// int u,v;cin >> u >> v;
// cout << aa.query(u,v) << endl;
// }
int N;
vector<int> G[201010];
map<int,int> F[201010];
int dfs(int cu,int pa){
F[cu][cu]=1;
for(int to:G[cu]){
if(to==pa)continue;
F[cu][to]=dfs(to,cu);
F[cu][cu]+=F[cu][to];
}
return F[cu][cu];
}
int solve(LCA &lca,int s,int t){
if(s==t){
return 0;
}
int dists=lca.get_dist(s,0);
int distt=lca.get_dist(t,0);
if(dists==distt){
int u=lca.query(s,t);
int s2=lca.get_post(u,s);
int t2=lca.get_post(u,t);
// if(s==14&&t==7){
// cout << s2 << " " << t2 << endl;
// }
// ans.pb(N-F[u][s2]-F[u][t2]);
// if(N-F[u][s2]-F[u][t2]<0){
// cout << s << " " << t << endl;
// }
return N-F[u][s2]-F[u][t2];
}
else{
int distst=lca.get_dist(s,t);
if(distst%2==1){
// ans.pb(0);
return 0;
}
else{
if(dists>distt){
swap(s,t);
swap(dists,distt);
}
int u=lca.get_half(s,t);
int t2=lca.get_post(u,t);
// ans.pb(F[u][u]-F[u][t2]);
return F[u][u]-F[u][t2];
}
}
}
int main(void){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int Q;cin >> N >> Q;
Graph g(N);
rep(i,0,N-1){
int a,b;cin >> a >> b;
a--;b--;
Edge c;
c.to=b;
g[a].push_back(c);
c.to=a;
g[b].push_back(c);
G[a].pb(b);
G[b].pb(a);
}
dfs(0,-1);
LCA lca=LCA(g,0);
vector<int> ans;
rep(i,0,Q){
int s,t;cin >> s >> t;s--;t--;
ans.pb(solve(lca,s,t));
}
// rep(s,0,N)rep(t,0,N){
// int cnt=0;
// if(s==t){
// continue;
// }
// rep(u,0,N){
// if(u==s||t==u)continue;
// if(lca.get_dist(s,u)==lca.get_dist(t,u))cnt++;
// }
// solve(lca,s,t);
// cout << s << " " << t << " : " << solve(lca,s,t) << " " << cnt << endl;
// }
for(int x:ans){
cout << x << endl;
}
// rep(i,0,N){
// cout << F[i][i] << endl;
// }
}