bzoj3488: [ONTAK2010]Highways
题解:我们考虑如果两点直接不是祖先关系 那么对应的答案为非树边能使两个子树的联通的方案数 如果是祖先关系 则可以看成2个非祖先关系的情况 将区间拆分成两个部分 用树状数组维护答案即可 复杂度o(nlogn)
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <vector> #include <stack> #include <queue> #include <cmath> #include <set> #include <map> #define mp make_pair #define pb push_back #define pii pair<int,int> #define link(x) for(edge *j=h[x];j;j=j->next) #define inc(i,l,r) for(int i=l;i<=r;i++) #define dec(i,r,l) for(int i=r;i>=l;i--) const int MAXN=1e5+10; const double eps=1e-8; #define ll long long using namespace std; struct edge{int t;edge*next;}e[MAXN<<1],*h[MAXN],*o=e; void add(int x,int y){o->t=y;o->next=h[x];h[x]=o++;} ll read(){ ll x=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); return x*f; } int fa[MAXN][21],dep[MAXN],p[MAXN],cnt,fp[MAXN],num[MAXN],n; void dfs(int x,int pre,int deep){ fa[x][0]=pre;dep[x]=deep+1;p[x]=++cnt;fp[p[x]]=x;num[x]=1; link(x){ if(j->t!=pre){ dfs(j->t,x,deep+1); num[x]+=num[j->t]; } } } void dfs1(int x){ inc(i,1,20)fa[x][i]=fa[fa[x][i-1]][i-1]; link(x){ if(j->t!=fa[x][0])dfs1(j->t); } } int Lca(int u,int v){ if(dep[u]<dep[v])swap(u,v); int tmp=dep[u]-dep[v]; for(int i=0;i<=20;i++)if(tmp&(1<<i))u=fa[u][i]; if(u==v)return u; for(int i=20;i>=0;i--){ if(fa[u][i]!=fa[v][i])u=fa[u][i],v=fa[v][i]; } return fa[u][0]; } int Querty(int u,int v){ for(int i=20;i>=0;i--){ if(dep[fa[u][i]]>dep[v])u=fa[u][i]; } return u; } vector<int>vec[MAXN]; typedef struct node{ int l,r,id; }node; vector<node>que[MAXN*20]; int ans[MAXN*6],sum[MAXN]; int get_id(int x){return x&(-x);} void Sum(int x){ for(int i=x;i<=n;i+=get_id(i))sum[i]++; } int querty(int x){ int ans1=0; for(int i=x;i>0;i-=get_id(i))ans1+=sum[i]; return ans1; } int main(){ n=read(); int u,v; inc(i,1,n-1)u=read(),v=read(),add(u,v),add(v,u); dfs(1,0,0); dfs1(1); int m=read(); inc(i,1,m)u=read(),v=read(),vec[max(p[u],p[v])].pb(min(p[u],p[v])); int q=read(); inc(i,1,q){ u=read();v=read(); int lca=Lca(u,v);int ans1=0; if(lca==u||lca==v){ if(dep[u]<dep[v])swap(u,v); int lca1=Querty(u,lca); que[p[u]-1].pb((node){1,p[lca1]-1,-i}); que[p[u]+num[u]-1].pb((node){1,p[lca1]-1,i}); que[p[lca1]+num[lca1]-1].pb((node){p[u],p[u]+num[u]-1,-i}); que[n].pb((node){p[u],p[u]+num[u]-1,i}); } else{ if(p[u]>p[v])swap(u,v); que[p[v]-1].pb((node){p[u],p[u]+num[u]-1,-i}); que[p[v]+num[v]-1].pb((node){p[u],p[u]+num[u]-1,i}); } } inc(i,1,n){ for(int j=0;j<vec[i].size();j++)Sum(vec[i][j]); for(int j=0;j<que[i].size();j++){ if(que[i][j].id<0)ans[-que[i][j].id]-=querty(que[i][j].r)-querty(que[i][j].l-1); else ans[que[i][j].id]+=querty(que[i][j].r)-querty(que[i][j].l-1); } } inc(i,1,q)printf("%d\n",ans[i]+1); return 0; }
3488: [ONTAK2010]Highways
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 267 Solved: 45
[Submit][Status][Discuss]
Description
Byteland is a small country, with cities connected by N-1 bidirectional roads. From each city there is just one way of reaching every other city using the road network, which causes severe traffic jams. For this reason several highways have been built; each highway connects some pair of cities.
By a route we mean a sequence of roads and/or highways. All cities on a route should be distinct. For each pair of cities x,y there exists exactly one route which does not use any highway; we call such a route the main route between x and y .
People going from a city x to a city y can either choose the main route or use some highway. In the latter case the route cannot intersect the main route except for the cities x and y and must contain exactly one highway.
Your task is to calculate the number of routes people can take between given pairs of cities.
给一棵n个点的树以及m条额外的双向边
q次询问,统计满足以下条件的u到v的路径:
恰经过一条额外的边
不经过树上u到v的路径上的边
Input
The first line of input contains a single integer N(1<=N<=100000) - the number of cities in Byteland. Cities are numbered from 1 to n . Each of the next N -1 lines contains two integers Ai, Bi(1<=Ai,Bi<=N) meaning that cities Ai and Biare connected by a road.
The next line contains an integer M(1<=M<=100000) - the number of highways. Each of the next m lines contains a description of a single highway. The next line contains an integer Q (1<=Q<=500000) - the number of queries. Each of the next Q lines contains a description of a query. Both highways and queries are given in the same format as the roads.
Output
Your program should output exactly Q lines. The i-th line should contain the number of routes in the i-th query.
Sample Input
1 2
2 3
4 2
1 5
5 6
7 5
7 8
9 7
4
2 5
3 4
6 4
8 3
4
4 9
2 5
1 6
1 7
Sample Output
4
2
2