题目链接: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. [Leetcode] Roman to integer 罗马数字转成整数

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  2. 12.25模拟赛T1

    可以区间dp,但是复杂度太高. 所以应该是贪心,怎么贪心呢? 这种题目,最好还是手玩找一些规律. 可以发现,由于保证可以m次填完,所以颜色之间没有相互包含关系. 比较像分治的模型. 所以考虑拿到一个区 ...

  3. bzoj2724: [Violet 6]蒲公英 分块 区间众数 论algorithm与vector的正确打开方式

    这个,要处理各个数的话得先离散,我用的桶. 我们先把每个块里的和每个块区间的众数找出来,那么在查询的时候,可能成为[l,r]区间的众数的数只有中间区间的众数和两边的数. 证明:若不是这里的数连区间的众 ...

  4. mysql 存在update不存在insert

    如果在INSERT语句末尾指定了ON DUPLICATE KEY UPDATE,并且插入行后会导致在一个UNIQUE索引或PRIMARY KEY中出现重复值,则在出现重复值的行执行UPDATE:如果不 ...

  5. 原型prototype与原型链__proto__

    在 javascript 中我们会约定俗成,如果一个方法是被 new 出来使用的,那么该方法名首字母通常会大写,例如下面代码块中的 Person. var Person = function(name ...

  6. ListView使用--文章集锦

    详解ListView加载网络图片的优化,让你轻松掌握! ListView具有多种item布局--实现微信对话列 关注公众号,分享干货,讨论技术

  7. mybatis 关系映射

    一:订单商品数据模型 1.数据库执行脚本 创建数据库表代码: 1 CREATE TABLE items ( 2 id INT NOT NULL AUTO_INCREMENT, 3 itemsname ...

  8. C#网络编程基本字段---IPAddress、IPEndPoint

    命名空间: using System.Net; PAddress类提供了对IP地址的转换.处理等功能.其Parse方法可将IP地址字符串转换为IPAddress实例. 如:IPAddress ip = ...

  9. 【BZOJ3700】发展城市 [LCA][RMQ]

    发展城市 Time Limit: 20 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description 众所周知,Hzwer学长是一名高富 ...

  10. bzoj1036: [ZJOI2008]树的统计Count link-cut-tree版

    题目传送门 这 算是link-cut-tree裸题啊 不过以前好像没有写过单点修改.............. #include<cstdio> #include<cstring&g ...