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

题目链接: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;
}

转载于:https://www.cnblogs.com/Blackops/p/5971139.html

PAT (Top Level) Practise 1008 Airline Routes(Tarjan模版题)相关推荐

  1. PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)

    PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)   http://www.patest.cn/contests/pat-b-practise/1025 ...

  2. PAT (Basic Level) Practise (中文)-1034. 有理数四则运算(20)

    PAT (Basic Level) Practise (中文)-1034. 有理数四则运算(20)  http://www.patest.cn/contests/pat-b-practise/1034 ...

  3. 卡拉兹(Callatz)猜想,PAT(Basic Level) Practise NO.1001

    PAT(Basic Level) Practise NO.1001 卡拉兹(Callatz)猜想: 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n+1)砍掉一半. 这样 ...

  4. PAT乙级真题全集-PAT (Basic Level) Practise (中文)

    1001. 害死人不偿命的(3n+1)猜想 (15) 卡拉兹(Callatz)猜想: 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n+1)砍掉一半.这样一直反复砍下去, ...

  5. PAT (Advanced Level) Practise 1004 解题报告

    GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 1600 ...

  6. PAT (Basic Level) Practise 1040 有几个PAT(DP)

    1040. 有几个PAT(25) 时间限制 120 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CAO, Peng 字符串APPAPT中包含了两个单 ...

  7. PAT (Basic Level) Practise 1045 快速排序(离散化+主席树区间内的区间求和)

    1045. 快速排序(25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CAO, Peng 著名的快速排序算法里有一个经典的划分 ...

  8. PAT (Basic Level) Practise:1012. 数字分类

    [题目链接] 给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字: A1 = 能被5整除的数字中所有偶数的和: A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3- ...

  9. PAT (Basic Level) Practise:1017. A除以B

    [题目链接] 本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数.你需要输出商数Q和余数R,使得A = B * Q + R成立. 输入格式: 输入在1行中依次给出A和B,中间以1空格 ...

最新文章

  1. rabbitmq beam.smp cpu利用率过高
  2. 设计模式之 - 简单工厂模式
  3. NASA宣布发现 “第2个地球”
  4. pythonre正则表达式1012pythonre正则表达式_python re模块 正则表达式
  5. Nginx upstream 配置
  6. xml html 转化为字符串,XSLT:将字符串解析为XML节点集(concret:将HTML-String转换为节点集)?...
  7. 前端与移动开发之vue-day1(1)
  8. php实现access数据库连接,PHP实现Access数据库连接
  9. CentOS设置时间
  10. vscode-更换图标主题VSCode Icons.
  11. Hibernate 关联映射
  12. 记一次进销存软件的破解(补充)
  13. rhel7-firewalld端口转发
  14. 28岁华为员工工资表曝光,牛逼的人注定会牛逼
  15. 2019icpc徐州站 H题 Yuuki and a problem(树套树(树状数组套主席树))
  16. Java8新特性(三) – 流式数据处理
  17. 【蓝桥杯】水题 基础练习 回文数 c语言
  18. 【CC2640】CC2640架构及原理
  19. 【笔记】MOS导通条件
  20. seoer请不要把自己定位成seoer

热门文章

  1. 26期20180606 chmod chown umask 隐藏权限
  2. 关于inodes占用100%的问题及解决方法续集如何解决clientmqueue目录文件太多
  3. HDU 3695 Computer Virus on Planet Pandora (AC自己主动机)
  4. 提高Objective-C代码质量心机一:简化写法
  5. 前端开发中的性能那点事(三)php的opcode缓存
  6. 让你不富都难的28个理财习惯
  7. 我对windows核心编程的理解之一
  8. 计算机 旧词新说_如何使旧计算机再次有用
  9. 书评专家_书评:“开放”探讨开放的广泛文化含义
  10. 熔接机使用方法_熔接机社区版,欧盟委员会开源审核以及更多新闻