标签:style http color io os ar for 文件 sp
题目链接:Codeforces 466E Information Graph
题目大意:一开始有n个员工,他们互相独立。现在有三种操作。
解题思路:将每个文件移动的范围处理出来,然后对于每次询问,将询问拆成两个标记,假设查询x是否浏览过第k号文件,第k号文件的范围为u-v,那么在最后dfs时,遍历到x,判断是否经过u;遍历到v时,判断是否经过x。如果两个都满足,则是YES。
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<int,int> pii;
const int maxn = 1e5 + 5;
int N, M, K = 0, f[maxn], c[maxn], vis[maxn];
vector<int> G[maxn];
vector<pii> P, Q[maxn];
inline int getfar(int x) {
return x == f[x] ? x : f[x] = getfar(f[x]);
}
void init () {
scanf("%d%d", &N, &M);
for (int i = 0; i <= N; i++)
f[i] = i;
int op, x, y;
while (M--) {
scanf("%d", &op);
if (op == 1) {
scanf("%d%d", &x, &y);
f[x] = y;
G[y].push_back(x);
} else if (op == 2) {
scanf("%d", &x);
P.push_back(make_pair(getfar(x), x));
} else {
scanf("%d%d", &x, &y);
pii u = P[y-1];
Q[x].push_back(make_pair(u.first, K));
Q[u.second].push_back(make_pair(x, K));
K++;
}
}
}
void dfs (int u) {
vis[u] = 1;
for (int i = 0; i < G[u].size(); i++)
dfs(G[u][i]);
for (int i = 0; i < Q[u].size(); i++) {
int v = Q[u][i].first, id = Q[u][i].second;
if (vis[v])
c[id]++;
}
vis[u] = 0;
}
int main () {
init();
for (int i = 1; i <= N; i++) {
if (f[i] == i)
dfs(i);
}
for (int i = 0; i < K; i++)
printf("%s\n", c[i] == 2 ? "YES" : "NO");
return 0;
}
Codeforces 466E Information Graph(dfs+并查集)
标签:style http color io os ar for 文件 sp
原文地址:http://blog.csdn.net/keshuai19940722/article/details/39524363