PAT (Top Level) Practise 1008 Airline Routes(Tarjan模版题)
1008. Airline Routes (35)
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
题目链接:PAT (Top Level) Practise 1008
给m组单向边和k个询问,每次询问两个点是否互相可达……学完Tarjan就来想做这个以前一直不会的模版题了,用sc表示当前检测到的连通分量个数,belong[]数组表示当前点属于第几个连通分量,至于如何Tarjan,画个图比较好理解,DFS这种东西真是只可意会不可言传
代码:
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=1e4+7;
const int M=6e4+7;
struct edge
{
int to;
int pre;
};
edge E[M];
int head[N],tot;
int ins[N],low[N],dfn[N],st[N],top,belong[N];
int ts,sc; inline void add(int s,int t)
{
E[tot].to=t;
E[tot].pre=head[s];
head[s]=tot++;
}
void init()
{
CLR(head,-1);
tot=0;
CLR(ins,0);
CLR(low,0);
CLR(dfn,0);
ts=0;
top=0;
sc=0;
CLR(belong,-1);
}
void tar(int u)
{
dfn[u]=low[u]=++ts;
st[top++]=u;
ins[u]=1;
int v;
for (int i=head[u]; ~i; i=E[i].pre)
{
v=E[i].to;
if(!dfn[v])
{
tar(v);
low[u]=min<int>(low[u],low[v]);
}
else if(ins[v])
low[u]=min<int>(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
++sc;
do
{
v=st[--top];
ins[v]=0;
belong[v]=sc;
}while (u!=v);
}
}
int main(void)
{
int n,m,a,b,i,k;
while (~scanf("%d%d",&n,&m)&&(n||m))
{
init();
for (i=0; i<m; ++i)
{
scanf("%d%d",&a,&b);
add(a,b);
}
for (i=1; i<=n; ++i)
if(!dfn[i])
tar(i);
scanf("%d",&k);
for (i=0; i<k; ++i)
{
scanf("%d%d",&a,&b);
puts(belong[a]==belong[b]?"Yes":"No");
}
}
return 0;
}
PAT (Top Level) Practise 1008 Airline Routes(Tarjan模版题)的更多相关文章
- PAT (Top Level) Practise 1005 Programming Pattern (35)
后缀数组.排序之后得到height数组,然后从上到下将height>=len的都分为一组,然后找到第一组个数最多的输出即可. #pragma comment(linker, "/STA ...
- PAT (Basic Level) Practise (中文)-1039. 到底买不买(20)
PAT (Basic Level) Practise (中文)-1039. 到底买不买(20) http://www.patest.cn/contests/pat-b-practise/1039 小红 ...
- PAT (Basic Level) Practise (中文)- 1022. D进制的A+B (20)
PAT (Basic Level) Practise (中文)- 1022. D进制的A+B (20) http://www.patest.cn/contests/pat-b-practise/1 ...
- PAT (Basic Level) Practise (中文)- 1024. 科学计数法 (20)
PAT (Basic Level) Practise (中文)- 1024. 科学计数法 (20) http://www.patest.cn/contests/pat-b-practise/1024 ...
- PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)
PAT (Basic Level) Practise (中文)-1025. 反转链表 (25) http://www.patest.cn/contests/pat-b-practise/1025 ...
- PAT (Basic Level) Practise (中文)- 1026. 程序运行时间(15)
PAT (Basic Level) Practise (中文)- 1026. 程序运行时间(15) http://www.patest.cn/contests/pat-b-practise/10 ...
- PAT (Basic Level) Practise (中文)-1027. 打印沙漏(20)
PAT (Basic Level) Practise (中文)-1027. 打印沙漏(20) http://www.patest.cn/contests/pat-b-practise/1027 本题 ...
- PAT (Basic Level) Practise (中文)-1028. 人口普查(20)
PAT (Basic Level) Practise (中文)-1028. 人口普查(20) http://www.patest.cn/contests/pat-b-practise/1028 某 ...
- PAT (Basic Level) Practise (中文)-1029. 旧键盘(20)
PAT (Basic Level) Practise (中文)-1029. 旧键盘(20) http://www.patest.cn/contests/pat-b-practise/1029 旧键盘上 ...
随机推荐
- CSS鼠标样式整理
鼠标样式的标签: cursor:*; //该属性定义了鼠标指针放在一个元素边界范围内时所用的光标形状: 鼠标样式: 值 描述 url 需使用的自定义光标的 URL. 注释:请在此列表的末端始终定义一 ...
- VS2010中“工具>选项中的VC++目录编辑功能已被否决”解决方法
转自:http://blog.csdn.net/chaijunkun/article/details/6658923 这是VS2010的改变,不能够在“工具-选项”中看到“VC++目录”了. 但是呢, ...
- eoe项目结构
├ cn.eoe.app –存放程序全局性类的包├ cn.eoe.app.adapter –存放适配器的实现类的包 ├ cn.eoe.app.adapter.base –存放适配器基类的包├ cn.e ...
- register
register:这个关键字请求编译器尽可能的将变量存在CPU内部寄存器中,而不是通过内存寻址访问,以提高效率.注意是尽可能,不是绝对.你想想,一个CPU 的寄存器也就那么几个或几十个,你要是定义了很 ...
- Linux 内核中断内幕
转自:http://www.ibm.com/developerworks/cn/linux/l-cn-linuxkernelint/index.html#resources Linux 内核中断内幕 ...
- PostGreSQL最简安装
A.先用yum安装服务器和客户端 yum install postgresql yum install postgresql-server B.初始化数据库并启动服务 service postgres ...
- vim 多窗口
打开多个文件: 1.vim还没有启动的时候: 在终端里输入 vim file1 file2 ... filen便可以打开所有想要打开的文件 2.vim已经启动 输入 :open file 可以再打开一 ...
- ER-STUDIO 6.5工具使用帮助的中文翻译
转自于:http://yujingwang.blog.sohu.com/63362979.html 1 资料 ER-STUDIO的帮助(英文) 2 内容 2.1 ...
- android:layout_weight属性详解(转)
在android开发中LinearLayout很常用,LinearLayout的内控件的android:layout_weight在某些场景显得非常重要,比如我们需要按比例显示.android并没用提 ...
- C++primer学习笔记(一)——Chapter 3
3.1 Namespace using Declarations 1.因为C++里有名字空间的定义,例如我们使用cin的时候必须写成std::cin,如果就用一次还是可以接受的,但是如果一直都这样,那 ...