题目链接:http://codeforces.com/contest/652/problem/E

给你n个点m个边,x和y双向连接,要是z是1表示这条边上有宝藏,0则没有,最后给你起点和终点,问你要是到从起点到终点要是中间遇到宝藏就输出YES,否则就输出NO。

每条边只能经过一次,而且这个图保证连通的。

我用tarjan强连通缩点,把这个图变成一棵树,要是起点终点在一个连通分量里且分量里的边有宝藏,那么就输出YES。否则,就找起点到终点直接的路有没有宝藏,因为缩点之后是一棵树,所以起点和终点只有一条路。那我从起点dfs一遍,par[i]记录的是i前驱节点。那我之后就从终点反向遍历这条路,要是路上有宝藏,就输出YES。

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <set>
using namespace std;
const int MAXN = 3e5 + ;
typedef pair <int , int> P;
struct data {
int next , to , art;
}edge[MAXN * ];
int head[MAXN] , low[MAXN] , dfn[MAXN] , st[MAXN] , block[MAXN] , p[MAXN];
int top , ord , sccnum , end_point , cont;
bool instack[MAXN] , vis[MAXN] , ok , res;
vector <P> G[MAXN]; void init() {
memset(head , - , sizeof(head));
} inline void add(int u , int v , int art) {
edge[cont].next = head[u];
edge[cont].to = v;
edge[cont].art = art;
head[u] = cont++;
} void tarjan(int u , int par) {
low[u] = dfn[u] = ++ord;
st[++top] = u;
instack[u] = true;
for(int i = head[u] ; ~i ; i = edge[i].next) {
int v = edge[i].to;
if(v == par)
continue;
if(!dfn[v]) {
tarjan(v , u);
low[u] = min(low[v] , low[u]);
}
else if(instack[v]) {
low[u] = min(low[u] , dfn[v]);
}
}
if(dfn[u] == low[u]) {
sccnum++;
int v;
do {
v = st[top--];
instack[v] = false;
block[v] = sccnum;
}while(u != v);
}
} void dfs(int u , int par) {
p[u] = par;
for(int i = ; i < G[u].size() ; i++) {
int v = G[u][i].first;
if(par == v)
continue;
if(G[u][i].second)
vis[v] = true;
dfs(v , u);
}
} int main()
{
int n , m , u , v , art , s , e , heheda = ;
scanf("%d %d" , &n , &m);
init();
for(int i = ; i < m ; i++) {
scanf("%d %d %d" , &u , &v , &art);
add(u , v , art);
add(v , u , art);
if(art == )
heheda = ;
}
scanf("%d %d" , &s , &e);
if(heheda) {
printf("NO\n");
return ;
}
tarjan( , -);
end_point = block[e];
for(int u = ; u <= n ; u++) {
for(int i = head[u] ; ~i ; i = edge[i].next) {
int v = edge[i].to;
if(block[u] != block[v]) {
G[block[u]].push_back(P(block[v] , edge[i].art));
}
else {
if(!vis[block[u]])
vis[block[u]] = (bool)(edge[i].art);
}
}
}
if(block[s] == end_point && vis[block[s]]) {
printf("YES\n");
return ;
}
if(vis[block[s]])
res = true;
dfs(block[s] , -);
for(int i = end_point ; ~i ; i = p[i]) {
if(vis[i])
res = true;
}
if(res)
printf("YES\n");
else
printf("NO\n");
}

Educational Codeforces Round 10 E - Pursuit For Artifacts (强联通缩点 + 回溯)的更多相关文章

  1. Educational Codeforces Round 10

    A:Gabriel and Caterpillar 题意:蜗牛爬树问题:值得一提的是在第n天如果恰好在天黑时爬到END,则恰好整除,不用再+1: day = (End - Begin - day0)/ ...

  2. Educational Codeforces Round 10 A. Gabriel and Caterpillar 模拟

    A. Gabriel and Caterpillar 题目连接: http://www.codeforces.com/contest/652/problem/A Description The 9-t ...

  3. Educational Codeforces Round 10 D. Nested Segments (树状数组)

    题目链接:http://codeforces.com/problemset/problem/652/D 给你n个不同的区间,L或者R不会出现相同的数字,问你每一个区间包含多少个区间. 我是先把每个区间 ...

  4. CF Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组

    题目链接:http://codeforces.com/problemset/problem/652/D 大意:给若干个线段,保证线段端点不重合,问每个线段内部包含了多少个线段. 方法是对所有线段的端点 ...

  5. Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化

    D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...

  6. Educational Codeforces Round 10 C. Foe Pairs 水题

    C. Foe Pairs 题目连接: http://www.codeforces.com/contest/652/problem/C Description You are given a permu ...

  7. Educational Codeforces Round 10 B. z-sort 构造

    B. z-sort 题目连接: http://www.codeforces.com/contest/652/problem/B Description A student of z-school fo ...

  8. Educational Codeforces Round 10 D. Nested Segments 【树状数组区间更新 + 离散化 + stl】

    任意门:http://codeforces.com/contest/652/problem/D D. Nested Segments time limit per test 2 seconds mem ...

  9. Educational Codeforces Round 10 D. Nested Segments

    D. Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. BZOJ4785 [Zjoi2017]树状数组 【二维线段树 + 标记永久化】

    题目链接 BZOJ4785 题解 肝了一个下午QAQ没写过二维线段树还是很难受 首先题目中的树状数组实际维护的是后缀和,这一点凭分析或经验或手模观察可以得出 在\(\mod 2\)意义下,我们实际求出 ...

  2. COGS1752. [BOI2007]摩基亚Mokia CDQ

    CDQ的板子题 #include<cstdio> #include<cstring> #include<iostream> #include<algorith ...

  3. JQuery队列queue与原生模仿其实现

    jQuery中的queue和dequeue是一组很有用的方法,他们对于一系列需要按次序运行的函数特别有用.特别animate动画,ajax,以及timeout等需要一定时间的函数. queue() 方 ...

  4. 常见编程语言对REPL支持情况小结

    最近跟一个朋友聊起编程语言的一些特性,他有个言论让我略有所思:“不能REPL的都是渣”.当然这个观点有点偏激,但我们可以探究一下,我们常用的编程语言里面,哪些支持REPL,哪些不支持,还有REPL的一 ...

  5. vue+koa+mysql简易demo

    功能支持网址收藏编辑 代码: https://github.com/lanleilin/lanOdyssey/tree/master/vueKoa/webCollection1 运行方法: 在serv ...

  6. Spring表达式语言之SpEL

    •Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言. •语法类似于 EL:SpEL 使用 #{…} 作为定界符,所有在大框号中的字符都将被认为是 SpEL ...

  7. Mybatis如何查询部分字段

    解决问题:数据库表里面很多字段不太需要,有时只想取到里面的部分字段的值,如果重新定义 DTO 会比较麻烦. BookMapper.xml 文件中定义如下: ` <!-- Book全部字段 --& ...

  8. 从一段字符串中去除数字的shell方法

  9. 【hdu2222-Keywords Search】AC自动机基础裸题

    http://acm.hust.edu.cn/vjudge/problem/16403 题意:给定n个单词,一个字符串,问字符串中出现了多少个单词.(若单词her,he,字符串aher中出现了两个单词 ...

  10. Python与金融分析基础

    一 .ipython的简单使用 IPython:交互式的Python命令行 丰富的快捷键 TAB键自动完成 ?:内省.命名空间搜索 !:执行系统命令 魔术命令:以%开始的命令 %run:执行文件代码 ...