CodeForces 652E Pursuit For Artifacts 边双连通分量

题意:

给出一个无向连通图,每条边为黑色或者白色。
每条边至多走一次,询问是否可以从\(a\)走到\(b\)且至少经过一条黑色边。

分析:

先找出所有的边双连通分量,然后缩点成树。
如果在同一个边双连通分量中有一条黑边,那么一定存在一个环经过这条黑边并且回到原点。
缩点后,如果某一双连通分量中有黑边,就把它对应的树节点标记为黑色节点。
最终询问的是树的某一路径中是否含有黑边或黑点。
所以把树DFS一遍,向上找LCA即可。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;

const int maxn = 300000 + 10;

struct Edge
{
	int v, color, nxt;
	Edge() {}
	Edge(int v, int c, int n): v(v), color(c), nxt(n) {}
};

int n, m;

struct Graph
{
	int ecnt, head[maxn];
	Edge edges[maxn * 2];

	void init() { ecnt = 0; memset(head, -1, sizeof(head)); }

	void AddEdge(int u, int v, int color) {
		edges[ecnt] = Edge(v, color, head[u]);
		head[u] = ecnt++;
	}
};

Graph g, t;

stack<int> S;
int dfs_clock, pre[maxn], low[maxn];
int scc_cnt, sccno[maxn];

void dfs(int u, int fa) {
	pre[u] = low[u] = ++dfs_clock;
	S.push(u);
	bool flag = false;
	for(int i = g.head[u]; ~i; i = g.edges[i].nxt) {
		int v = g.edges[i].v;
		if(v == fa && !flag) { flag = true; continue; }
		if(!pre[v]) {
			dfs(v, u);
			low[u] = min(low[u], low[v]);
		} else if(!sccno[v]) low[u] = min(low[u], pre[v]);
	}

	if(low[u] == pre[u]) {
		scc_cnt++;
		for(;;) {
			int x = S.top(); S.pop();
			sccno[x] = scc_cnt;
			if(x == u) break;
		}
	}
}

void find_scc() {
	dfs_clock = scc_cnt = 0;
	memset(pre, 0, sizeof(pre));
	memset(sccno, 0, sizeof(sccno));
	for(int i = 1; i <= n; i++) if(!pre[i])
		dfs(i, 0);
}

int a, b;

int colv[maxn];

int fa[maxn], dep[maxn], cole[maxn];

void dfs2(int u) {
	for(int i = t.head[u]; ~i; i = t.edges[i].nxt) {
		int v = t.edges[i].v;
		if(v == fa[u]) continue;
		fa[v] = u;
		dep[v] = dep[u] + 1;
		if(t.edges[i].color) cole[v] = 1;
		dfs2(v);
	}
}

bool query(int u, int v) {
	if(colv[u] || colv[v]) return true;
	if(dep[u] < dep[v]) swap(u, v);
	while(dep[u] > dep[v]) {
		if(cole[u]) return true;
		u = fa[u];
		if(colv[u]) return true;
	}
	while(u != v) {
		if(cole[u] || cole[v]) return true;
		u = fa[u]; v = fa[v];
		if(colv[u] || colv[v]) return true;
	}
	return false;
}

int main()
{
	scanf("%d%d", &n, &m);
	g.init();
	while(m--) {
		int u, v, c; scanf("%d%d%d", &u, &v, &c);
		g.AddEdge(u, v, c); g.AddEdge(v, u, c);
	}
	scanf("%d%d", &a, &b);

	find_scc();

	t.init();
	for(int u = 1; u <= n; u++) {
		for(int i = g.head[u]; ~i; i = g.edges[i].nxt) {
			int v = g.edges[i].v;
			int c = g.edges[i].color;
			if(sccno[u] == sccno[v]) {
				if(c == 1) colv[sccno[u]] = 1;
			} else {
				t.AddEdge(sccno[u], sccno[v], c);
			}
		}
	}

	dfs2(1);
	if(query(sccno[a], sccno[b])) puts("YES");
	else puts("NO");

	return 0;
}
posted @ 2016-04-05 15:31  AOQNRMGYXLMV  阅读(341)  评论(0编辑  收藏  举报