Pursuit For Artifacts

CodeForces - 652E

Johnny is playing a well-known computer game. The game are in some country, where the player can freely travel, pass quests and gain an experience.

In that country there are n islands and m bridges between them, so you can travel from any island to any other. In the middle of some bridges are lying ancient powerful artifacts. Johnny is not interested in artifacts, but he can get some money by selling some artifact.

At the start Johnny is in the island a and the artifact-dealer is in the island b(possibly they are on the same island). Johnny wants to find some artifact, come to the dealer and sell it. The only difficulty is that bridges are too old and destroying right after passing over them. Johnnie's character can't swim, fly and teleport, so the problem became too difficult.

Note that Johnny can't pass the half of the bridge, collect the artifact and return to the same island.

Determine if Johnny can find some artifact and sell it.

Input

The first line contains two integers n and m (1 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) — the number of islands and bridges in the game.

Each of the next m lines contains the description of the bridge — three integers x**i, y**i, z**i (1 ≤ x**i, y**i ≤ n, x**i ≠ y**i, 0 ≤ z**i ≤ 1), where x**i and y**i are the islands connected by the i-th bridge, z**i equals to one if that bridge contains an artifact and to zero otherwise. There are no more than one bridge between any pair of islands. It is guaranteed that it's possible to travel between any pair of islands.

The last line contains two integers a and b (1 ≤ a, b ≤ n) — the islands where are Johnny and the artifact-dealer respectively.

Output

If Johnny can find some artifact and sell it print the only word "YES" (without quotes). Otherwise print the word "NO" (without quotes).

Examples

Input

6 71 2 02 3 03 1 03 4 14 5 05 6 06 4 01 6

Output

YES

Input

5 41 2 02 3 03 4 02 5 11 4

Output

NO

Input

5 61 2 02 3 03 1 03 4 04 5 15 3 01 2

Output

YES

题意:

给你一个含有n个节点,m个边的无向图。

以及一个起点a,终点b。

问你是否存在一个从a到b的路径,路径中一条边只走一次并且经过了一个边权为1的边。

思路:

Tarjan缩点建树,每一个强连通块中如果有1的边,,那么缩成的点权为1.

然后强连通块的之间的边(即桥)也有边权,

然后跑一遍dfs,只要有一个经过的节点或者边是权为1即为YES。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}} inline void getInt(int *p);
const int maxn = 1000010;
int From[maxn], Laxt[maxn], To[maxn << 2], Next[maxn << 2], cnt;
bool flag[maxn];
int low[maxn], dfn[maxn], times, q[maxn], head, scc_cnt, scc[maxn];
vector<pii>G[maxn];
int dis[maxn], S, T, ans;
int check[maxn];
void add(int u, int v, int z)
{
Next[++cnt] = Laxt[u]; From[cnt] = u;
flag[cnt] = z;
Laxt[u] = cnt; To[cnt] = v;
}
void tarjan(int u, int fa)
{
dfn[u] = low[u] = ++times;
q[++head] = u;
for (int i = Laxt[u]; i; i = Next[i]) {
if (To[i] == fa) { continue; }
if (!dfn[To[i]]) {
tarjan(To[i], u);
low[u] = min(low[u], low[To[i]]);
} else { low[u] = min(low[u], dfn[To[i]]); }
}
if (low[u] == dfn[u]) {
scc_cnt++;
while (true) {
int x = q[head--];
scc[x] = scc_cnt;
if (x == u) { break; }
}
}
}
void init()
{
memset(Laxt, 0, sizeof(Laxt));
cnt = 0;
}
int n;
int m;
bool dfs(int S, int pre, int T, bool now)
{
now |= check[S];
if (S == T) {
return now;
}
bool res = 0;
for (auto y : G[S]) {
if (y.fi != pre) {
res |= dfs(y.fi, S, T, now | y.se);
if (res) {
return res;
}
}
}
return res;
}
int a, b;
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
init();
int N, M, u, v, i, j;
int z;
scanf("%d%d", &N, &M);
for (i = 1; i <= M; i++) {
scanf("%d%d%d", &u, &v, &z);
add(u, v, z); add(v, u, z);
}
tarjan(1, 0);
for (i = 1; i <= N; i++) {
for (j = Laxt[i]; j; j = Next[j]) {
if (scc[i] != scc[To[j]]) {
G[scc[i]].push_back(make_pair(scc[To[j]], flag[j]));
} else {
check[scc[i]] |= flag[j];
}
}
}
int a, b;
scanf("%d %d", &a, &b);
a = scc[a];
b = scc[b];
if (a == b) {
if (check[a]) {
printf("YES\n");
} else {
printf("NO\n");
}
} else {
if (dfs(a, -1, b, 0)) {
printf("YES\n");
} else {
printf("NO\n");
}
}
return 0;
} inline void getInt(int *p)
{
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
} else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}

Pursuit For Artifacts CodeForces - 652E (Tarjan+dfs)的更多相关文章

  1. Pursuit For Artifacts CodeForces - 652E

    https://vjudge.net/problem/CodeForces-652E 边双啊,就是点双那个tarjan里面,如果low[v]==dfn[v](等同于low[v]>dfn[u]), ...

  2. 【NOIP模拟题】Graph(tarjan+dfs)

    似乎我搞得太复杂了? 先tarjan缩点然后dfs就行了QAQ. (我不说我被一个sb错调了半个小时....不要以为缩点后dfs就可以肆无忌惮的不加特判判vis了.. bfs的做法:减反图,然后从大到 ...

  3. Cut 'em all! CodeForces - 982C(贪心dfs)

    K - Cut 'em all! CodeForces - 982C 给一棵树 求最多能切几条边使剩下的子树都有偶数个节点 如果n是奇数 那么奇数=偶数+奇数 不管怎么切 都会有奇数 直接打印-1 贪 ...

  4. E. Reachability from the Capital(tarjan+dfs)

    求联通分量个数,在dfs一次 #include <iostream> #include <algorithm> #include <cstring> #includ ...

  5. Kuro and Walking Route CodeForces - 979C (树上DFS)

    Kuro is living in a country called Uberland, consisting of nn towns, numbered from 11to nn, and n−1n ...

  6. 割点(Tarjan算法)【转载】

    本文转自:www.cnblogs.com/collectionne/p/6847240.html 供大家学习 前言:之前翻译过一篇英文的关于割点的文章(英文原文.翻译),但是自己还有一些不明白的地方, ...

  7. 割点(Tarjan算法)

    本文可转载,转载请注明出处:www.cnblogs.com/collectionne/p/6847240.html .本文未完,如果不在博客园(cnblogs)发现此文章,请访问以上链接查看最新文章. ...

  8. Cleaning Robot (bfs+dfs)

    Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...

  9. 【洛谷2403】[SDOI2010] 所驼门王的宝藏(Tarjan+dfs遍历)

    点此看题面 大致题意: 一个由\(R*C\)间矩形宫室组成的宫殿中的\(N\)间宫室里埋藏着宝藏.由一间宫室到达另一间宫室只能通过传送门,且只有埋有宝藏的宫室才有传送门.传送门分为3种,分别可以到达同 ...

随机推荐

  1. vue路由传参的几种基本方式

    原文地址 this.$router.push跳转 现有如下场景,点击父组件的li元素跳转到子组件中,并携带参数,便于子组件获取数据.父组件中: <li v-for="article i ...

  2. 【计算机视觉】基于样本一致性的背景减除运动目标检测算法(SACON)

    SACON(SAmple CONsensus)算法是基于样本一致性的运动目标检测算法.该算法通过对每个像素进行样本一致性判断来判定像素是否为背景. 算法框架图 由上图可知,该算法主要分为四个主要部分, ...

  3. HDU1814(Peaceful Commission) 【2-SAT DFS暴力求最小字典序的模板】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1814 题意:给出一个数n,代表有n个党派,每个党派要求派出其中一个人去参加会议,且只能派出一人.给出m ...

  4. Django模块

    django.contrib.humanize 一系列Django的模板过滤器,有助于向数据添加“人文关怀”. 把'django.contrib.humanize'添加到INSTALLED_APPS设 ...

  5. Oracle-DQL 5- 分组函数(多行函数)

    分组函数(多行函数):--针对表中的多行数据进行运算,返回一个结果 1.多行函数 --sum() 求和SELECT SUM(sal) FROM emp; --avg() 求平均值SELECT AVG( ...

  6. dev控件学习笔记之----CxGrid

    本人总结的DEV学习:希望对大家有所帮助. 一.是否显示分组工具: 二.表格左边记录信息显示的宽度: 三.设置表格行高: 四.表头文件的水平和垂直设置:多个设置用按住SHIFT后进行多选,然后就可以设 ...

  7. Snoopy.class.php使用手册

    Snoopy - the PHP net client v1.2.4 Snoopy是一个php类,用来模拟浏览器的功能,可以获取网页内容,发送表单.Snoopy的特点:1.抓取网页的内容 fetch2 ...

  8. PHP读取xml并写入数据库示例

    <?php $xml = simplexml_load_file('books.xml'); // print_r($xml); // echo "<br/>"; ...

  9. 【hash表】图书管理

    [哈希和哈希表]图书管理 题目描述 图书管理是一件十分繁杂的工作,在一个图书馆中每天都会有许多新书加入.为了更方便的管理图书(以便于帮助想要借书的客人快速查找他们是否有他们所需要的书),我们需要设计一 ...

  10. Jmeter之断言(响应断言,断言持续时间)

    断言是测试环节中,十分重要的一节. 响应结果是否正确,可以通过断言判断,无需人工确认. 1.为请求添加断言 常使用:响应断言>Bean Shell断言>断言持续时间 2.响应断言 ●常用来 ...