题意:给定一棵树的公司职员管理图,有两种操作,

第一种是 T x y,把 x 及员工都变成 y,

第二种是 C x 询问 x 当前的数。

析:先把该树用dfs遍历,形成一个序列,然后再用线段树进行维护,很简单的线段树。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 5e4 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} vector<int> G[maxn];
int in[maxn], out[maxn];
int cnt;
int sum[maxn<<2], setv[maxn<<2]; void dfs(int u){
in[u] = ++cnt;
for(int i = 0; i < G[u].size(); ++i)
dfs(G[u][i]);
out[u] = cnt;
} void push_down(int rt){
if(setv[rt] == -1) return ;
int l = rt<<1, r = rt<<1|1;
sum[l] = sum[r] = setv[rt];
setv[l] = setv[r] = setv[rt];
setv[rt] = -1;
} void update(int L, int R, int val, int l, int r, int rt){
if(L <= l && r <= R){
sum[rt] = val;
setv[rt] = val;
return ;
}
push_down(rt);
int m = l + r >> 1;
if(L <= m) update(L, R, val, lson);
if(R > m) update(L, R, val, rson);
} int query(int M, int l, int r, int rt){
if(l == r) return sum[rt];
push_down(rt);
int m = l + r >> 1;
return M <= m ? query(M, lson) : query(M, rson);
} int main(){
int T; cin >> T;
for(int kase = 1; kase <= T; ++kase){
scanf("%d", &n);
for(int i = 1; i <= n; ++i) G[i].clear();
memset(in, 0, sizeof in);
for(int i = 1; i < n; ++i){
int u, v;
scanf("%d %d", &u, &v);
G[v].push_back(u);
++in[u];
}
cnt = 0;
for(int i = 1; i <= n; ++i)
if(!in[i]){ dfs(i); break; }
memset(setv, -1, sizeof setv);
memset(sum, -1, sizeof sum); scanf("%d", &m);
char s[5];
printf("Case #%d:\n", kase);
while(m--){
int x, y;
scanf("%s %d", s, &x);
if(s[0] == 'C') printf("%d\n", query(in[x], 1, n, 1));
else {
scanf("%d", &y);
update(in[x], out[x], y, 1, n, 1);
}
}
}
return 0;
}

  

HDU 3974 Assign the task (DFS+线段树)的更多相关文章

  1. HDU 3974 Assign the task 暴力/线段树

    题目链接: 题目 Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...

  2. hdu 3974 Assign the task(线段树)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3974 题意:给定一棵树,50000个节点,50000个操作,C x表示查询x节点的值,T x y表示更 ...

  3. HDU - 3974 Assign the task (线段树区间修改+构建模型)

    https://cn.vjudge.net/problem/HDU-3974 题意 有一棵树,给一个结点分配任务时,其子树的所有结点都能接受到此任务.有两个操作,C x表示查询x结点此时任务编号,T ...

  4. hdu 3974 Assign the task (线段树+树的遍历)

    Description There is a company that has N employees(numbered from 1 to N),every employee in the comp ...

  5. HDU 3974 Assign the task (DFS序 + 线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 给你T组数据,n个节点,n-1对关系,右边的是左边的父节点,所有的值初始化为-1,然后给你q个操 ...

  6. HDU 3974 Assign the task(DFS序+线段树单点查询,区间修改)

    描述There is a company that has N employees(numbered from 1 to N),every employee in the company has a ...

  7. HDU 3974 Assign the task(dfs建树+线段树)

    题目大意:公司里有一些员工及对应的上级,给出一些员工的关系,分配给某员工任务后,其和其所有下属都会进行这项任务.输入T表示分配新的任务, 输入C表示查询某员工的任务.本题的难度在于建树,一开始百思不得 ...

  8. hdu 3974 Assign the task(dfs序上线段树)

    Problem Description There is a company that has N employees(numbered from 1 to N),every employee in ...

  9. HDU 3974 Assign the task 并查集/图论/线段树

    Assign the task Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

随机推荐

  1. PageRank算法原理及实现

    PageRank算法原理介绍 PageRank算法是google的网页排序算法,在<The Top Ten Algorithms in Data Mining>一书中第6章有介绍.大致原理 ...

  2. BZOJ - 2618 凸多边形 (半平面交)

    题意:求n个凸多边形的交面积. 半平面交模板题. #include<bits/stdc++.h> using namespace std; typedef long long ll; ty ...

  3. LeetCode Valid Palindrome II

    原题链接在这里:https://leetcode.com/problems/valid-palindrome-ii/description/ 题目: Given a non-empty string  ...

  4. git常用命令备忘录

    返回未修改状态 [git checkout . --没有的提交的,都返回到原来的状态  git clean -xdf 删除文件和目录] git checkout . && git cl ...

  5. IntellJ IDEA快捷键汇总

    今天开始使用IDEA,各种不习惯,一会Eclipse一会IDEA来回切换,需要一个熟悉的过程,记录一下常用的快捷键. IDEA常用快捷键 Alt+回车 导入包,自动修正Ctrl+N  查找类Ctrl+ ...

  6. (C#)Windows Shell 外壳编程系列2 - 解释,从“桌面”开始展开

    (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一篇:(C#)Windows Shell 外壳编程系列1 - 基础,浏览一个文件夹 让我们详细解释一下 Shell 编程中最基本 ...

  7. [转]【技术心得】Last-Modified,Etag,Expire区别

    Last-Modified 是什么 Last-Modified 是 HttpHeader 中的资源的最后修改时间,如果带有 Last-Modified ,下一次发送 Http 请求时,将会发生带 If ...

  8. HTML5通信

    跨文档消息传输 HTML5中提供了在网页文档之间互相接收与发送信息的功能.使用这个功能只要获取到网页所在窗口对象的实例,无论是否同源都可以实现跨域通信.经常用于不同frame之间的通信. 当我们想要接 ...

  9. Java-API:javax.servlet.http.HttpServletRequest

    ylbtech-Java-API:javax.servlet.http.HttpServletRequest 1.返回顶部 1. javax.servlet.http Interface HttpSe ...

  10. MySQL 常用启动,关闭,登录脚本

    MySQL 常用启动,关闭,登录脚本 规范化mysql的启动,关闭,登录 1 cat mysql_env.ini #set env MYSQL_USER=system #注意用户权限 MYSQL_PA ...