PAT (Top Level) Practise 1008 Airline Routes (35)

1008. Airline Routes (35)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

Given a map of airline routes, you are supposed to check if a round trip can be planned between any pair of cities.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (2<= N <= 104) and M (<=6N), which are the total number of cities (hence the cities are numbered from 1 to N) and the number of airline routes, respectively. Then M lines follow, each gives the information of a route in the format of the source city index first, and then the destination city index, separated by a space. It is guaranteed that the source is never the same as the destination.

After the map information, another positive integer K is given, which is the number of queries. Then K lines of queries follow, each contains a pair of distinct cities' indices.

Output Specification:

For each query, output in a line "Yes" if a round trip is possible, or "No" if not.

Sample Input:
12 19
3 4
1 3
12 11
5 9
6 2
3 2
10 7
9 1
7 12
2 4
9 5
2 6
12 4
11 10
4 8
8 12
11 8
12 7
1 5
20
11 4
12 7
3 6
2 3
5 3
3 9
4 3
8 3
8 10
10 11
7 8
7 1
9 5
1 9
2 6
3 1
3 12
7 3
6 9
6 8
Sample Output:
Yes
Yes
No
No
No
No
No
No
Yes
Yes
Yes
No
Yes
Yes
Yes
No
No
No
No

No

题意是给出一个有向图,然后问两个点在不在一个环上。直接上tarjan强连通即可,是一道裸题,只要判断low【x】和low【y】是不是相同即可

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<stack>
#include<algorithm>
#include<bitset>
#include<functional>
using namespace std;
typedef unsigned long long ull;
typedef long long LL;
const int maxn = 2e5 + 10;
int n, m, x, y, tot, t;
int dfn[maxn], low[maxn];
int ft[maxn], nt[maxn], v[maxn];
int vis[maxn];
stack<int> p;

void tarjan(int x)
{
	dfn[x] = low[x] = ++t;
	p.push(x); vis[x] = 1;
	for (int i = ft[x]; i != -1; i = nt[i])
	{
		if (!dfn[v[i]])
		{
			tarjan(v[i]);
			low[x] = min(low[x], low[v[i]]);
		}
		else if (vis[v[i]]) {
			low[x] = min(low[x], dfn[v[i]]);
		}
	}
	if (low[x] == dfn[x])
	{
		int u;
		do
		{
			u = p.top();  p.pop();
			low[u] = low[x];
			vis[u] = 0;
		} while (u != x);
	}
}

int main()
{
	while (scanf("%d%d", &n, &m) != EOF)
	{
		tot = t = 0;
		while (!p.empty()) p.pop();
		for (int i = 1; i <= n; i++) ft[i] = -1, dfn[i] = vis[i] = 0;
		while (m--)
		{
			scanf("%d%d", &x, &y);
			v[tot] = y;  nt[tot] = ft[x]; ft[x] = tot++;
		}
		for (int i = 1; i <= n; i++) if (!dfn[i]) tarjan(i);
		scanf("%d", &m);
		while (m--)
		{
			scanf("%d%d", &x, &y);
			printf("%s\n", low[x] == low[y] ? "Yes" : "No");
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值