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 项目

    sudo npm install -g @vue/cli-init vue init webpack my-project cd my-project/ npm install npm run dev

  2. 云计算openstack核心组件--glance-镜像服务(6)

    glance做什么 OpenStack 由 Glance 提供 Image 服务 获取镜像位置 https://docs.openstack.org/image-guide/obtain-images ...

  3. Windows环境下安装Hadoop+Hive的使用案例

    Hadoop安装: 首先到官方下载官网的hadoop2.7.7,链接如下 https://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/ 找网盘的 ...

  4. Similar String Groups

    Two strings X and Y are similar if we can swap two letters (in different positions) of X, so that it ...

  5. 第十三章 字符串(一)之 String

    这一节来学习String的特性和方法. 一.String对象的不变性 不变性:String对象是由一个final char[] value 数组实现的,因此String对象是不可变的.任何看起来改变S ...

  6. pycharm 安装好,只要三部! 超级简单教程!

    pycharm的安装,确实比较麻烦,所以特意做了一期简单版本的安装教程,跟着教程走...只要三部! →下载 链接:https://pan.baidu.com/s/1JxZgAhPVKAIoM1_jpD ...

  7. B2B、B2C、C2C、O2O分别是什么意思?

    1.B2B 是指进行电子商务交易的供需双方都是商家(或企业.公司),她(他)们使用了互联网的技术或各种商务网络平台,完成商务交易的过程.电子商务是现代 B2B marketing的一种具体主要的表现形 ...

  8. APOC官网触发器示例执行后Web页面一直转圈

    apoc使用触发器:如apoc官网指导 CREATE (d:Person {name:'Daniel'}) CREATE (l:Person {name:'Mary'}) CREATE (t:Pers ...

  9. .Net面试题三

    1..Net中类和结构的区别? 2.死锁地必要条件?怎么克服? 3.接口是否可以继承接口?抽象类是否可以实现接口?抽象类是否可以继承实体类? 4.构造器COnstructor是否可以被继承?是否可以被 ...

  10. 怎样解决在执行 vue init 时提示 "vue : 无法加载文件" 的问题?

    注意, 以下操作需要 以管理员身份 在 PowerShell 中进行, 不能是 CMD / Git Bash 等. 1. 以 管理员身份 运行 PowerShell 2. 执行 get-Executi ...