http://codeforces.com/contest/812/problem/D

题意:

现在有n个孩子,m个玩具,每次输入x y,表示x孩子想要y玩具,如果y玩具没人玩,那么x就可以去玩,如果y有人玩的话,x就必须等待y玩完后才能玩。如果出现循环,那么这个循环里的孩子都会哭。

现在有q次询问,如果加入x y,会有多少孩子哭。

思路:

建立一棵树,根结点就是第一个玩玩具y的人,它的子树就是等待玩具的人(子树按照等待顺序建树)。这样就会形成很多棵树。

这样的话,对于每个询问,我们去找到最后一个玩y玩具的人,只有y玩完了那才会给x,那么这样的话这两个人也应该连一条边,如果连边后形成了环,那么这个环里的顶点数就是哭的孩子数。

其实这就是判断祖先的问题,如果x是最后一个玩y玩具的人的祖先,那么他们连边后肯定是要形成环的。

那么怎么判断两个点的祖先关系呢?

对树dfs,记录每个顶点的访问次序标号in和结束访问标号out,如果x是y的祖先就需要满足in【i】<in【y】<out【i】

在这道题目中每个人最多只会等待一个玩具,所以在树中,每个结点最多只有一个子节点,那么我们就可以根据out和in标记来计算环中的顶点个数。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
typedef pair<int,long long> pll;
const int INF = 0x3f3f3f3f;
const int maxn=1e5+; int n,m,k,q;
vector<int> g[maxn];
int in[maxn],out[maxn];
int vis[maxn];
int dfs_clock; //记录访问标记和结束标记
void dfs(int u)
{
in[u]=++dfs_clock;
for(int i=;i<g[u].size();i++)
{
dfs(g[u][i]);
}
out[u]=dfs_clock;
} int main()
{
//freopen("input.txt","r",stdin);
scanf("%d%d%d%d",&n,&m,&k,&q);
for(int i=;i<=k;i++)
{
int x,y;
scanf("%d%d",&x,&y);
if(vis[y])
{
g[vis[y]].push_back(x); //x需要等待,vis[y]玩好后给x
}
vis[y]=x;
}
dfs_clock=;
for(int i=;i<=n;i++)
{
if(!in[i])
dfs(i);
} while(q--)
{
int x,y;
scanf("%d%d",&x,&y);
y=vis[y]; //找到最后一个玩y的人
if(in[x]<=in[y] && out[x]>=in[y])
printf("%d\n",out[x]-in[x]+);
else
puts("");
} return ;
}

Codeforces Round #417 (Div. 2) D. Sagheer and Kindergarten(树中判祖先)的更多相关文章

  1. Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister —— DP

    题目链接:http://codeforces.com/problemset/problem/812/B B. Sagheer, the Hausmeister time limit per test ...

  2. Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  3. Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister

    http://codeforces.com/contest/812/problem/B 题意: 有n层楼,每层楼有m个房间,1表示灯开着,0表示灯关了.最两侧的是楼梯. 现在每从一个房间移动到另一个房 ...

  4. Codeforces Round #417 (Div. 2)-A. Sagheer and Crossroad

    [题意概述] 在一个十字路口 ,给定红绿灯的情况, 按逆时针方向一次给出各个路口的左转,直行,右转,以及行人车道,判断汽车是否有可能撞到行人 [题目分析] 需要在逻辑上清晰,只需要把所有情况列出来即可 ...

  5. 【二分】Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market

    傻逼二分 #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; ll ...

  6. 【动态规划】Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister

    预处理每一层最左侧的1的位置,以及最右侧的1的位置. f(i,0)表示第i层,从左侧上来的最小值.f(i,1)表示从右侧上来. 转移方程请看代码. #include<cstdio> #in ...

  7. [Codeforces Round#417 Div.2]

    来自FallDream的博客,未经允许,请勿转载,谢谢. 有毒的一场div2 找了个1300的小号,结果B题题目看错没交  D题题目剧毒 E题差了10秒钟没交上去. 233 ------- A.Sag ...

  8. Codeforces Round #417 (Div. 2)A B C E 模拟 枚举 二分 阶梯博弈

    A. Sagheer and Crossroads time limit per test 1 second memory limit per test 256 megabytes input sta ...

  9. Codeforces Round #417 (Div. 2) 花式被虐

    A. Sagheer and Crossroads time limit per test 1 second memory limit per test 256 megabytes input sta ...

随机推荐

  1. NAS设备是什么

    1.NAS含义: NAS(Network Attached Storage)网络存储基于标准网络协议实现数据传输,为网络中的Windows / Linux / Mac OS 等各种不同操作系统的计算机 ...

  2. [Bootstrap] install Bootstrap framework in window 7 by npm

    Install with npm You can also install Bootstrap using npm: $ npm install bootstrap require('bootstra ...

  3. Think PHP递归重新排序无限极子分类数组(递归无限极分类)

    Think PHP递归重新排序无限极子分类数组 // 递归重新排序无限极子分类数组 function recursive($array,$pid=0,$level=0){ $arr = array() ...

  4. poj2376 Cleaning Shifts【线段树】【DP】

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32561   Accepted: 7972 ...

  5. poj3171 Cleaning Shifts【线段树(单点修改区间查询)】【DP】

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4422   Accepted: 1482 D ...

  6. 焦作网络赛B-Mathematical Curse【dp】

    A prince of the Science Continent was imprisoned in a castle because of his contempt for mathematics ...

  7. tomcat设置编码格式utf8

    利用request.setCharacterEncoding("UTF-8");来设置Tomcat接收请求的编码格式,只对POST方式提交的数据有效,对GET方式提交的数据无效! ...

  8. Romantic---hdu2669(扩展欧几里德模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2669 详解:扩展欧几里德 #include <iostream> #include < ...

  9. Idea的注入和自动编译配置

    实时编译: 第二个(防止编译时Autowired报错): 修改成:

  10. thinkcmf安装教程与目录结构详解 快速上手版

    最近接了一个建站项目,要求用thinkcmf来搭建,ytkah在想php都大致一样吧,快速地下载安装包,可是!怎么安装呢?没看到安装指引文件或目录,查看了安装说明public目录做为网站根目录,入口文 ...