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 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;int n, m, cnt, deg[N], fa[N], op[N], x[N], y[N], depth[N], f[N][20];
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] + 1;f[u][0] = fa;for(int i = 1; i < 20; i++)f[u][i] = f[f[u][i - 1]][i - 1];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 = 19; i >= 0; 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 = 1; i <= n; i++) fa[i] = i;for(int i = 1; i <= m; i++) {scanf("%d%d", &op[i], &x[i]);if(op[i] != 2) scanf("%d", &y[i]);if(op[i] == 3) qus[y[i]].push_back(i);if(op[i] == 1) G[y[i]].push_back(x[i]), deg[x[i]]++;}for(int i = 1; i <= n; i++)if(!deg[i]) dfs(i, 0, ++cnt);cnt = 0;for(int i = 1; i <= m; i++) {if(op[i] == 1) {Merge(x[i], y[i]);} else if(op[i] == 2) {cnt++;for(auto& p : qus[cnt])if(check(x[i], x[p]))ans[p] = true;}}for(int i = 1; i <= m; i++)if(op[i] == 3) printf("%s\n", ans[i] ? "YES" : "NO");return 0;
}/**/

转载于:https://www.cnblogs.com/CJLHY/p/10391176.html

Codeforces 466E Information Graph相关推荐

  1. CodeForces - 1494E A-Z Graph(构造+思维)

    题目链接:https://vjudge.net/problem/CodeForces-1494E 题目大意:给出一个初始时只有 nnn 个点的有向带权图,需要执行 mmm 次操作,每次操作分为下列三种 ...

  2. Codeforces 1093D Beautiful Graph

    题目链接:http://codeforces.com/contest/1093/problem/D 第一次做这种题目,染色+组合数学.. 假设这个图里有奇数环,那么肯定是不行的,因为无论怎么样都是偶数 ...

  3. codeforces CF986C AND Graph 建圖 dfs

    $ \rightarrow $ 戳我進CF原題 C. AND Graph time limit per test: 4 seconds memory limit per test: 256 megab ...

  4. codeforces gym-101745 C-Infinite Graph Game 分块

    题意 题目链接 给出一个顶点带权无向图. 定义访问操作:访问一个点,就要把与这个点相邻的点的权值全部都加到答案里去,然后给这个顶点的权值/2.现在给出一个无穷的访问序列中的一个循环节,求最终答案的极限 ...

  5. Codeforces 题目合集+分类+代码 【Updating...】【361 in total】

    961A - Tetris                                                模拟                                      ...

  6. paper reading:[renormalization]Semi-supervised Classification with Graph Convolutional Networks

    paper reading:[Renormalization Trick] Semi-supervised classification with graph convolutional networ ...

  7. A Comprehensive Survey on Graph Neural Networks(图神经网络综合研究)

    A Comprehensive Survey on Graph Neural Networks 图神经网络综合研究 Zonghan Wu, Shirui Pan, Member, IEEE, Feng ...

  8. 阅读Skeleton-Based Action Recognition with Directed Graph Neural Networks(CVPR2019)

      Skeleton-Based Action Recognition with Directed Graph Neural Networks是Lei Shi等人的另一篇文章,和之前的2s-AGCN以 ...

  9. Knowledge Graph Embedding: A Survey of Approaches and Applications 摘要

    想看原文戳这里 1. introduction (1) the definition of KG A KG is a multi-relational graph composed of entiti ...

最新文章

  1. caffe 下一些参数的设置
  2. html动态加载js方法,如何通过JavaScript动态加载js
  3. 【经典课程】李宏毅机器学习2020版正式上线!!!
  4. 第10章 指针(五) 使用地址-------指针的引用
  5. ECSHOP 助手使用
  6. TensorFlow中设置学习率的方式
  7. Oracle 12c In-Memory Option应用解析
  8. 你应该知道的Excel 2007小技巧
  9. Java 线程的生命周期
  10. 数据库-事务并发操作问题及并发的控制
  11. IdentityServer4 4.x版本 配置Scope的正确姿势
  12. WordPress /wp-admin/includes/post.php user_ID 参数操作权限提升漏洞
  13. python 写入文件编码_使用Python写入文本文件时的编码问题
  14. Unix安装BerkeleyDB
  15. 计算机顶会论文写作科研利器
  16. anaconda pycharm_搭建 Python 高效开发环境: Pycharm + Anaconda
  17. 解决PCoIP连接View 5.0虚拟桌面超时断开导致无法再次登录的问题
  18. C++ std::move/std::forward/完美转发
  19. 树莓派学习2-连接蓝牙音箱进行语音播放
  20. 程序员福音,关于如何使用Markdown写出一份漂亮的简历 —— 程序员简历 | md文档简历制作教程

热门文章

  1. uml边界类例子_面向对象UML笔记
  2. js的tree数组对象扁平化思否_js 中的tree型数组如何去重与合并?
  3. html实现横向轮播,js实现横向轮播效果
  4. 样条线怎么挤出平面_最速降线的故事
  5. Another Distinct Values
  6. java url压缩_URL短地址压缩算法 微博短地址原理解析 (Java实现)
  7. Go语言基础进阶—程序结构—命名
  8. 安装PaddleOCR遇到ERROR: Command errored out with exit status 1:command: ‘f:\python3.7\python.exe‘ -u -c
  9. 牛客网暑期ACM多校训练营(第五场): A. gpa(01分数规划)
  10. qscoj:默契值(状压DP)