zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    js经典试题之数据类型
    js单行写一个评级组件
    Spring Cloud Gateway的全局异常处理
    spring cloud gateway 全局过滤器
    公司ES升级带来的坑怎么填?
    Sentinel: 分布式系统的流量防卫兵
    微信小程序之页面打开数量限制
    微信小程序登录那些事
    API数据加密框架monkey-api-encrypt
    微信小程序之启动页的重要性
  • 原文地址:https://www.cnblogs.com/AOQNRMGYXLMV/p/5355275.html
Copyright © 2011-2022 走看看