843C - Upgrading Tree

843C - Upgrading Tree

题意:给一棵n个点的树,允许不超过2*n次操作。操作:(x,y,z),删除边(x,y),添加边(y,x),要求满足三个条件(见原题)

题解:

待补~

先贴上dalao代码:

 1 #include <bits/stdc++.h>
 2
 3 using namespace std;
 4
 5 const int N = 1234567;
 6
 7 vector <int> st;
 8 int pv[N], sz[N];
 9 vector <int> g[N];
10 vector <int> jobs[N];
11 int depth[N];
12
13 bool flag;
14
15 void dfs(int v, int pr) {
16   st.push_back(v);
17   pv[v] = pr;
18   sz[v] = 1;
19   for (int u : g[v]) {
20     if (u == pr) {
21       continue;
22     }
23     depth[u] = depth[v] + 1;
24     dfs(u, v);
25     sz[v] += sz[u];
26   }
27   if (flag && depth[v] >= 3) {
28     jobs[st[1]].push_back(v);
29   }
30   st.pop_back();
31 }
32
33 int main() {
34   int n;
35   scanf("%d", &n);
36   for (int i = 0; i < n - 1; i++) {
37     int x, y;
38     scanf("%d %d", &x, &y);
39     x--; y--;
40     g[x].push_back(y);
41     g[y].push_back(x);
42   }
43   flag = false;
44   dfs(0, -1);
45   vector <int> roots;
46   for (int i = 0; i < n; i++) {
47     int outer = n - sz[i];
48     if (outer > n / 2) {
49       continue;
50     }
51     bool ok = true;
52     for (int j : g[i]) {
53       if (j == pv[i]) {
54         continue;
55       }
56       if (sz[j] > n / 2) {
57         ok = false;
58         break;
59       }
60     }
61     if (ok) {
62       roots.push_back(i);
63     }
64   }
65   assert(!roots.empty());
66   flag = true;
67   if ((int) roots.size() == 1) {
68     depth[roots[0]] = 0;
69     dfs(roots[0], -1);
70   } else {
71     depth[roots[0]] = depth[roots[1]] = 0;
72     dfs(roots[0], roots[1]);
73     dfs(roots[1], roots[0]);
74   }
75   vector < pair <int, pair <int, int> > > res;
76   for (int i = 0; i < n; i++) {
77     if (jobs[i].empty()) {
78       continue;
79     }
80     int at = i;
81     for (int &v : jobs[i]) {
82       res.emplace_back(pv[i], make_pair(at, v));
83       res.emplace_back(v, make_pair(pv[v], i));
84       at = v;
85     }
86     res.emplace_back(pv[i], make_pair(at, i));
87   }
88   int cnt = res.size();
89   printf("%d\n", cnt);
90   for (int i = 0; i < cnt; i++) {
91     printf("%d %d %d\n", res[i].first + 1, res[i].second.first + 1, res[i].second.second + 1);
92   }
93   return 0;
94 }

tourist

时间: 2024-10-20 03:47:31

843C - Upgrading Tree的相关文章

easyui js取消选中 Tree 指定节点

取消所有选中 var rootNodes = treeObject.tree('getRoots'); for ( var i = 0; i < rootNodes.length; i++) { var node = treeObject.tree('find', rootNodes[i].id); treeObject.tree('uncheck', node.target); }

Maximum Depth of Binary Tree

这道题为简单题 题目: Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 思路: 我是用递归做的,当然也可以用深搜和广搜,递归的话就是比较左右子树的深度然后返回 代码: 1 # Definition for a binary tre

538. Convert BST to Greater Tree 二叉搜索树转换为更大树

Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example: Input: The root of a Binary Search Tree like thi

SPOJ375 Query on a tree

https://vjudge.net/problem/SPOJ-QTREE 题意: 一棵树,每条边有个权值 两种操作 一个修改每条边权值 一个询问两点之间这一条链的最大边权 点数<=10000 多组测试数据,case<=20 Example Input: 1 3 1 2 1 2 3 2 QUERY 1 2 CHANGE 1 3 QUERY 1 2 DONE Output: 1 3 #include<cstdio> #include<iostream> #include&

POJ 1741 Tree(树的点分治,入门题)

Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21357   Accepted: 7006 Description Give a tree with n vertices,each edge has a length(positive integer less than 1001).Define dist(u,v)=The min distance between node u and v.Give an in

命令-tree

tree命令 tree - list contents of directories in a tree-like format. 显示目录的层级结构: tree 命令英文理解为树的意思,其功能是创建文件列表,将目录所有文件以树状的形式列出来.linux中的tree命令默认并不会安装,所以需要通过yum install tree -y来安装此命令. [SYNOPSIS] tree [options] [directory] [OPTIONS] -L level:指定要显示的层级: -d:仅列出目

[LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod

226反转二叉树 Invert Binary Tree

Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Trivia:This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a wh

[hihoCoder#1381]Little Y&#39;s Tree

[hihoCoder#1381]Little Y's Tree 试题描述 小Y有一棵n个节点的树,每条边都有正的边权. 小J有q个询问,每次小J会删掉这个树中的k条边,这棵树被分成k+1个连通块.小J想知道每个连通块中最远点对距离的和. 这里的询问是互相独立的,即每次都是在小Y的原树上进行操作. 输入 第一行一个整数n,接下来n-1行每行三个整数u,v,w,其中第i行表示第i条边边权为wi,连接了ui,vi两点. 接下来一行一个整数q,表示有q组询问. 对于每组询问,第一行一个正整数k,接下来一