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. C语言递归之二叉树的最大深度

    题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例 给定二叉树 [3,9,20,null,null,15 ...

  2. 关于kail的远程连接

    昨天开始学关于网络攻防的一下知识,虚拟机的镜像用的是kail,对自己造成了很多不适应的地方,有点自闭了. 最近会和大家分享一些关于kail的问题或者说网络攻防方面.这次就说一下kail的远程服务. k ...

  3. prometheus 的promsql的经典例子 安装grafana

    0.好的参考文档 https://www.cnblogs.com/longcnblogs/p/9620733.html 1.多维度数据 2.Prometheus的全局监控指标 只需要定义一个全局的指标 ...

  4. python数值列表

    使用range函数生成数值列表 使用range函数打印1~5的数字 for i in range(1,6): print(i) 输出 1 2 3 4 5 利用range函数生成数值列表 >> ...

  5. Windows冷门快捷键

    Win+Shift+>或者+<光标键,可以使一个程序,在双屏显示器上左右切换. alt+space快捷键相当于在窗口的标题栏上面右键单击,弹出菜单,选择M键,就可以使用光标键上下左右移动来 ...

  6. Response.write()弹出窗口的问题!

    今天偶然发现在.NET中使用Javascript语句弹出窗口时发现一个小小的问题! 例子如下: 1: Response.Write ("<script languge=javascri ...

  7. 网站循环加载监控-C#

    背景: 公司有一个报表的网站,服务器或系统不太稳定,导致客户有时候查看报表网址的时候网站打不开或者打开时间过长,影响用户体验 需求: 通过程序循环打开网址了解加载情况,使用谷歌浏览器内核.,程序开发不 ...

  8. easyswoole报错:failed: Error during WebSocket handshake: Unexpected response code: 200

    WebSocket connection to 'ws://www.xxxx.com/xxx/xx' failed: Error during WebSocket handshake: Unexpec ...

  9. oracle建表详细信息

    一张用户表 -- Create table create table OA_DM.DM_GY_USER ( ), username ) not null, loginname ) not null, ...

  10. the specified service is marked as deletion,can not find the file specified

    使用命令注册windows service sc create CCGSQueueService binpath= "D:\DKX4003\services\xxx.xx.xx\xxx.ex ...