80pts求调
查看原帖
80pts求调
905064
Disjoint_Set楼主2023/9/13 20:06

rt, WA on #6 #9

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

const int N = 80005, M = 800005;
int n, m, D;
int cnt, head[N], nxt[M<<1], to[M<<1];
int num, tot, dfn[N], low[N], c[N]; bool cut[N];
int cntc, headc[N], nxtc[M<<1], toc[M<<1];
int fa[N], dep[N], siz[N], son[N], top[N];

void add(int u, int v) {
	to[++cnt] = v;
	nxt[cnt] = head[u];
	head[u] = cnt;
}

void addc(int u, int v) {
	toc[++cntc] = v;
	nxtc[cntc] = headc[u];
	headc[u] = cntc;
}

void tarjan(int x) {
	dfn[x] = low[x] = ++num;
	int flag = 0;
	for(int i = head[x]; i; i = nxt[i]) {
		int y = to[i];
		if(!dfn[y]) {
			tarjan(y);
			low[x] = min(low[x], low[y]);
			if(dfn[x] <= low[y]) {
				flag++;
				if(x != 1 || flag > 1) cut[x] = true;
			}
		}
		else low[x] = min(low[x], dfn[y]);
	}
}

void dfs(int x) {
	c[x] = tot;
	for(int i = head[x]; i; i = nxt[i]) {
		int y = to[i];
		if(cut[y] || c[y]) continue;
		dfs(y);
	}
}

void dfs1(int x, int f, int d) {
	fa[x] = f, dep[x] = d, siz[x] = 1; int maxson = -1;
	for(int i = headc[x]; i; i = nxtc[i]) {
		int y = toc[i]; if(y == f || dep[y]) continue;
		dfs1(y,x,d+1);
		siz[x] += siz[y];
		if(siz[y] > maxson) son[x] = y, maxson = siz[y]; 
	}
}

void dfs2(int x, int topf) {
	top[x] = topf;
	if(!son[x]) return;
	dfs2(son[x],topf);
	for(int i = headc[x]; i; i = nxtc[i]) {
		int y = toc[i]; if(y == son[x] || y == fa[x] || top[y]) continue;
		dfs2(y,y);
	}
}

int lca(int x, int y) {
	while(top[x] != top[y]) {
		if(dep[top[x]] < dep[top[y]]) swap(x,y);
		x = fa[top[x]];
	}
	if(dep[x] > dep[y]) swap(x,y);
	return x;
}

int dis(int x, int y) {
	return dep[x]+dep[y]-2*dep[lca(x,y)];
}

int main() {
	scanf("%d%d",&n,&m);
	for(int i = 1; i <= m; i++) {
		int u, v; scanf("%d%d",&u,&v);
		add(u,v), add(v,u);
	}
	tarjan(1);
	for(int i = 1; i <= n; i++) {
		if(c[i]) continue;
		if(cut[i]) c[i] = ++tot;
		else ++tot, dfs(i);
	}
	for(int x = 1; x <= n; x++) {
		for(int i = head[x]; i; i = nxt[i]) {
			int y = to[i], X = c[x], Y = c[y];
			if(X == Y) continue;
			addc(X,Y);
		}
	}
	dfs1(1,0,1);
	dfs2(1,1);
	scanf("%d",&D);
	while(D--) {
		int s, t, q; scanf("%d%d%d",&s,&t,&q); int S = c[s], T = c[t], Q = c[q];
		if(!cut[q]) {
			printf("no\n");
			continue;
		}
		
		if(dis(S,T) == dis(S,Q)+dis(T,Q)) printf("yes\n");
		else printf("no\n");
	}
	return 0;
} 
2023/9/13 20:06
加载中...