Codeforce-191C-Fools and Roads (树链剖分 更新边权)
来源:互联网 发布:java简单小游戏 编辑:程序博客网 时间:2024/05/23 17:33
They say that Berland has exactly two problems, fools and roads. Besides, Berland hasn cities, populated by the fools and connected by the roads. All Berland roads are bidirectional. As there are many fools in Berland, between each pair of cities there is a path (or else the fools would get upset). Also, between each pair of cities there is no more than one simple path (or else the fools would get lost).
But that is not the end of Berland's special features. In this country fools sometimes visit each other and thus spoil the roads. The fools aren't very smart, so they always use only the simple paths.
A simple path is the path which goes through every Berland city not more than once.
The Berland government knows the paths which the fools use. Help the government count for each road, how many distinct fools can go on it.
Note how the fools' paths are given in the input.
The first line contains a single integer n (2 ≤ n ≤ 105) — the number of cities.
Each of the next n - 1 lines contains two space-separated integersui, vi (1 ≤ ui, vi ≤ n,ui ≠ vi), that means that there is a road connecting citiesui andvi.
The next line contains integer k (0 ≤ k ≤ 105) — the number of pairs of fools who visit each other.
Next k lines contain two space-separated numbers. Thei-th line (i > 0) contains numbersai, bi (1 ≤ ai, bi ≤ n). That means that the fool number 2i - 1 lives in cityai and visits the fool number2i, who lives in city bi. The given pairs describe simple paths, because between every pair of cities there is only one simple path.
Print n - 1 integer. The integers should be separated by spaces. Thei-th number should equal the number of fools who can go on thei-th road. The roads are numbered starting from one in the order, in which they occur in the input.
51 21 32 42 521 43 5
2 1 1 1
53 44 51 42 432 31 33 5
3 1 1 1
In the first sample the fool number one goes on the first and third road and the fool number 3 goes on the second, first and fourth ones.
In the second sample, the fools number 1, 3 and 5 go on the first road, the fool number 5 will go on the second road, on the third road goes the fool number 3, and on the fourth one goes fool number 1.
题意:给你一个无向图,然后人可以从一个点走到另一个点,问最后每条路呗走了几次;
思路:树链剖分裸题,维护边权,然后按输入顺序输出每条边的次数
AC代码:
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<string>#include<sstream>#define maxn 150000using namespace std;int n,k,tot,num;struct E{int x;int y;}e[maxn];struct Edge{int to;int next;}edge[maxn*2];int dep[maxn],son[maxn],sz[maxn],fa[maxn],id[maxn],top[maxn],head[maxn];void add_edge(int u,int v){ edge[++tot].to=v; edge[tot].next=head[u]; head[u]=tot;}void dfs1(int u,int f,int deep){ dep[u]=deep; sz[u]=1; son[u]=0; fa[u]=f; for(int i=head[u];i!=-1;i=edge[i].next) { int ff=edge[i].to; if(ff==f)continue; dfs1(ff,u,deep+1); sz[u]+=sz[ff]; if(sz[son[u]]<sz[ff]) son[u]=ff; }}void dfs2(int u,int tp){ top[u]=tp; id[u]=++num; if(son[u])dfs2(son[u],tp); for(int i=head[u];i!=-1;i=edge[i].next) { int ff=edge[i].to; if(ff==fa[u]||ff==son[u])continue; dfs2(ff,ff); }}struct node{ int l; int r; int tag;}tree[maxn*4];void build(int root,int l,int r){ tree[root].l=l; tree[root].r=r; tree[root].tag=0; if(l==r) return; int mid=(l+r)>>1; build(root<<1,l,mid); build(root<<1|1,mid+1,r);}void pushdown(int root){ if(tree[root].tag) { tree[root<<1].tag+=tree[root].tag; tree[root<<1|1].tag+=tree[root].tag; tree[root].tag=0; }}void update(int root,int l,int r,int val){ int ll=tree[root].l; int rr=tree[root].r; if(l<=ll&&rr<=r) { tree[root].tag+=val; return; } pushdown(root); int mid=(ll+rr)>>1; if(l<=mid) update(root<<1,l,r,val); if(r>mid) update(root<<1|1,l,r,val);}int query(int root,int p){ int ll=tree[root].l; int rr=tree[root].r; if(ll==rr) return tree[root].tag; pushdown(root); int mid=(ll+rr)>>1; if(p<=mid) return query(root<<1,p); else return query(root<<1|1,p);}void Yougth(int u,int v){ int tp1=top[u]; int tp2=top[v]; while(tp1!=tp2) { if(dep[tp1]<dep[tp2]) { swap(u,v); swap(tp1,tp2); } update(1,id[tp1],id[u],1); u=fa[tp1]; tp1=top[u]; } if(u==v) return; if(dep[u]>dep[v]) swap(u,v); update(1,id[son[u]],id[v],1);}void init(){ tot=num=0; memset(head,-1,sizeof(head));}int main(){ while(~scanf("%d",&n)) { init(); int x,y; for(int i=1;i<=n-1;i++) { scanf("%d%d",&e[i].x,&e[i].y); add_edge(e[i].x,e[i].y); add_edge(e[i].y,e[i].x); } dfs1(1,0,1); dfs2(1,1); for(int i=1;i<n;i++) { if(dep[e[i].x]>dep[e[i].y]) swap(e[i].x,e[i].y); } build(1,1,n); scanf("%d",&k); while(k--) { scanf("%d%d",&x,&y); Yougth(x,y); } for(int i=1;i<n;i++) printf("%d%c",query(1,id[e[i].y]),i==n-1?'\n':' '); } return 0;}
- Codeforce-191C-Fools and Roads (树链剖分 更新边权)
- Codeforces 191 C Fools and Roads (树链剖分)
- Codeforces 191C Fools and Roads(树链剖分)
- Codeforces-191C: Fools and Roads(LCA)
- Codeforces 192E Fools and Roads【树链剖分】
- CodeForces 191C Fools and Roads 树上的前缀和 LCA
- CodeForces 题目191C. Fools and Roads(Link Cut Tree,边权加求边权值)
- Fools and Foolproof Roads CodeForces
- [CF 191C]Fools and Roads[LCA Tarjan算法][LCA 与 RMQ问题的转化][LCA ST算法]
- codeforces 192E Fools and Roads
- Codeforces Round #121 (Div. 1) C. Fools and Roads 经典的lca前缀和问题
- Codeforces 362D Fools and Foolproof Roads 构造题
- codeforce Ilya and Roads (区间dp)
- Codeforces 362D Fools and Foolproof Roads【并查集+优先队列+思维】
- codeforce C - Vasya and Robot
- CodeForces 191CFools and Roads 树链剖分
- Codeforce 313C Ilya and Matrix
- CodeForce 204 Div2. C Jeff And Rounding
- failed to connect to beauty.moreunion.com/47.93.20.43 (port 80) after 15000ms
- HTTP协议理解 + PHP使用curl函数发送Post请求的注意事项
- 上台阶
- 网络七层协议
- 图片缓存到 cdCard
- Codeforce-191C-Fools and Roads (树链剖分 更新边权)
- 二维数组更快的遍历方式
- 正则表达式简介及常用正则表达式分享
- Numpy库学习(二)文件读取
- 探索频道和谷歌联合制作七大洲人文VR视频,11月3日可收看
- linux 基础5
- 14 事物&&连接池
- 查找
- CLASSLOADER详解