G. How Many Paths? + 强连通

题目来源G. How Many Paths? + 强连通

You are given a directed graph G which can contain loops (edges from a
vertex to itself). Multi-edges are absent in G which means that for
all ordered pairs (u,v) exists at most one edge from u to v. Vertices
are numbered from 1 to n.

A path from u to v is a sequence of edges such that:

vertex u is the start of the first edge in the path; vertex v is the
end of the last edge in the path; for all pairs of adjacent edges next
edge starts at the vertex that the previous edge ends on. We will
assume that the empty sequence of edges is a path from u to u.

For each vertex v output one of four values:

0, if there are no paths from 1 to v; 1, if there is only one path
from 1 to v; 2, if there is more than one path from 1 to v and the
number of paths is finite; −1, if the number of paths from 1 to v is
infinite. Let’s look at the example shown in the figure.

在这里插入图片描述

Then:

the answer for vertex 1 is 1: there is only one path from 1 to 1 (path with length 0);
the answer for vertex 2 is 0: there are no paths from 1 to 2;
the answer for vertex 3 is 1: there is only one path from 1 to 3 (it is the edge (1,3));
the answer for vertex 4 is 2: there are more than one paths from 1 to 4 and the number of paths are finite (two paths: [(1,3),(3,4)] and [(1,4)]);
the answer for vertex 5 is −1: the number of paths from 1 to 5 is infinite (the loop can be used in a path many times);
the answer for vertex 6 is −1: the number of paths from 1 to 6 is infinite (the loop can be used in a path many times).
Input
The first contains an integer t (1≤t≤104) — the number of test cases in the input. Then t test cases follow. Before each test case, there is an empty line.

The first line of the test case contains two integers n and m (1≤n≤4⋅105,0≤m≤4⋅105) — numbers of vertices and edges in graph respectively. The next m lines contain edges descriptions. Each line contains two integers ai, bi (1≤ai,bi≤n) — the start and the end of the i-th edge. The vertices of the graph are numbered from 1 to n. The given graph can contain loops (it is possible that ai=bi), but cannot contain multi-edges (it is not possible that ai=aj and bi=bj for i≠j).

The sum of n over all test cases does not exceed 4⋅105. Similarly, the sum of m over all test cases does not exceed 4⋅105.

Output
Output t lines. The i-th line should contain an answer for the i-th test case: a sequence of n integers from −1 to 2.

Example
inputCopy
5

6 7
1 4
1 3
3 4
4 5
2 1
5 5
5 6

1 0

3 3
1 2
2 3
3 1

5 0

4 4
1 2
2 3
1 4
4 3
outputCopy
1 0 1 2 -1 -1
1
-1 -1 -1
1 0 0 0 0
1 1 2 1

具体思路: 就是从 1 开始 把所有的环(强连通分量找出来),直接赋值成 -1 (ans[u] = -1)在根据 u == -1, 用dfs()遍历推广到每一个具有该分量的路径; 1, 2 的情况直接dfs()遍历判断即可, 最后面还要从新更新一下,1, 2, 的情况即可;

注意就是要初始化

void init() {
	cnt = 0;
	while(!st.empty()) {
		st.pop();
	}
	for(int i = 1; i <= n; i ++) {
		dfn[i] = low[i] = vis[i] = in[i] = ans[i] = 0;
		g[i].clear();
	}
}

具体看注释

/*
https://codeforces.ml/contest/1547/problem/G
      CF -- G. How Many Paths?
*/

/*
  强联通升级
 */
#include<bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 10;
int vis[maxn]; // 标记有没有遍历过
vector<int > g[maxn]; // 邻接表地图
// tarjan区域
int dfn[maxn];
int low[maxn];
int in[maxn];
int ans[maxn];
stack<int > st;
int n, m, cnt;

void init() {
	cnt = 0;
	while(!st.empty()) {
		st.pop();
	}
	for(int i = 1; i <= n; i ++) {
		dfn[i] = low[i] = vis[i] = in[i] = ans[i] = 0;
		g[i].clear();
	}
}

void tarjan(int u) {
//	cout << " u == "  << u << " ";
	low[u] = dfn[u] = ++ cnt;
	st.push(u);
	in[u] = 1;
	int sz = g[u].size();
	for(int i = 0; i < sz; i ++) {
		int v = g[u][i];
		if(v == u)  ans[u] = -1;
		if(!dfn[v]) {
			tarjan(v);
			low[u] = min(low[u], low[v]);
		} else if(in[v]) {
			low[u] = min(low[u], dfn[v]);
		}
	}
	if(dfn[u] == low[u]) {
		if(st.top() == u) {
			in[st.top()] = 0; // 只有一个, 直接出栈
			st.pop();
		} else {
			int v;
			do {
				v = st.top();
				st.pop();
				ans[v] = -1; // 强连通分量
				in[v] = 0; // 出栈
			} while(u != v);
		}
	}
}

void solve(int child, int fa) {
	if(fa == -1) // 到father有无数路,到其儿子也有
		ans[child] = -1;

	if(ans[child] == -1)
		return;

	ans[child] = min(ans[child]+fa, 2);
}

void dfs(int u) {
	if(vis[u]) return; // 标记的直接退出
	vis[u] = 1; // 标记
	int sz = g[u].size();
	for(int i = 0; i < sz; i ++) {
		int v = g[u][i];
		solve(v, ans[u]); // 处理该点;
		dfs(v);
	}
}

void dfs2(int u) {
	int sz = g[u].size();
	for(int i = 0; i < sz; i ++) {
		int v = g[u][i];
		if(ans[v] == 1) {
			ans[v] = 2;  // 还有1 条路则  超过 1, 为 2 
			dfs2(v);
		}
	}
}

int main() {
	int t;
	cin >> t;
	while(t --) {
		cin >> n >> m;
		init();
		for(int i = 1; i <= m; i ++) {
			int a, b;
			cin >> a >> b;
			g[a].push_back(b); // a可以去 b;
		}
		tarjan(1); // 根据题意从1开始遍历 
		
		// 注意这里不能直接ans[1] = 1;
		// 防止 1 形成分量  
		solve(1, 1); // 先把1到自身处理掉

		//1. 深度遍历所有的点, if 该点 ans = -1, 则其能到的点,也都为-1;
		for(int i = 1; i <= n; i ++) if(ans[i] == -1) dfs(i);
		
		dfs(1);  // 2. 1的情况 

		// 3.有多条边到父点,后面的点同样有多条边, ans = 2;
		for(int i = 1; i <= n; i ++) if(ans[i] == 2) dfs2(i);

		for(int i = 1; i <= n; i ++) cout << ans[i] << " ";

		cout << endl;
	}
	return 0;
}
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值