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 ...
随机推荐
- 导入已有的vmdk文件,发现网络无法连通
把以前的节点都删除了,重新载入镜像.发现每一个都ping不同,ifconfig发现eth0端口都没有打开.. 解决: 进入: vim /etc/sysconfig/network-scripts/if ...
- Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II
以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...
- ubuntu14.04中mysql的安裝及utf8编码集配置
mysql的安裝使用sudo apt-get install mysql-server即可安裝,我安裝的是5.6版.安装过程中会要求输入root账户的密码,按提示输入即可. Mysql Workben ...
- Oracle中字段的修改操作语法
对字段操作 操作方法 更新字段名 alter table TABLE_NAME rename column column_old to column_new; 添加字段 alter table T ...
- Android 启动APP黑屏解决方案
#Android 启动APP黑屏解决方案# 1.自定义Theme //1.设置背景图Theme <style name="Theme.AppStartLoad" parent ...
- 使用device.js检测设备并实现不同设备展示不同网页
现在很多时候会用@media来控制页面在不同分辨率的设备商展示不同效果,但是有些时候想在直接在PC上展示一个做好的页面,在mobile展示另一个页面.这个时候可以借助device.js来检测设备,然后 ...
- zend guard loader
1 .是zendoptimizer的前身, 在php 5.3 (含)之前使用更新到6 ,5.4 之后不再使用.是代码优化的一种,7中opcache 类似功效. 2 .php版本的变量 phpversi ...
- PHP Version之PHP5.2.x到5.3.x
不向下兼容的变化 1. 在5.3的所有绑定扩展中应用了新的内部参数解析API,当给函数传递不兼容的参数时将返回NULL,但有些例外,比如函数get_class()在出现错误时返回FALSE 2. ...
- 【自学php】第二天 - php快速入门
打算看<php和mysql web开发>来学习php,所以也算是这本书的学习笔记吧,也按照书里的例子来练习,但是也有些取舍.第一章是一个订单表单的例子,php用于处理提交的表单. 1.先创 ...
- XCode: Target Settings和Project Settings的区别
一个XCode project包含了两种设置:Project Settings 和 Target Settings. 它们之间的主要区别在于:Project settings应用于project里面的 ...