Codeforces Round #112 (Div. 2) D. Beard Graph
地址:http://codeforces.com/problemset/problem/165/D
题目:
4 seconds
256 megabytes
standard input
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.
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 vi, ui (1 ≤ vi, ui ≤ n, vi ≠ 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 a, b (1 ≤ a, b ≤ n, a 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.
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.
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
1
2
1
1
-1
-1
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
3
-1
3
2
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的更多相关文章
- Codeforces Round #112 (Div. 2)
Codeforces Round #112 (Div. 2) C. Another Problem on Strings 题意 给一个01字符串,求包含\(k\)个1的子串个数. 思路 统计字符1的位 ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #237 (Div. 2) C. Restore Graph(水构造)
题目大意 一个含有 n 个顶点的无向图,顶点编号为 1~n.给出一个距离数组:d[i] 表示顶点 i 距离图中某个定点的最短距离.这个图有个限制:每个点的度不能超过 k 现在,请构造一个这样的无向图, ...
- Codeforces Round #112 (Div. 2)---A. Supercentral Point
Supercentral Point time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #261 (Div. 2)——Pashmak and Graph
题目链接 题意: n个点.m个边的有向图.每条边有一个权值,求一条最长的路径,使得路径上边值严格递增.输出路径长度 )) 分析: 由于路径上会有反复点,而边不会反复.所以最開始想的是以边为状态进行DP ...
- Codeforces Round #600 (Div. 2) - D. Harmonious Graph(并查集)
题意:对于一张图,如果$a$与$b$连通,则对于任意的$c(a<c<b)$都有$a$与$c$连通,则称该图为和谐图,现在给你一张图,问你最少添加多少条边使图变为和谐图. 思路:将一个连通块 ...
- 构造图 Codeforces Round #236 (Div. 2) C. Searching for Graph
题目地址 /* 题意:要你构造一个有2n+p条边的图,使得,每一个含k个结点子图中,最多有2*k+p条边 水得可以啊,每个点向另外的点连通,只要不和自己连,不重边就可以,正好2*n+p就结束:) */ ...
- DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph
题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...
- CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)
思路来自这里,重点大概是想到建树和无解情况,然后就变成树形DP了- - /* CodeForces 840B - Leha and another game about graph [ 增量构造,树上 ...
随机推荐
- hrbustoj 1318:蛋疼的蚂蚁(计算几何,凸包变种,叉积应用)
蛋疼的蚂蚁 Time Limit: 1000 MS Memory Limit: 65536 K Total Submit: 39(22 users) Total Accepted: 26 ...
- Java知识点梳理——装箱和拆箱
1.前言:Java是典型的面向对象编程语言,但其中有8种基本数据类型不支持面向对象编程,基本数据类型不具备对象的特性,没有属性和方法:Java为此8种基本数据类型设计了对应的类(包装类),使之相互转换 ...
- public, protected, private,internal,protected internal的区别
虽然这个知识比较简单, 但是老是会忘, 写上来, 增强记忆. 在C#语言中,共有五种访问修饰符:public.private.protected.internal.protected internal ...
- 【BZOJ3555】[Ctsc2014]企鹅QQ hash
[BZOJ3555][Ctsc2014]企鹅QQ Description PenguinQQ是中国最大.最具影响力的SNS(Social Networking Services)网站,以实名制为基础, ...
- [转]Mac下cocos2dx-3.2+Xcode环境配置和项目创建
原文:http://blog.csdn.net/u012200908/article/details/38070885 这是有关环境配置的第二篇教程,第一篇讲的是win8下的环境配置.这里我们使用C+ ...
- IOS 十位数0补齐
NSCalendar *calendar = [NSCalendar currentCalendar]; unsigned unitFlags = NSYearCalendarUnit | NSMon ...
- Python全栈day21(调用模块路径BASEDIR的正确方法)
正常写python程序会有一个可执行的bin.py文件,假如这个文件需要导入my_module里面定义的模块,应该怎么设置sys.path 文件夹目录结构如下,因为bin不在与my_module同级目 ...
- response.setContentType()的String参数及对应类型(转)
response.setContentType(MIME)的作用是使客户端浏览器,区分不同种类的数据,并根据不同的MIME调用浏览器内不同的程序嵌入模块来处理相应的数据. 例如web浏览器就是通过MI ...
- HDU2604—Queuing
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2604 题目意思:n个人排队,f表示女,m表示男,包含子串‘fmf’和‘fff’的序列为O队列,否则为E ...
- 小程序 Expecting 'EOF','}',',',']', got INVALID
修改了app.json中的东西,做了注释操作,报如下错误: 修改了好一阵,一直报错,原来是json文件中无法使用注释惹的祸,具体查看:官方文档