Fix a Tree】的更多相关文章

D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes     A tree is an undirected connected graph without cycles. Let's consider a rooted undirected tree with n vertices, numbered 1 through n. There are many ways to represen…
D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output A tree is an undirected connected graph without cycles. Let's consider a rooted undirected tree with n vertices, numbered 1 th…
Fix a Tree A tree is an undirected connected graph without cycles. Let's consider a rooted undirected tree with n vertices, numbered 1 through n. There are many ways to represent such a tree. One way is to create an array with n integers p1, p2, ...,…
Problem - D - Codeforces  Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作数. 如果有环和线,环被打开的同时,接入到线上.那就是线和环的总数-1. 如果只有环的话,把所有的环打开,互相接入,共需n次操作. #include <cstdio> #include <algorithm> using namespace std; ; int cur[maxn];…
题目链接:http://codeforces.com/contest/699/problem/D D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output A tree is an undirected connected graph without cycles. Let's consider a root…
Fix a Tree time limit per test2 seconds A tree is an undirected connected graph without cycles. Let's consider a rooted undirected tree with n vertices, numbered 1 through n. There are many ways to represent such a tree. One way is to create an array…
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output A tree is an undirected connected graph without cycles. Let's consider a rooted undirected tree with n vertices, numbered 1 through n. There are…
原题:http://codeforces.com/contest/699/problem/D 题目中所描述的从属关系,可以看作是一个一个块,可以用并查集来维护这个森林.这些从属关系中会有两种环,第一种是一个点从自身出发到自己,这说明该点是一棵子树的根:第二种是从一点出发到另外一个点.这两种情况在并查集合并的时候都会失败,因为合并时他们都已经属于一个子树,我们现在需要做的就是将这些子树合并,这时我们要优先对生成第二种环的子树进行合并,因为这些从属关系一定是需要修改的,第一种情况有一个点可以不需要修…
dfs找出联通块个数cnt,当形成环时,令指向已访问过节点的节点变成指向-1,即做一个标记.把它作为该联通图的根. 把所有联通的图变成一颗树,如果存在指向自己的点,那么它所在的联通块就是一个树(n-1条边),选择这样一个点,其它联通块的根指向它,就需要cnt-1次改变.如果都是环(没有指向自己的),那任意选定一个环,拆开,其它环拆开再连到此环上,就需要cnt次改变. #include <cstdio> #define N 200005 int a[N],v[N],h[N],fa[N],q[N]…
先做拓扑排序,再bfs处理 #include<cstdio> #include<iostream> #include<cstdlib> #include<cstring> #include<, INF = , , ; } ;i<=n; i++) {         ) {             st.push(i);         }     }     ;         ; i = edge[i].next){             ;…