Information Graph

把询问离线之后就能随便搞了, 去check一下是不是祖先, 可以用倍增也能用dfs序。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long
using namespace std; const int N = 1e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; int n, m, cnt, deg[N], fa[N], op[N], x[N], y[N], depth[N], f[N][];
bool vis[N];
bool ans[N];
vector<int> G[N];
vector<int> qus[N]; int getRoot(int x) {
return x == fa[x] ? x : fa[x] = getRoot(fa[x]);
} void Merge(int x, int y) {
fa[getRoot(x)] = getRoot(y);
} void dfs(int u, int fa, int idx) {
vis[u] = idx;
depth[u] = depth[fa] + ;
f[u][] = fa;
for(int i = ; i < ; i++)
f[u][i] = f[f[u][i - ]][i - ];
for(int& v : G[u]) if(!vis[v]) dfs(v, u, idx);
} bool check(int u, int v) {
if(depth[u] < depth[v]) return false;
if(vis[u] != vis[v]) return false;
if(getRoot(u) != getRoot(v)) return false;
for(int i = ; i >= ; i--)
if(depth[f[u][i]] >= depth[v])
u = f[u][i];
return u == v;
} int main() {
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++) fa[i] = i;
for(int i = ; i <= m; i++) {
scanf("%d%d", &op[i], &x[i]);
if(op[i] != ) scanf("%d", &y[i]);
if(op[i] == ) qus[y[i]].push_back(i);
if(op[i] == ) G[y[i]].push_back(x[i]), deg[x[i]]++;
}
for(int i = ; i <= n; i++)
if(!deg[i]) dfs(i, , ++cnt);
cnt = ;
for(int i = ; i <= m; i++) {
if(op[i] == ) {
Merge(x[i], y[i]);
} else if(op[i] == ) {
cnt++;
for(auto& p : qus[cnt])
if(check(x[i], x[p]))
ans[p] = true;
}
}
for(int i = ; i <= m; i++)
if(op[i] == ) printf("%s\n", ans[i] ? "YES" : "NO");
return ;
} /**/

Codeforces 466E Information Graph的更多相关文章

  1. CodeForces 466E Information Graph --树形转线性+并查集

    题意:有三种操作: 1.新增一条边从y连向x,此前x没有父节点 2.x接到一份文件,(文件标号逐次递增),然后将这份文件一路上溯,让所有上溯的节点都接到这份文件 3.查询某个节点x是否接到过文件F 解 ...

  2. 466E - Information Graph 巧妙的判断祖先于孩子的关系

    这题说的是给了一个公司员工100000 然后现在又3种操作第一种将y置为x的父亲,第二种操作将文件给第x个人签他签完给他的上司签,一直到没有上司为止,第三种操作问x是否签了第i份文件,然后 我们只要直 ...

  3. Codeforces 466 E. Information Graph

    并查集.... E. Information Graph time limit per test 1 second memory limit per test 512 megabytes input ...

  4. Codeforces 1093D Beautiful Graph(二分图染色+计数)

    题目链接:Beautiful Graph 题意:给定一张无向无权图,每个顶点可以赋值1,2,3,现要求相邻节点一奇一偶,求符合要求的图的个数. 题解:由于一奇一偶,需二分图判定,染色.判定失败,直接输 ...

  5. Codeforces 986C AND Graph dfs

    原文链接https://www.cnblogs.com/zhouzhendong/p/9161514.html 题目传送门 - Codeforces 986C 题意 给定 $n,m (0\leq n\ ...

  6. CodeForces - 986C AND Graph

    不难想到,x有边连出的一定是 (2^n-1) ^ x 的一个子集,直接连子集复杂度是爆炸的...但是我们可以一个1一个1的消去,最后变成补集的一个子集. 但是必须当且仅当 至少有一个 a 等于 x 的 ...

  7. [Codeforces 1178D]Prime Graph (思维+数学)

    Codeforces 1178D (思维+数学) 题面 给出正整数n(不一定是质数),构造一个边数为质数的无向连通图(无自环重边),且图的每个节点的度数为质数 分析 我们先构造一个环,每个点的度数都是 ...

  8. Codeforces 986C - AND Graph(dfs)

    Codeforces 题面传送门 & 洛谷题面传送门 考虑 DFS 一遍遍历每个连通块. 当我们遍历到一个点 \(x\) 时,我们就建立一个虚点 \((2^n-1-x)'\) 表示我们要访问 ...

  9. Codeforces 1288F - Red-Blue Graph(上下界网络流)

    Codeforces 题面传送门 & 洛谷题面传送门 好久没有写过上下界网络流了,先来一题再说( 首先先假设所有边都是蓝边,那么这样首先就有 \(b\times m\) 的花费,但是这样不一定 ...

随机推荐

  1. 洛谷P2542 [AHOI2005]航线规划(LCT,双连通分量,并查集)

    洛谷题目传送门 太弱了不会树剖,觉得LCT好写一些,就上LCT乱搞,当LCT维护双连通分量的练手题好了 正序删边是不好来维护连通性的,于是就像水管局长那样离线处理,逆序完成操作 显然,每个点可以代表一 ...

  2. 【CF605E】Intergalaxy Trips(贪心,动态规划)

    [CF605E]Intergalaxy Trips(贪心,动态规划) 题面 Codeforces 洛谷 有\(n\)个点,每个时刻第\(i\)个点和第\(j\)个点之间有\(p_{ij}\)的概率存在 ...

  3. linux运维之分析日志相关命令(1)

    一.分析日志 1.查看有多少IP访问 awk '{print $1}' log_file|sort|uniq|wc -l 2.查看某一个页面被访问的次数 grep "/index.php&q ...

  4. spark性能调优(二) 彻底解密spark的Hash Shuffle

    装载:http://www.cnblogs.com/jcchoiling/p/6431969.html 引言 Spark HashShuffle 是它以前的版本,现在1.6x 版本默应是 Sort-B ...

  5. bzoj 4328 始祖鸟

    4328: JSOI2012 始祖鸟 Time Limit: 10 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 76  Solved: 52[ ...

  6. angularJs实现动态增加输入框

    摘要:首先,有一个这样的需求,就是说,我点击添加,会动态出现需要输入的输入框.我们需要定义一个对象,类似这种, {spc:{},spctions:[]} 意思是spc对应的是一个对象,spctions ...

  7. Logistic Regression – Geometric Intuition

    Logistic Regression – Geometric Intuition Everybody who has taken a machine learning course probably ...

  8. 论文中“but”与“however”的区别

  9. VMware Linux 下 Nginx 安装配置 (一)

    资源准备 1. pcre-8.34.tar.gz: ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 2. zlib-1.2.8.tar.g ...

  10. 360 / 小米 / 百度 随身wifi Ubuntu 下作为无线网卡使用

    这篇文章说得其实很好了,http://www.freemindworld.com/blog/2013/131010_360_wifi_in_linux.shtml 不过因为专利问题,官网貌似不直接提供 ...