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 [ 增量构造,树上 ...
随机推荐
- VC++ ListCtrl Report使用
1.在VC++ 6.0中新建基于对话框的MFC应用程序ListCtrl; 2.在主对话框上添加一个List Control至合适的位置及大小: 3.在对话框OnInitDialog中初始化ListCt ...
- PHP的只是结构图
- THINKPHP5获取设置缓存的例子
在THINKPHP5中 缓存的配置放在了config.php文件中 代码如下 如何设置缓存? 可以使用静态方法 Cache::set('key',$value,3600);//存储缓存 Cache:: ...
- 解决项目导入dubbo依赖项目报红叉问题
1.maven+ssm项目导入dubbo依赖 项目报错如下 2.出错原因在于dubbo依赖低版本的spring和低版本netty,准备通过maven的依赖管理将依赖传递过来的低版本的spring和ne ...
- EditText监听方法,实时的判断输入多少字符
最近在写一个小项目,其中有一点用到了显示EditText中输入了多少个字符,像微博中显示剩余多少字符的功能.在EditText提供了一个方法addTextChangedListener实现对输入文本的 ...
- sssssss
1dispatcherServlet—拦截到spring mvc的请求 2dispatchServlet调用HandlerMapping( DefaultAnnoationHandlerMapping ...
- http协议------>概述和动手实践认识Http协议
http协议是用来定义客户端和web服务器通讯格式 浏览器与服务器的交互过程 是tcp/ip的应用层 版本:http/1.0(客户端和web服务器建立连接后只能访问一个web资源) http/1. ...
- getResource()和getResourceAsStream以及路径问题(转)
用JAVA获取文件,听似简单,但对于很多像我这样的新人来说,还是掌握颇浅,用起来感觉颇深,大常最经常用的,就是用JAVA的File类,如要取得c:/test.txt文件,就会这样用File file ...
- linux伙伴系统接口alloc_page分析1
在内核中分配内存,最后要通过伙伴系统接口进行实际物理页面的分配,一个重要的接口便是alloc_page.本文介绍下alloc_page的主要流程,各个部分的执行.主要包含正常分配流程,当页面不足的时候 ...
- python迟邦定
1.绑定 将函数体和函数调用关联起来,就叫绑定 2.迟绑定 在程序运行之前(也就是编译和链接时)执行的绑定是早绑定,迟绑定(late binding)是发生在运行时. 3.实例说明 def outer ...