【Educational Codeforces Round 10E】【双连通分量缩环 BFS】Pursuit For Artifacts 从ST到ED每条边最多经过一次能否经过任一特殊边
2016-03-27 10:20
393 查看
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
output
input
output
input
output
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
, id;
int w[M], tp[M], nxt[M];
int dfn
, low
, tim;
int sta
, top; bool e
;
int c
, color;
int v
;
int ST, ED;
vector< pair<int,int> >a
;
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)
*/
相关文章推荐
- UVA 656 - Optimal Programs(BFS)
- 【Educational Codeforces Round 1A】【水题】Tricky Sum 1~n之和减去2的幂
- 【Educational Codeforces Round 1B】【字符串平移 水题】Queries on a String 字符串平移水题
- 【Educational Codeforces Round 1C】【计算几何-极角排序 atan2 long double】Nearest vectors 平面图上原点引出角度最小的两个
- 【Educational Codeforces Round 1D】【DFS 联通块打标记法】Igor In the Museum 联通块内墙的面数
- 【Educational Codeforces Round 1E】【动态规划-多维DP】Chocolate Bar 矩形巧克力掰开吃的最小成本
- 【HDU5222 2015赛码冠军杯I】【并查集找双连通 + tarjan求强连通】Exploration 双向边只能走一边是否图上存在环
- 【Codeforces Round 299 (Div 2)D】【KMP 本质是最前与最后匹配】Tavas and Malekas 长度为n的匹配串被模板串多位点覆盖的匹配串个数
- 【Codeforces Round 333 (Div 2)A 】【水题 简单进制模拟】Two Bases 不同进制数比大小
- 【Codeforces Round 333 (Div 2)B】【贪心 多指针】Approximating a Constant Range 给定数组 相邻元素波动为1 求差值不超1的最长序连续子序列
- 【Codeforces Round 333 (Div 2)C】【最短路】The Two Routes 完全图两种双向边的最小最大距离
- 【Codeforces Round 333 (Div 2)D】【线段树 or ST-RMQ 初始化78msAC】Lipshitz Sequence 若干区间询问所有子区间的答案和
- 【Codeforces Round 333 (Div 2)E】【期望DP概率做法 树状数组转前缀和】Kleofáš and the n-thlon n场比赛m个人获得总名次的期望
- 【Codeforces Testing Round 12A】【讨论 边界元素映射】Divisibility 区间范围内k倍数的数的个数
- 【Codeforces Testing Round 12B】【贪心】Restaurant 选取数量最多的不覆盖区间数
- 【Codeforces Testing Round 12C】【DP 树状数组优化】Subsequences n个不同数,长度为m的LIS数
- 【Educational Codeforces Round 2B】【map or 二分查找】Queries about less or equal elements b[]中的每个数比a[]中多少数大
- 【Educational Codeforces Round 2C】【贪心】最少修改数下得到字典序尽可能小回文串
- 【Educational Codeforces Round 2D】【计算几何 圆面积交 模板】Area of Two Circles' Intersection
- 【Educational Codeforces Round 2E】【STL-map 启发式合并 or 线段树动态开节点 】Lomsat gelral 一棵树每点一个颜色问每个节点子树的颜色众数之和