UVALive-5731

题意

一颗 n - 1 条边的有向树,要求整棵树成为强连通图,一次操作即构建一条路(一笔画),

限制:

  • 新建的路上的所有边必须与原有的边逆向,即构建的路必须在原有的边和点上,
  • 操作构建的路可以存在公共边或公共点,
  • 一次操作构建的路只能有同一点或边一次

分析

要成为强连通图,那么在建边的时候肯定是正反边都要建了,

up[i] ,表示从 子节点 到 当前节点 i 要有多少条向上的边;

down[i] ,表示从 当前节点 i 到 子节点 要有多少条向下的边;

对1这个节点,up[1] = 1,因为实际有一条从 1 到 2 的边,那么新建的边就是 2 -> 1 的这条边;

结合这个例子模拟一下就好懂了,

从 0 开始向下搜索,

先搜到 4 之后回到 3 ,显然 4 -> 3 建边,那么 up[3] = 1 ,

再去搜 5 再回到 3 ,此时 5 -> 3 建边,那么 up[3] = 2,

回到 2 ,那么 up[2] = max(1, up[3]) = 2 ,

再去搜 1 ,回到 2,2 -> 1 建边,down[2] = 1,

关键来了,此时 min(up[2], down[2]) > 0 ,说明可以直接从 4 向 1 建一条路,然后 up[2]-- ,down[2]-- ,ans++;

多的 up[] down[] 继续向上传递即可,最后答案加上 max(up[0], down[0]);

对于这种情况,从 0 开始搜,搜到 2 后回到 1,up[1] = 1,在回到 0 ,down[0] = 1,在得到 down[0] 的值的时候注意了,ans 要直接加上 up[1] 的值,因为up[1] 无法向上传递了,所以当边的方向产生交替的时候,就要处理那些无法向上传递的值;

code

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e4 + 10;
typedef pair<int, int> P;
vector<P> G[MAXN];
int up[MAXN];
int down[MAXN];
int ans;
void dfs(int pre, int u)
{
for(int i = 0; i < G[u].size(); i++)
{
P v = G[u][i];
if(v.first == pre) continue;
dfs(u, v.first);
if(v.second)
{
up[u] += max(1, up[v.first]);
ans += down[v.first];
}
else
{
down[u] += max(1, down[v.first]);
ans += up[v.first];
}
}
int tmp = min(up[u], down[u]);
up[u] -= tmp; down[u] -= tmp;
ans += tmp;
}
int main()
{
int T, c = 1;
scanf("%d", &T);
while(T--)
{
memset(G, 0, sizeof G);
memset(up, 0, sizeof up);
memset(down, 0, sizeof down);
ans = 0;
int n;
scanf("%d", &n);
for(int i = 1; i < n; i++)
{
int x, y;
scanf("%d%d", &x, &y);
G[x].push_back(P(y, 1));
G[y].push_back(P(x, 0));
}
dfs(-1, 0);
printf("Case %d: %d\n", c++, ans + max(up[0], down[0]));
}
return 0;
}

UVALive-5731的更多相关文章

  1. uvalive 5731 Qin Shi Huang’s National Road System

    题意: 秦始皇要修路使得所有的城市连起来,并且花费最少:有一个人,叫徐福,他可以修一条魔法路,不花费任何的钱与劳动力. 秦始皇想让修路的费用最少,但是徐福想要受益的人最多,所以他们经过协商,决定让 A ...

  2. UVALive - 4108 SKYLINE[线段树]

    UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug ...

  3. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  4. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  5. 思维 UVALive 3708 Graveyard

    题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...

  6. UVALive 6145 Version Controlled IDE(可持久化treap、rope)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  7. UVALive 6508 Permutation Graphs

    Permutation Graphs Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  8. UVALive 6500 Boxes

    Boxes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Pract ...

  9. UVALive 6948 Jokewithpermutation dfs

    题目链接:UVALive 6948  Jokewithpermutation 题意:给一串数字序列,没有空格,拆成从1到N的连续数列. dfs. 可以计算出N的值,也可以直接检验当前数组是否合法. # ...

  10. 【暑假】[实用数据结构]UVAlive 3135 Argus

    UVAlive 3135 Argus Argus Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %l ...

随机推荐

  1. C++中的类继承(4)继承种类之单继承&多继承&菱形继承

    单继承是一般的单一继承,一个子类只 有一个直接父类时称这个继承关系为单继承.这种关系比较简单是一对一的关系: 多继承是指 一个子类有两个或以上直接父类时称这个继承关系为多继承.这种继承方式使一个子类可 ...

  2. vue2 与后台信息交互

    vue-resource  是vue的ajax请求插件 vue-resource文档:https://github.com/vuejs/vue-resource/blob/master/docs/ht ...

  3. gulp基于seaJs模块化项目打包实践【原创】

    公司还一直在延续使用jq+seajs的技术栈,所以只能基于现在的技术栈进行静态文件打包,而众所周知seajs的打包比较"偏门",在查了不少的文档和技术分享后终于琢磨出了自己的打包策 ...

  4. 第二章 Oracle数据库应用

    第二章   Oracle数据库应用2.1 表空间和用户权限下管理    2.1.1 表空间        2.1.1.1 分类:            永久性表空间            临时性表空间 ...

  5. Java多线程学习笔记(二)——Executor,Executors,ExecutorService比较

    Executor:是Java线程池的超级接口:提供一个execute(Runnable command)方法;我们一般用它的继承接口ExecutorService. Executors:是java.u ...

  6. AngularJs 4大核心

    放弃了IE8, 4大核心: MVC: 数据模型,视图层,业务逻辑和控制模式(控制器), 为何MVC不是设计模式呢?(23种设计模式里没有MVC,MVC模式的目的就是实现Web系统的职能分工,超越了设计 ...

  7. Java设置Excel单元格式

    XSSFWorkbook wb = new XSSFWorkbook(); CellStyle style = wb.createCellStyle(); style.setBorderRight(C ...

  8. Java中的集合与线程的Demo

    一.简单线程同步问题 package com.ietree.multithread.sync; import java.util.Vector; public class Tickets { publ ...

  9. jquery 模态窗口 蒙层无法覆盖flash解决办法

    在应用swf的<object></object>标签中加入如下属性: <param name="wmode" value="transpar ...

  10. Markdown - 语法简介

    标题 在文字里书写不同数量的“#”可以完成不同的标题,如下: # 一级标题 ## 二级标题 ### 三级标题 #### 四级标题 ##### 五级标题 ###### 六级标题 列表 无序列表的使用,在 ...