Pursuit For Artifacts CodeForces - 652E (Tarjan+dfs)
Posted qieqiemin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Pursuit For Artifacts CodeForces - 652E (Tarjan+dfs)相关的知识,希望对你有一定的参考价值。
Pursuit For Artifacts
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 xi, yi, *z**i* (1?≤?xi,?yi?≤?n, xi?≠?yi, 0?≤?zi?≤?1), where xi 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), '