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. 拼接的html的onclick事件中无法传递对象给js方法的处理办法

    如下: 拼接的html: " onclick=\"valDocName2('"+JSON.stringify(doc).replace(new RegExp(" ...

  2. ElementUI select

    https://blog.csdn.net/qq_33769914/article/details/81738278 https://blog.csdn.net/m0_37972557/article ...

  3. 新版jquery的ajax调用 , jquery1.5以上

    原文出处:http://api.jquery.com/jQuery.ajax/,该链接页面底部有代码展示 示例1: $.ajax({ method: "POST", url: &q ...

  4. asp.net SessionState模式的配置及使用

    由于项目dll文件变动比较频繁,而保存登陆的状态又保存在Session中,所以导致用户经常无故掉线(PS:dll变动的时候导致Session).有一种方法可以长期保存session,那就是sessio ...

  5. poj2185 Milking Grid【KMP】

    Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10084   Accepted: 4371 Des ...

  6. 【转】B2C电子商务系统设计精选

    B2C电子商务系统研发——促销引擎设计(一)(Promotion Engine) B2C电子商务系统研发——商品SKU分析和设计(一) B2C电子商务系统研发——商品SKU分析和设计(二) 电商后台系 ...

  7. 删除 oracle

    C:\app\Administrator\product\11.2.0\client_1\deinstall 用这个批处理文件,会把oracle全部删除,除这个目录本身以外 .另外它不删除服务,即使服 ...

  8. In ZeroDB, the client is responsible for the database logic. Data encryption, decryption, and compression also happen client side. Therefore, the server never has any knowledge about the data, its str

    zerodb/index.rst at master · zerodb/zerodb https://github.com/zerodb/zerodb/blob/master/docs/source/ ...

  9. talib 中文文档(三):talib 方法大全

    Function API Examples Similar to TA-Lib, the function interface provides a lightweight wrapper of th ...

  10. SDL结合QWidget的简单使用说明

    SDL(Simple DirectMeida Layer)是一个简单的封装媒体库,功能主要涉及了相关于OpenGL或者DirectX的显卡硬件功能和一些鼠标,键盘等外设访问.这里主要只说明一下它的渲染 ...