Pursuit For Artifacts CodeForces - 652E (Tarjan+dfs)

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 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), '\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';
        }
    }
}

转载于:https://www.cnblogs.com/qieqiemin/p/11571324.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值