用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. Go_Json序列化

    1. json介绍 2. json格式说明 3. json序列化 3.1 结构体序列化 package main import ( "fmt" "encoding/jso ...

  2. 字符串类型:char,varchar,text,enum,set

    字符串类型 1.char 格式:char (M) 2.varchar 格式:varchar (M) [注意]M表示允许的字符串长度[65535].M表示的是字符数,而不是字节数.但是最大长度的使用是按 ...

  3. mongodb的一些操作

    插入: db.inventory.insert( { _id: 10, type: "misc", item: "card", qty: 15 } ) db.i ...

  4. 为什么要使用wsgi协议

    一个cs模型是由服务器和客户端组成,大多相互情况下也就是服务器端和浏览器之间的通信.通过浏览器请求服务器,然后服务器再响应浏览器. 那么如果浏览器想要请求一个python文件,例如http://127 ...

  5. php 加解密函数

    PHP 加密解密函数: /** * 系统加密方法 * @param string $data 要加密的字符串 * @param string $key 加密密钥 * @param int $expir ...

  6. 关于宽搜BFS广度优先搜索的那点事

    以前一直知道深搜是一个递归栈,广搜是队列,FIFO先进先出LILO后进后出啥的.DFS是以深度作为第一关键词,即当碰到岔道口时总是先选择其中的一条岔路前进,而不管其他岔路,直到碰到死胡同时才返回岔道口 ...

  7. IIS-反向代理简介

    参考:https://www.williamlong.info/archives/5353.html 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后 ...

  8. jQuery中$.fn

    $.fn是指jquery的命名空间,加上fn上的方法及属性,会对jquery实例每一个有效. 如扩展$.fn.abc(),即$.fn.abc()是对jquery扩展了一个abc方法,那么后面你的每一个 ...

  9. 【C语言】输入5个整数并按输入顺序逆序输出

    #include <stdio.h> int main() { ],i; printf("请输入5个整数:\n"); ;i<;i++) scanf("% ...

  10. nginx介绍以及nginx的反向代理

    什么是nginx? Nginx 是一个高性能的轻量级的HTTP和反向代理服务器,也是一个邮件服务器. 下载地址 本人使用的是Tengine,它是由淘宝网发起的Web服务器项目.它在Nginx的基础上, ...