Educational Codeforces Round 10 E - Pursuit For Artifacts (强联通缩点 + 回溯)
题目链接:http://codeforces.com/contest/652/problem/E
给你n个点m个边,x和y双向连接,要是z是1表示这条边上有宝藏,0则没有,最后给你起点和终点,问你要是到从起点到终点要是中间遇到宝藏就输出YES,否则就输出NO。
每条边只能经过一次,而且这个图保证连通的。
我用tarjan强连通缩点,把这个图变成一棵树,要是起点终点在一个连通分量里且分量里的边有宝藏,那么就输出YES。否则,就找起点到终点直接的路有没有宝藏,因为缩点之后是一棵树,所以起点和终点只有一条路。那我从起点dfs一遍,par[i]记录的是i前驱节点。那我之后就从终点反向遍历这条路,要是路上有宝藏,就输出YES。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <set>
using namespace std;
const int MAXN = 3e5 + ;
typedef pair <int , int> P;
struct data {
int next , to , art;
}edge[MAXN * ];
int head[MAXN] , low[MAXN] , dfn[MAXN] , st[MAXN] , block[MAXN] , p[MAXN];
int top , ord , sccnum , end_point , cont;
bool instack[MAXN] , vis[MAXN] , ok , res;
vector <P> G[MAXN]; void init() {
memset(head , - , sizeof(head));
} inline void add(int u , int v , int art) {
edge[cont].next = head[u];
edge[cont].to = v;
edge[cont].art = art;
head[u] = cont++;
} void tarjan(int u , int par) {
low[u] = dfn[u] = ++ord;
st[++top] = u;
instack[u] = true;
for(int i = head[u] ; ~i ; i = edge[i].next) {
int v = edge[i].to;
if(v == par)
continue;
if(!dfn[v]) {
tarjan(v , u);
low[u] = min(low[v] , low[u]);
}
else if(instack[v]) {
low[u] = min(low[u] , dfn[v]);
}
}
if(dfn[u] == low[u]) {
sccnum++;
int v;
do {
v = st[top--];
instack[v] = false;
block[v] = sccnum;
}while(u != v);
}
} void dfs(int u , int par) {
p[u] = par;
for(int i = ; i < G[u].size() ; i++) {
int v = G[u][i].first;
if(par == v)
continue;
if(G[u][i].second)
vis[v] = true;
dfs(v , u);
}
} int main()
{
int n , m , u , v , art , s , e , heheda = ;
scanf("%d %d" , &n , &m);
init();
for(int i = ; i < m ; i++) {
scanf("%d %d %d" , &u , &v , &art);
add(u , v , art);
add(v , u , art);
if(art == )
heheda = ;
}
scanf("%d %d" , &s , &e);
if(heheda) {
printf("NO\n");
return ;
}
tarjan( , -);
end_point = block[e];
for(int u = ; u <= n ; u++) {
for(int i = head[u] ; ~i ; i = edge[i].next) {
int v = edge[i].to;
if(block[u] != block[v]) {
G[block[u]].push_back(P(block[v] , edge[i].art));
}
else {
if(!vis[block[u]])
vis[block[u]] = (bool)(edge[i].art);
}
}
}
if(block[s] == end_point && vis[block[s]]) {
printf("YES\n");
return ;
}
if(vis[block[s]])
res = true;
dfs(block[s] , -);
for(int i = end_point ; ~i ; i = p[i]) {
if(vis[i])
res = true;
}
if(res)
printf("YES\n");
else
printf("NO\n");
}
Educational Codeforces Round 10 E - Pursuit For Artifacts (强联通缩点 + 回溯)的更多相关文章
- Educational Codeforces Round 10
A:Gabriel and Caterpillar 题意:蜗牛爬树问题:值得一提的是在第n天如果恰好在天黑时爬到END,则恰好整除,不用再+1: day = (End - Begin - day0)/ ...
- Educational Codeforces Round 10 A. Gabriel and Caterpillar 模拟
A. Gabriel and Caterpillar 题目连接: http://www.codeforces.com/contest/652/problem/A Description The 9-t ...
- Educational Codeforces Round 10 D. Nested Segments (树状数组)
题目链接:http://codeforces.com/problemset/problem/652/D 给你n个不同的区间,L或者R不会出现相同的数字,问你每一个区间包含多少个区间. 我是先把每个区间 ...
- CF Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组
题目链接:http://codeforces.com/problemset/problem/652/D 大意:给若干个线段,保证线段端点不重合,问每个线段内部包含了多少个线段. 方法是对所有线段的端点 ...
- Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化
D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...
- Educational Codeforces Round 10 C. Foe Pairs 水题
C. Foe Pairs 题目连接: http://www.codeforces.com/contest/652/problem/C Description You are given a permu ...
- Educational Codeforces Round 10 B. z-sort 构造
B. z-sort 题目连接: http://www.codeforces.com/contest/652/problem/B Description A student of z-school fo ...
- Educational Codeforces Round 10 D. Nested Segments 【树状数组区间更新 + 离散化 + stl】
任意门:http://codeforces.com/contest/652/problem/D D. Nested Segments time limit per test 2 seconds mem ...
- Educational Codeforces Round 10 D. Nested Segments
D. Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- 【BZOJ 3907】网格 组合数学
大家说他是卡特兰数,其实也不为过,一开始只是用卡特兰数来推这道题,一直没有怼出来,后来发现其实卡特兰数只不过是一种组合数学,我们可以退一步直接用组合数学来解决,这道题运用组合数的思想主要用到补集与几何 ...
- yum命令Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
yum命令Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY 博客分类: linux 三种解决方案 我采取第三种方案解决的 第一种: linu ...
- QT QLayout 清空
QLayout* pLayout = (QLayout*)ui->frame->layout(); )) { QWidget* pWidget = child->widget(); ...
- 新手如何更换自己喜欢的背景以及此背景的css码
以下内容为转载(对于css码可以自己写当然也可以去网上搜现成的): 更换背景教学:https://jingyan.baidu.com/album/fc07f9897c730412ffe519c0.ht ...
- node搭建文件服务器
python可以在目录下python -m http.server 8080来启动一个静态文件服务器,使用node实现一个 运行node fileServer.js D:\lanFeature 即可将 ...
- phpAdmin 修改密码后拒绝登陆
phpMyadmin没配置正确,打开 phpMyadmin 目录找到config.inc.php文件,查找到$cfg['Servers'][$i]['password']='';这行,在''中输入你正 ...
- Codeforces 542C - 漫长艰辛的解题历程
尽管在下面的一部分解题的方法都有问题... 但是每一次重新提交都是打了一个有意义的补丁的呢 可见在我做题的过程中有多少问题存在... 现在想想,类似于是变量没有开int64,过程函数忘开int64.. ...
- [ CodeVS冲杯之路 ] P2456
不充钱,你怎么AC? 题目:http://codevs.cn/problem/2456/ 用贪心的思想,木材当然要尽量分成多的木板,而大的木材能够分成大木板,但是小的木材不一定能够分成大的木板,所以木 ...
- [bzoj3224]Tyvj 1728 普通平衡树——splay模板
题目 你需要写一种数据结构支援以下操作. 插入元素. 删除元素. 查询元素的排名. 查询第k小的元素. 查询元素前趋. 查询元素后继. 题解 BBST裸题. 代码 #include <cstdi ...
- Type of flip id
http://www.haskell.org/pipermail/beginners/2011-March/006477.html The point is that the type of id h ...