poj2762 (Tarjan + dp找最长链)

题意:

给定一副有向图,选择两个点v和u,要求v能到达u或者u能到达v。

问是否可以对于图中的每一个点对都能满足条件?输出Yes或No。


思路:

强连通分量中的点可以看作一个点,所以先tarjan缩点。

然后当且仅当缩点图是一条链时才能满足任意一个点对都能从一点到达另一点。

因为如果缩点图有分叉,则分叉之间一定是不可达的。

所以只要dp求最长链,如果长度就是缩点后点的个数则输出Yes,否则输出No。


代码(1876K,391MS):

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

int T, n, m, t;
vector edges[1005];
vector tree[1005];
int dp[1005];
bool flag[1005][1005];
stack s;
int dfn[1005];
int low[1005];
int vis[1005];
int col[1005];
int cnt, num;

void dfs(int u) {
	s.push(u);
	vis[u] = 1;
	dfn[u] = low[u] = ++cnt;
	for (int i = 0; i < edges[u].size(); i++) {
		int v = edges[u][i];
		if (!dfn[v]) {
			dfs(v);
			low[u] = min(low[u], low[v]);
		} else if (vis[v])
			low[u] = min(low[u], dfn[v]);
	}
	if (low[u] == dfn[u]) {
		num++;
		do {
			t = s.top();
			s.pop();
			vis[t] = 0;
			col[t] = num;
		} while (t != u);
	}
}

void tarjan() {
	while (!s.empty()) s.pop();
	memset(dfn, 0, sizeof(dfn));
	memset(low, 0, sizeof(low));
	memset(vis, 0, sizeof(vis));
	memset(col, 0, sizeof(col));
	cnt = num = 0;
	for (int i = 1; i <= n; i++)
		if (!dfn[i]) dfs(i);
}

int DP(int x) {
	if (dp[x] != -1) return dp[x];
	int ans = 0;
	for (int i = 0; i < tree[x].size(); i++) {
		int nex = tree[x][i];
		ans = max(ans, DP(nex));
	}
	dp[x] = ++ans;
	return ans;
}

int main() {
	scanf("%d", &T);
	while (T--) {
		scanf("%d %d", &n, &m);
		for (int i = 0; i <= n; i++) {
			tree[i].clear();
			edges[i].clear();
		}
		int a, b;
		for (int i = 0; i < m; i++) {
			scanf("%d %d", &a, &b);
			edges[a].push_back(b);
		}
		tarjan();
		memset(flag, 0, sizeof(flag));
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j < edges[i].size(); j++) {
				if (col[i] != col[edges[i][j]] && !flag[col[i]][col[edges[i][j]]]) {
					flag[col[i]][col[edges[i][j]]] = 1;
					tree[col[i]].push_back(col[edges[i][j]]);
				}
			}
		}
		memset(vis, 0, sizeof(vis));
		memset(dp, -1, sizeof(dp));
		int ans = 0;
		for (int i = 1; i <= num; i++)
			ans = max(ans, DP(i));
		if (ans == num) printf("Yes\n");
		else printf("No\n");
	}
	return 0;
}


你可能感兴趣的:(图论)