【Educational Codeforces Round 10E】【双连通分量缩环 BFS】Pursuit For Artifacts 从ST到ED每条边最多经过一次能否经过任一特殊边

E. Pursuit For Artifacts
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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, zi (1 ≤ xi, yi ≤ n, xi ≠ yi, 0 ≤ zi ≤ 1), where xi andyi are the islands connected by the i-th bridge, zi 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 7
1 2 0
2 3 0
3 1 0
3 4 1
4 5 0
5 6 0
6 4 0
1 6
output
YES
input
5 4
1 2 0
2 3 0
3 4 0
2 5 1
1 4
output
NO
input
5 6
1 2 0
2 3 0
3 1 0
3 4 0
4 5 1
5 3 0
1 2
output
YES
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 3e5+10, M = N*2, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n, m;
int x, y, z;
int first[N], id;
int w[M], tp[M], nxt[M];
int dfn[N], low[N], tim;
int sta[N], top; bool e[N];
int c[N], color;
int v[N];
int ST, ED;
vector< pair<int,int> >a[N];
void ins(int x, int y, int z)
{
	w[++id] = y;
	nxt[id] = first[x];
	tp[id] = z;
	first[x] = id;
}
void tarjan(int x, int lastz)
{
	dfn[x] = low[x] = ++tim;
	sta[++top] = x; e[x] = 1;
	for (int z = first[x]; z; z = nxt[z])
	{
		if ((z^lastz) == 1)continue;
		int y = w[z];
		if (dfn[y] == 0)tarjan(y, z);
		if (e[y])gmin(low[x], low[y]);
	}
	if (dfn[x] == low[x])
	{
		v[++color] = 0;
		a[color].clear();
		while (1)
		{
			int y = sta[top--]; e[y] = 0;
			c[y] = color;
			if (y == x)break;
		}
	}
}
bool bfs()
{
	MS(e, 0);
	queue<int>q;
	q.push(c[ST]); e[c[ST]] = 1;
	while (!q.empty())
	{
		int x = q.front(); q.pop();
		for (int i = a[x].size() - 1; ~i; --i)
		{
			int y = a[x][i].first;
			int tp = a[x][i].second;
			if (e[y])continue;	//一定注意,我们只可以更新未入队的点
			v[y] |= v[x];
			v[y] |= tp;
			q.push(y);
			e[y] = 1;
		}
	}
	return v[c[ED]];
}
bool solve()
{
	for (int x = 1; x <= n; ++x)
	{
		for (int z = first[x]; z; z = nxt[z])
		{
			int y = w[z];
			if (c[x] == c[y])v[c[x]] |= tp[z];
			a[c[x]].push_back(MP(c[y], tp[z]));
		}
	}
	if (c[ST] == c[ED] && v[c[ST]])return 1;
	return bfs();
}
int main()
{
	while (~scanf("%d%d", &n,&m))
	{
		MS(first, 0); id = 1;
		for (int i = 1; i <= m; ++i)
		{
			scanf("%d%d%d", &x, &y, &z);
			ins(x, y, z);
			ins(y, x, z);
		}
		scanf("%d%d", &ST, &ED);
		MS(dfn, 0); tim = color = 0;
		tarjan(1, 0);
		puts(solve() ? "YES" : "NO");
	}
	return 0;
}
/*
【trick&&吐槽】
1,缩环重建图的时候一定要注意映射关系。

【题意】
有n(3e5)个点,m(3e5)条双向边(xi,yi,zi)。
没有重边没有自环(这个条件其实无所谓)
有些边是特殊的边(zi==1)
每条边只能经过一次
问你我们能否有一条路径,使得我们可以从ST出发到达ED

【类型】
双连通分量tarjan缩环
BFS

【分析】
这题思考起来有些不着边际。

关键信息是——一条边只能经过一次。
什么情况下一条边我们会经过多次呢?
经过多次必然形成了环。

于是,介于这个图是无向图,我们可以考虑先双连通缩环。
如果ST和ED在同一个联通块内,且这个联通块内有special edge 显然答案为YES
否则的话,已经缩环之后的图,ST与ED之前肯定只有单一路径可达,每个点只会入栈一次,被更新一次,于是直接做BFS即可。

就AC啦!

【时间复杂度&&优化】
O(n+m)

*/



你可能感兴趣的:(codeforces,搜索-BFS,题库-CF,图论-双联通分量)