[CF652E]Pursuit For Artifacts - cool's Blog

[CF652E]Pursuit For Artifacts

cool posted @ 2016年4月18日 18:35 in 图论 , 763 阅读
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·1050 ≤ 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 xiyizi (1 ≤ xi, yi ≤ nxi ≠ yi0 ≤ 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).

题目大意:给出一个无向图,每条边的边权是0或1,且只能走一次。询问从S到T的所有路径中,是否有一种路径上存在一条边权为1的点。

一开始想的是直接跑最长路,后来发现被轻易鏼掉,数据如下:

1 2 0

2 3 0

3 4 0

4 5 0

5 3 1

S=1 T=2

然后搜题解,是双连通分量缩点,然后就趁机学习了一下这个算法。

一个无向连通图,去掉一点或一边后影响了该图的连通性(本来是连通的,现在不连通了),则该点就是割点或该边就是桥。

把所有桥去掉后的连通块缩成一个点,在把桥连上就成一棵树。具体实现可以用tarjan求dfn和low,然后缩。

因为双连通分量,所以你走过去以后,一定可以走回来,或者说你肯定可以把这个连通分量的边都走一遍。

所以缩点以后只要每个点记录一个点权,然后S到T dfs一下就行了。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 3e5 + 5;
int head[maxn], l=2;
int h[maxn << 1],V[maxn<<1],last[maxn<<1],t[maxn<<1];

void add(int u, int y, int z)
{
	t[l] = u;V[l] = z;h[l] = y;
    last[l] = head[u];
    head[u] = l++;
}

int n, m;
int dfn[maxn], low[maxn], cnt;
int St[maxn], is[maxn], Belong[maxn],N,st;
int A[maxn];
void tarjan(int x,int fa)
{
//	printf("%d",x);
	is[x] = 1;St[++st] = x;
	dfn[x] = low[x] = ++cnt;
	for (int i = head[x];i;i = last[i])
	{
		if ((i^1) == fa) continue;
		int j = h[i];
		if (!dfn[j]) tarjan(j,i),low[x] = min(low[x],low[j]);
		else if (dfn[j]<dfn[x]) low[x] = min(low[x],dfn[j]);
	}
	if (dfn[x] == low[x])
	{
		int v = St[st--];
	//	printf("%d",v);
        is[v] = 0;
        Belong[v] = ++N;
        for (;st && v != x;v = St[st--],is[v]=0,Belong[v] = N);
    }
}
int DFS(int st, int ed, int f, int s) {
    s |= A[st];
    if(st == ed) return s;
    for(int i = head[st];i; i = last[i])
	{
        int v = h[i];
        if(v == f) continue;
        if(DFS(v, ed, st, s | V[i])) return 1;
    }
    return 0;
}

int main()
{
	scanf("%d%d",&n,&m);
	for (int i = 1;i <= m;i++)
	{
		int x,y,z;
		scanf("%d%d%d",&x,&y,&z);
		add(x,y,z);
		add(y,x,z);
	}
	for (int i = 1;i <= n;i++)
		if (!dfn[i]) tarjan(i,-1);
	int etot = l;
	l = 1;memset(head,0,sizeof(head));
    for(int i = 1; i < etot;i++)
	{
        int u = Belong[t[i]],v = Belong[h[i]];
        if(u == v) A[u] |= V[i];
        else add(u, v, V[i]);
    }
    int st,ed;
    scanf("%d%d",&st,&ed);
    if(DFS(Belong[st],Belong[ed],-1,0)) puts("YES");
	else puts("NO");
    return 0;
}
cheap minibus coach 说:
2020年1月21日 15:28

New for 2013 !!  Our orange tans are on holiday, but must return to their five new homes next summer.

 

Go behind the scenes with our amazing animal experiences. Feed the tiger, train the seal ions, tickle a tapir or even become a keeper for the day!! minibus hire sheffield cheap minibus hire thorpe park minibus hire Cheap Minibus Coach Hire minibus hire kent coach hire peterborough minibus hire glasgow minibus hire blackpool minibus hire scarborough minibus hire with driver minibus hire cardiff minibus hire barnsley minibus hire wolverhampton Cheap Minibus Coach Hire


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee