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. .NET中的简单的并行

    https://www.cnblogs.com/hdwgxz/p/6129419.html https://www.cnblogs.com/hueychan/p/10575907.html

  2. DEVOPS ROADMAP

  3. ForeFront TMG标准版

    ForeFront TMG 标准版安装指南 目前 Forefonrt TMG 的RTM版本已经正式发布,你可以在 ISA中文站上下载120天测试版本: http://www.isacn.org/inf ...

  4. A Mixed Flash Translation Layer Structure for SLC-MLC Combined Flash Memory System

    http://blog.sina.com.cn/s/blog_502c8cc40100pztk.html 摘要 1.In this paper, we propose the SLC-MLC mixe ...

  5. pramfs安装

    1. 拷贝 linux-3.11 pramfs-1.5.0-3.11 pramfs-Tools 到电脑中(我是新建目录linux中) 2.解压 3.cd pramfs-1.5.0-3.11/ 4.将p ...

  6. 添加SSH keys到github帐号

    使用git clone命令从github上同步github上的代码库时,如果使用SSH链接(如我自己的esesgrid项目:git@github.com:hmilycc/esesgrid.git),而 ...

  7. 在使用selenium时出现FileNotFoundError: [WinError 2] 系统找不到指定的文件。

    今天在使用selenium出现这样的错: Traceback (most recent call last): File "E:\python\lib\site-packages\selen ...

  8. 华为设备ACL与NAT技术

    ACL 访问控制列表(Access Control Lists),是应用在路由器(或三层交换机)接口上的指令列表,用来告诉路由器哪些数据可以接收,哪些数据是需要被拒绝的,ACL的定义是基于协议的,它适 ...

  9. vi/vim 编辑、搜索、查找、定位

    介绍vi/vim 相关命令,主要涉及:编辑.搜索.查找.定位. 分为两个章节,即常用命令  及 键盘图 一.vi/vim常用命令 set nu 显示行号 gg 跳转到文件开头 / 向后搜索 ? 向前搜 ...

  10. 适配方案(四)适配的基础知识之单位、分辨率、viewport

    适配的基础知识 一.理解单位 px.pt.pc.sp.em.rem.dpr.dp.dip.ppi.dpi.ldpi.mdpi.hdpi.xhdpi.xxhdpi 如果你是ios开发,你需要了解的单位: ...