地址:http://codeforces.com/problemset/problem/165/D

题目:

D. Beard Graph
time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Let's define a non-oriented connected graph of n vertices and n - 1 edges as a beard, if all of its vertices except, perhaps, one, have the degree of 2 or 1 (that is, there exists no more than one vertex, whose degree is more than two). Let us remind you that the degree of a vertex is the number of edges that connect to it.

Let each edge be either black or white. Initially all edges are black.

You are given the description of the beard graph. Your task is to analyze requests of the following types:

  • paint the edge number i black. The edge number i is the edge that has this number in the description. It is guaranteed that by the moment of this request the i-th edge is white
  • paint the edge number i white. It is guaranteed that by the moment of this request the i-th edge is black
  • find the length of the shortest path going only along the black edges between vertices a and b or indicate that no such path exists between them (a path's length is the number of edges in it)

The vertices are numbered with integers from 1 to n, and the edges are numbered with integers from 1 to n - 1.

Input

The first line of the input contains an integer n (2 ≤ n ≤ 105) — the number of vertices in the graph. Next n - 1 lines contain edges described as the numbers of vertices viui (1 ≤ vi, ui ≤ nvi ≠ ui) connected by this edge. It is guaranteed that the given graph is connected and forms a beard graph, and has no self-loops or multiple edges.

The next line contains an integer m (1 ≤ m ≤ 3·105) — the number of requests. Next m lines contain requests in the following form: first a line contains an integer type, which takes values ​​from 1 to 3, and represents the request type.

If type = 1, then the current request is a request to paint the edge black. In this case, in addition to number type the line should contain integer id (1 ≤ id ≤ n - 1), which represents the number of the edge to paint.

If type = 2, then the current request is a request to paint the edge white, its form is similar to the previous request.

If type = 3, then the current request is a request to find the distance. In this case, in addition to type, the line should contain two integers ab (1 ≤ a, b ≤ na can be equal to b) — the numbers of vertices, the distance between which must be found.

The numbers in all lines are separated by exactly one space. The edges are numbered in the order in which they are given in the input.

Output

For each request to "find the distance between vertices a and b" print the result. If there is no path going only along the black edges between vertices a and b, then print "-1" (without the quotes). Print the results in the order of receiving the requests, separate the numbers with spaces or line breaks.

Examples
input
3
1 2
2 3
7
3 1 2
3 1 3
3 2 3
2 2
3 1 2
3 1 3
3 2 3
output
1
2
1
1
-1
-1
input
6
1 5
6 4
2 3
3 5
5 6
6
3 3 4
2 5
3 2 6
3 1 2
2 3
3 3 1
output
3
-1
3
2
Note

In the first sample vertices 1 and 2 are connected with edge number 1, and vertices 2 and 3 are connected with edge number 2. Before the repainting edge number 2 each vertex is reachable from each one along the black edges. Specifically, the shortest path between 1 and 3 goes along both edges.

If we paint edge number 2 white, vertex 3 will end up cut off from other vertices, that is, no path exists from it to any other vertex along the black edges.

思路: 树链剖分模板题。

 #include <bits/stdc++.h>

 using namespace std;

 #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=2e5+;
const int mod=1e9+; vector<int>mp[K];
int top[K],sz[K],fa[K],son[K],id[K],hid[K],deep[K];
int cnt,sum[*K];
PII edge[K];
int query(int o,int l,int r,int nl,int nr)
{
if(l==nl&&r==nr) return sum[o];
int mid=l+r>>;
if(nr<=mid) return query(o<<,l,mid,nl,nr);
if(nl>mid) return query(o<<|,mid+,r,nl,nr);
return query(o<<,l,mid,nl,mid)+query(o<<|,mid+,r,mid+,nr);
}
int update(int o,int l,int r,int x,int v)
{
if(l==r)
return sum[o]=v;
int mid=l+r>>;
if(x<=mid) update(o<<,l,mid,x,v);
else if(x>mid) update(o<<|,mid+,r,x,v);
return sum[o]=sum[o<<]+sum[o<<|];
}
void dfs1(int x,int f)
{
sz[x]=,fa[x]=f,son[x]=-,deep[x]=deep[f]+;
for(int i=;i<mp[x].size();i++)
if(mp[x][i]!=f)
{
dfs1(mp[x][i],x);
sz[x]+=sz[mp[x][i]];
if(son[x]==-||sz[son[x]]<sz[mp[x][i]])
son[x]=mp[x][i];
}
}
void dfs2(int x,int f) ///每条边用深度低的节点的序号表示
{
top[x]=f,id[x]=++cnt,hid[id[x]]=x;
if(son[x]!=-) dfs2(son[x],f);
for(int i=;i<mp[x].size();i++)
if(mp[x][i]!=fa[x]&&mp[x][i]!=son[x])
dfs2(mp[x][i],mp[x][i]);
}
void tree_update(int x,int y,int v)
{
if(deep[x]<deep[y]) x=y;
update(,,cnt,id[x],v);
}
int tree_query(int x,int y)
{
int ret=;
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]]) swap(x,y);
ret+=deep[x]-deep[top[x]]+;
if(query(,,cnt,id[top[x]],id[x])) return -;
x=fa[top[x]];
}
if(x==y) return ret;
if(deep[x]>deep[y]) swap(x,y);
ret+=deep[y]-deep[son[x]]+;
if(query(,,cnt,id[son[x]],id[y])) ret=-;
return ret;
}
int main(void)
{
int n,q;
while(~scanf("%d",&n))
{
for(int i=,x,y;i<n;i++)
scanf("%d%d",&x,&y),mp[x].PB(y),mp[y].PB(x),edge[i]=MP(x,y);
dfs1(,),dfs2(,);
scanf("%d",&q);
int op,x,y;
while(q--)
{
scanf("%d%d",&op,&x);
if(op==)
scanf("%d",&y),printf("%d\n",tree_query(x,y));
else if(op==)
tree_update(edge[x].first,edge[x].second,);
else
tree_update(edge[x].first,edge[x].second,);
}
}
return ;
}

Codeforces Round #112 (Div. 2) D. Beard Graph的更多相关文章

  1. Codeforces Round #112 (Div. 2)

    Codeforces Round #112 (Div. 2) C. Another Problem on Strings 题意 给一个01字符串,求包含\(k\)个1的子串个数. 思路 统计字符1的位 ...

  2. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  3. Codeforces Round #237 (Div. 2) C. Restore Graph(水构造)

    题目大意 一个含有 n 个顶点的无向图,顶点编号为 1~n.给出一个距离数组:d[i] 表示顶点 i 距离图中某个定点的最短距离.这个图有个限制:每个点的度不能超过 k 现在,请构造一个这样的无向图, ...

  4. Codeforces Round #112 (Div. 2)---A. Supercentral Point

    Supercentral Point time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. Codeforces Round #261 (Div. 2)——Pashmak and Graph

    题目链接 题意: n个点.m个边的有向图.每条边有一个权值,求一条最长的路径,使得路径上边值严格递增.输出路径长度 )) 分析: 由于路径上会有反复点,而边不会反复.所以最開始想的是以边为状态进行DP ...

  6. Codeforces Round #600 (Div. 2) - D. Harmonious Graph(并查集)

    题意:对于一张图,如果$a$与$b$连通,则对于任意的$c(a<c<b)$都有$a$与$c$连通,则称该图为和谐图,现在给你一张图,问你最少添加多少条边使图变为和谐图. 思路:将一个连通块 ...

  7. 构造图 Codeforces Round #236 (Div. 2) C. Searching for Graph

    题目地址 /* 题意:要你构造一个有2n+p条边的图,使得,每一个含k个结点子图中,最多有2*k+p条边 水得可以啊,每个点向另外的点连通,只要不和自己连,不重边就可以,正好2*n+p就结束:) */ ...

  8. DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph

    题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...

  9. CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)

    思路来自这里,重点大概是想到建树和无解情况,然后就变成树形DP了- - /* CodeForces 840B - Leha and another game about graph [ 增量构造,树上 ...

随机推荐

  1. UVA 548(二叉树重建与遍历)

    J - Tree Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Ap ...

  2. ThreadLocal并不是一个Thread

    ThreadLocal是什么? 早在JDK 1.2的版本中就提供java.lang.ThreadLocal,ThreadLocal为解决多线程程序的并发问题提供了一种新的思路.使用这个工具类可以很简洁 ...

  3. virtualbox虚拟机Linux系统与本地windows系统共享文件方法

    转自:http://jingyan.baidu.com/article/2fb0ba40541a5900f2ec5f07.html

  4. Android无线测试之—UiAutomator UiScrollable API介绍六

    向前与向后滚动API 一.向前与向后滚动相关API 返回值 API 描述 boolean scrollBackward(int steps) 自动以步长向后滑动 boolean scrollBackw ...

  5. mysql5.6的二进制包安装

    author: headsen chen data :2018-06-08  16:21:43 1. 创建存放软件文件夹 # cd / #mkdir a 2.下载MySQL5.6二进制包 cd a w ...

  6. 如何激励用户为你的app评分?

    如何激励用户为你的app评分? 2014-04-10 16:21 编辑: suiling 分类:营销推广 来源:CocoaChina  0 7247 应用设计应用评分 招聘信息: IOS兼职 深圳创业 ...

  7. angular4 常用pipe管道

    angular中的pipe是用来对输入的数据进行处理,如大小写转换.数值和日期格式化等. 常用的pipe有 1. 大小写转换 <p>{{str | uppercase}}</p> ...

  8. absolute float 比较分析

    同一:absolute-float 都脱离了文档流,也就是默认情况下,父盒子计算高度不包括 absolute/float 的元素 下例: 两个 parentDiv,背景色:深海蓝;边框:1px红色实线 ...

  9. docker-compose安装elasticsearch集群

    文件目录: 1.编写docker-compose文件 version: '3' services: es-master: image: elasticsearch:6.4.3 container_na ...

  10. 170328、Maven+SpringMVC+Dubbo 简单的入门demo配置

    之前一直听说dubbo,是一个很厉害的分布式服务框架,而且巴巴将其开源,这对于咱们广大程序猿来说,真是一个好消息.最近有时间了,打算做一个demo把dubbo在本地跑起来先. 先copy一段dubbo ...