codeforces 652E . Pursuit For Artifacts 强连通分量
题目链接
题目大意: 给一个图, n个点m条边, 某些边上面有权值。
一条边只能走一次, 给两个点s, t。 问你, 从s到t能否经过有权值的边。
首先肯定要缩点, 然后看同一个连通分量里面的边, 是否有有权值的边, 如果有, 那么这个联通块赋值为1。
看两个点s, t。 如果两个点在同一个联通分量里面, 看这个连通分量的值是否为1。
否则的话, 看从s到t路径上的边有没有边的权值为1, 或者是经过的连通分量有没有值为1的。 如果有就yes, 否则no。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 3e5+5;
int head[maxn], num, s[maxn], dfn[maxn], low[maxn];
int vis[maxn], instack[maxn], st[maxn], ok[maxn], top, cnt, deep;
pair <int, pll> ed[300005];
struct val
{
int u, ok;
};
struct node {
int to, nextt, w;
}e[maxn*2];
void add(int u, int v) {
e[num].to = v, e[num].nextt = head[u], head[u] = num++;
}
void add(int u, int v, int w) {
e[num].to = v, e[num].nextt = head[u], e[num].w = w, head[u] = num++;
}
void tarjan(int u, int fa) {
st[++top] = u;
instack[u] = 1;
low[u] = dfn[u] = ++deep;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(v == fa)
continue;
if(!dfn[v]) {
tarjan(v, u);
low[u] = min(low[u], low[v]);
} else if(instack[v]) {
low[u] = min(low[u], dfn[v]);
}
}
if(dfn[u] == low[u]) {
++cnt;
int v;
do {
v = st[top--];
instack[v] = 0;
s[v] = cnt;
} while(u != v);
}
}
int bfs(int u, int v) {
queue <val> q;
q.push(val{u, ok[u]});
vis[u] = 1;
while(!q.empty()) {
val tmp = q.front(); q.pop();
if(tmp.u == v&&tmp.ok)
return 1;
for(int i = head[tmp.u]; ~i; i = e[i].nextt) {
int ve = e[i].to;
if(vis[ve])
continue;
vis[ve] = 1;
val temp = tmp;
if(ok[ve] || e[i].w) {
temp.ok = 1;
}
temp.u = ve;
q.push(temp);
}
}
return 0;
}
int main()
{
int n, m, a, b;
cin>>n>>m;
mem1(head);
for(int i = 0; i < m; i++) {
scanf("%d%d%d", &ed[i].fi, &ed[i].se.fi, &ed[i].se.se);
add(ed[i].fi, ed[i].se.fi);
add(ed[i].se.fi, ed[i].fi);
}
tarjan(1, 0);
mem1(head);
num = 0;
for(int i = 0; i < m; i++) {
int u = ed[i].fi, v = ed[i].se.fi;
if(s[u] == s[v]) {
if(ed[i].se.se) {
ok[s[u]] = 1;
}
continue;
}
add(s[u], s[v], ed[i].se.se);
add(s[v], s[u], ed[i].se.se);
}
cin>>a>>b;
if(s[a] == s[b]) {
if(ok[s[a]]) {
puts("YES");
} else {
puts("NO");
}
} else {
if(bfs(s[a], s[b])) {
puts("YES");
} else {
puts("NO");
}
}
return 0;
}
codeforces 652E . Pursuit For Artifacts 强连通分量的更多相关文章
- codeforces 652E Pursuit For Artifacts 边双连通分量
题意:n个点,m条边的无向图,有的边上有标记,每条边只能走一次 给你一个起点,一个终点,询问是否能找到从起点到终点的路径,这条路径至少包含一条含有标记的边 分析:然后边双缩点 下面介绍一下边双的性质 ...
- Codeforces 950E Data Center Maintenance 强连通分量
题目链接 题意 有\(n\)个信息中心,每个信息中心都有自己的维护时间\((0\leq t\lt h)\),在这个时刻里面的信息不能被获得. 每个用户的数据都有两份备份,放在两个相异的信息中心(维护时 ...
- Codeforces 1137C Museums Tour (强连通分量, DP)
题意和思路看这篇博客就行了:https://www.cnblogs.com/cjyyb/p/10507937.html 有个问题需要注意:对于每个scc,只需要考虑进入这个scc的时间即可,其实和从哪 ...
- Pursuit For Artifacts CodeForces - 652E (Tarjan+dfs)
Pursuit For Artifacts CodeForces - 652E Johnny is playing a well-known computer game. The game are i ...
- Codeforces Round #244 (Div. 2) C. Checkposts (tarjan 强连通分量)
题目:http://codeforces.com/problemset/problem/427/C 题意:给你n座城市,m条有向道路,然后有一个机制,你在某一个城市设置检查点,那么被设置的检查点受保护 ...
- codeforces 711 D.Directed Roads(tarjan 强连通分量 )
题目链接:http://codeforces.com/contest/711/problem/D 题目大意:Udayland有一些小镇,小镇和小镇之间连接着路,在某些区域内,如果从小镇Ai开始,找到一 ...
- Codeforces K. Ice Skating(求强连通分量)
题目描述: Ice Skating time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces 950E Data Center Maintenance ( 思维 && 强连通分量缩点 )
题意 : 给出 n 个点,每个点有一个维护时间 a[i].m 个条件,每个条件有2个点(x,y)且 a[x] != a[y].选择最少的 k (最少一个)个点,使其值加1后,m个条件仍成立. 分析 : ...
- Codeforces B. Mouse Hunt(强连通分解缩点)
题目描述: Mouse Hunt time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
随机推荐
- const与重载
转载自CSDN博客:http://blog.csdn.net/polarbearboy/article/details/6762752 (由于该作者也属于转载,不知原出处) PS:我们很易对下面这 ...
- Servle原理
这篇博客将以Tomcat为例讲一讲Servlet的原理 Servlet容器 Servlet与Servlet容器的关系举个不恰当的例子就像枪和子弹的关系.而Servlet就是子弹,容器就是枪.子弹都有统 ...
- 使用HAProxy、PHP、Redis和MySQL支撑每周10亿请求
在公司的发展中,保证服务器的可扩展性对于扩大企业的市场需要具有重要作用,因此,这对架构师提出了一定的要求.Octivi联合创始人兼软件架构师Antoni Orfin将向你介绍一个非常简单的架构,使用H ...
- PHP编程中10个最常见的错误
PHP是一种非常流行的开源服务器端脚本语言,你在万维网看到的大多数网站都是使用php开发的.本篇经将为大家介绍PHP开发中10个最常见的问题,希望能够对朋友有所帮助. 错误1:foreach循环后留下 ...
- 下拉条的连动-ajax方式
客户端触发: <select id="category1" onchange="changecategory()"> <option v ...
- 分享一个PHP调用RestFul接口的函数
php越来越前端化,大型系统中的php经常是调用后端服务的接口,这里分享一个函数.希望对大家有用. /** * [http 调用接口函数] * @Date 2016-07-11 * @Author G ...
- 以Python列表特性为切入点的求质数列表的方法
一般,构造一个含有2-x之间所有质数的列表,我们采用最简单的遍历判断质数的方法: # 方法一 1 prime = [] def is_prime(n): if n <= 1: return Fa ...
- 用python实现文件读取和内容替换
infile = open("D:/test.txt", "r") #打开文件 outfile = open("D:/pp2.txt", & ...
- 【自学php】第四天 - 使用数组
php支持两种数组,数字索引数组和关联数组.关联数组有点类似Map,可以用字符串或其他数据类型做键对应相应的值保存在数组中. 1.初始化数组 数字索引数组的初始化可以使用如下代码: $products ...
- windows、linux创建子进程
在windows下创建子进程较常用到的API就是CreateProcess,可以通过以下的方式启动一个新进程: STARTUPINFO si = {0}; PROCES ...