您的位置:首页 > 其它

CodeForces 题目191C. Fools and Roads(Link Cut Tree,边权加求边权值)

2015-08-21 22:00 323 查看
C. Fools and Roads

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

They say that Berland has exactly two problems, fools and roads. Besides, Berland has n 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.

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 integers ui, vi (1 ≤ ui, vi ≤ n, ui ≠ vi),
that means that there is a road connecting cities ui and vi.
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. The i-th line (i > 0) contains
numbers ai, bi (1 ≤ ai, bi ≤ n).
That means that the fool number 2i - 1 lives in city ai and
visits the fool number 2i, who lives in city bi.
The given pairs describe simple paths, because between every pair of cities there is only one simple path.

Output
Print n - 1 integer. The integers
should be separated by spaces. The i-th number should equal the number of fools who can go on the i-th
road. The roads are numbered starting from one in the order, in which they occur in the input.

Sample test(s)

input
5
1 2
1 3
2 4
2 5
2
1 4
3 5


output
2 1 1 1


input
5
3 4
4 5
1 4
2 4
3
2 3
1 3
3 5


output
3 1 1 1


Note
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<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<iostream>
#define max(a,b) (a>b?a:b)
using namespace std;
int head[250050],cnt,vis[250050];
struct s
{
int u,v,w,next;
}edge[250050<<1];
struct LCT
{
int bef[250050],pre[250050],next[250050][2],key[250050],val[250050],belong[250050],add[200020];
void init()
{
memset(pre,0,sizeof(pre));
memset(next,0,sizeof(next));
memset(key,0,sizeof(key));
memset(add,0,sizeof(add));
}
void pushdown(int x)
{
if(add[x])
{
int a,b;
a=next[x][0];
b=next[x][1];
if(a)
{
add[a]+=add[x];
key[a]+=add[x];
}
if(b)
{
add[b]+=add[x];
key[b]+=add[x];
}
add[x]=0;
}
}
void rotate(int x,int kind)
{
int y,z;
y=pre[x];
z=pre[y];
pushdown(y);
pushdown(x);
next[y][!kind]=next[x][kind];
pre[next[x][kind]]=y;
next[z][next[z][1]==y]=x;
pre[x]=z;
next[x][kind]=y;
pre[y]=x;
}
void splay(int x)
{
int rt;
for(rt=x;pre[rt];rt=pre[rt]);
if(x!=rt)
{
bef[x]=bef[rt];
bef[rt]=0;
pushdown(x);
while(pre[x])
{
if(next[pre[x]][0]==x)
{
rotate(x,1);
}
else
rotate(x,0);
}
}
}
void access(int x)
{
int fa;
for(fa=0;x;x=bef[x])
{
splay(x);
pushdown(x);
pre[next[x][1]]=0;
bef[next[x][1]]=x;
next[x][1]=fa;
pre[fa]=x;
bef[fa]=0;
fa=x;
}
}
void change(int x,int y)
{
access(y);
for(y=0;x;x=bef[x])
{
splay(x);
if(!bef[x])
{
if(next[x][1])
{
key[next[x][1]]++;
add[next[x][1]]++;
}
if(y)
{
key[y]++;
add[y]++;
}
return;
}
pushdown(x);
pre[next[x][1]]=0;
bef[next[x][1]]=x;
next[x][1]=y;
pre[y]=x;
bef[y]=0;
y=x;
}
}
int query(int x)
{
splay(x);
return key[x];
}
}lct;
void add(int u,int v)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void bfs(int u)
{
queue<int>q;
memset(vis,0,sizeof(vis));
vis[u]=1;
q.push(u);
while(!q.empty())
{
u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(!vis[v])
{
lct.bef[v]=u;
lct.belong[i>>1]=v;
vis[v]=1;
q.push(v);
}
}
}
}
int  main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int i;
memset(head,-1,sizeof(head));
cnt=0;
lct.init();
for(i=1;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
bfs(1);
int q;
scanf("%d",&q);
while(q--)
{
int a,b;
scanf("%d%d",&a,&b);
lct.change(a,b);
}
for(i=0;i<n-2;i++)
{
printf("%d ",lct.query(lct.belong[i]));
}
printf("%d\n",lct.query(lct.belong[i]));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: