用tarjan算法缩点~

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+;
vector<int> g[maxn];
int N,M,x,y;
int low[maxn];
int dfn[maxn];
int cnt;
int pos[maxn];
int scc;
stack<int> st;
void tarjan (int x) {
low[x]=dfn[x]=++cnt;
st.push(x);
for (int i=;i<g[x].size();i++) {
if (!low[g[x][i]]) {
tarjan (g[x][i]);
low[x]=min(low[x],low[g[x][i]]);
}
else if (!pos[g[x][i]]) low[x]=min(low[x],dfn[g[x][i]]);
}
if (low[x]==dfn[x]) {
scc++;
while () {
int u=st.top();
st.pop();
low[u]=low[x];
pos[u]=scc;
if (u==x) break;
}
}
}
int main () {
scanf ("%d %d",&N,&M);
for (int i=;i<M;i++) {
scanf ("%d %d",&x,&y);
g[x].push_back(y);
}
for (int i=;i<=N;i++)
if (!low[i]) tarjan(i);
scanf ("%d",&M);
for (int i=;i<M;i++) {
scanf ("%d %d",&x,&y);
printf ("%s\n",low[x]==low[y]?"Yes":"No");
}
return ;
}

PAT T1008 Airline Routes的更多相关文章

  1. PAT (Top Level) Practise 1008 Airline Routes(Tarjan模版题)

    1008. Airline Routes (35) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given a ...

  2. PAT 1087 All Roads Lead to Rome[图论][迪杰斯特拉+dfs]

    1087 All Roads Lead to Rome (30)(30 分) Indeed there are many different tourist routes from our city ...

  3. PAT 1087 All Roads Lead to Rome

    PAT 1087 All Roads Lead to Rome 题目: Indeed there are many different tourist routes from our city to ...

  4. PAT甲级题分类汇编——图

    本文为PAT甲级分类汇编系列文章. 图,就是层序遍历和Dijkstra这一套,#include<queue> 是必须的. 题号 标题 分数 大意 时间 1072 Gas Station 3 ...

  5. 《转载》PAT 习题

    博客出处:http://blog.csdn.net/zhoufenqin/article/details/50497791 题目出处:https://www.patest.cn/contests/pa ...

  6. PAT Judge

    原题连接:https://pta.patest.cn/pta/test/16/exam/4/question/677 题目如下: The ranklist of PAT is generated fr ...

  7. PAT/字符串处理习题集(二)

    B1024. 科学计数法 (20) Description: 科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+ ...

  8. routes.rb和link_to的一些规则

    rails文档中描述了一个知识,link_to方法用于产生链接,但链接是根据routes.rb中的路由规则来产生的.这又分为面向资源和非面向资源两种产生链接的方法.比如 routes.rb文件中有两条 ...

  9. PAT 1041. 考试座位号(15)

    每个PAT考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位.正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座 ...

随机推荐

  1. [坑] js indexOf is not a function

    今天写js的时候,本来没有问题的代码突然出现了问题,就是本来下拉框里面在更新之后会出现内容的 但是并没有出现内容,按下F12 查看了Console之后发现确实是接收到了数据,但是却也报错了 内容是 我 ...

  2. 1.什么是Mybatis?

    在学习之前我们要回顾以下知识 JDBC Mysql java基础 Maven Junit 什么是 MyBatis? MyBatis 是一款优秀的持久层框架MyBatis 避免了几乎所有的 JDBC 代 ...

  3. Git - 06. git checkout

    1. 概述 已经学会查看 提交记录 尝试在 提交记录 之间做跳转 2. 命令 跳转到提交 概述 跳转到 日志 里的某次提交 命令 # 撤销 工作区的所有改动, 返回 当前分支 的 上一个提交 > ...

  4. python之nosetest

    nose介绍 nose下载 nose使用 -s 能够打印用例中的print信息 ➜ func_tests git:(master) ✗ nosetests -v test_app.py:TestApp ...

  5. Go_栈

    1. 栈的介绍 2. 栈的应用 3. 栈入门 package main import ( "fmt" "errors" ) //使用数组来模拟一个栈的使用 ty ...

  6. [原]eclipse中spring配置文件的自动提示和命名空间的添加

    在用spring或者springmvc框架进行开发时,编辑applicationcontext.xml等配置文件是必不可少的,在eclipse中打开applicationcontext.xml通常是这 ...

  7. Getopt::Long - Extended processing of command line options

    use Getopt::Long; my $data   = "file.dat"; my $length = 24; my $verbose; GetOptions (" ...

  8. MSComm控件进行串口编程的基本步骤

    Visual C++为我们提供了一种好用的ActiveX控件Microsoft Communications Control(即MSComm)来支持应用程序对串口的访问,在应用程序中插入MSComm控 ...

  9. spring feign依赖包

    1.feign依赖包 <properties> <java.version>1.8</java.version> <spring-cloud.version& ...

  10. LeetCode 编辑距离(DP)

    题目 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 思路 定 ...