Neither shaken nor stirred(DFS理解+vector存图)
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2013
题目理解:
给定n个点的有向图:
下面n行,第一个数字表示点权,后面一个数字m表示有m条边。
起点是1.
对于每个点,输出2个值表示前驱点权1和该点点权2。
1、就是若有多条路径且全为“同一个值”输出“同一个值”,否则输出unknown;
若有一条路径若前驱节点的权值>0,输出前驱结点的权值,否则输出sober;
2、否则若该点点权>0输出该点点权,否则输出前驱点权值(若有多条路径且全为“同一个值”输出“同一个值”,否则输出unknown;
若有一条路径若前驱节点的权值>0,输出前驱结点的权值,否则输出sober)
AC代码+详解
/* */
# include <iostream>
# include <stdio.h>
# include <string.h>
# include <algorithm>
# include <bitset>
# include <ctime>
# include <cmath>
# include <list>
# include <cctype>
# include <cassert>
# include <iomanip>
# include <deque>
# include <queue>
# include <stack>
# include <set>
# include <map>
# include <vector>
using namespace std; const int inf=0x3f3f3f3f;
const int maxn=2e5+;
int a[maxn];
int zt[maxn];
vector<int>ve[maxn]; void dfs(int x, int y)///x此结点,y前驱结点的权值
{
if( zt[x]==- )///有两个前驱,(既然已经判断出有两个前驱,且此路之前来过,就没必要再往下遍历了)
return ; if( !zt[x] )///还没有访问过
zt[x] = y;///记录其前驱节点的权值
else///已经访问过之后又来访问说明有两个前驱结点了,标记一下
zt[x] = -; if( a[x] )///此结点的权值不为0
y=a[x]; for( auto it:ve[x] )
{
if( zt[it]!=y )///之前的前驱结点的值不为现在前驱结点的值,
///若zt[it]原来为0,说明没有遍历过
///若zt[it]原来为-1,或者是一个正整数
///但之前前驱结点的值与此时前驱节点的值不一样
///那么就再进行一次遍历
///至于具体是什么情况,for有前面的判断决定
{
dfs(it, y);
}
} } int main()
{
memset(zt, , sizeof(zt));
int n;
cin>>n;
for(int i=; i<=n; i++ )
{
cin>>a[i];
int x;
cin>>x;
for(int j=; j<=x; j++ )
{
int y;
cin>>y;
ve[i].push_back(y);
}
}
dfs(, -); for(int i=; i<=n; i++ )
{
if( zt[i]==- )
cout<<"sober "; else if( zt[i]==- )
cout<<"unknown "; else
cout<<zt[i]<<' '; if( a[i]> )
cout<<a[i]<<endl; else
{
if( zt[i]==- )
cout<<"sober"<<endl; else if( zt[i]==- )
cout<<"unknown"<<endl; else
cout<<zt[i]<<endl;
}
}
return ;
}
Neither shaken nor stirred(DFS理解+vector存图)的更多相关文章
- POJ 1655.Balancing Act-树的重心(DFS) 模板(vector存图)
Balancing Act Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17497 Accepted: 7398 De ...
- Codeforce-1106-D. Lunar New Year and a Wander(DFS遍历+vector存图+set)
Lunar New Year is approaching, and Bob decides to take a wander in a nearby park. The park can be re ...
- B - Cow Marathon DFS+vector存图
After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exerc ...
- 【模板】Vector存图 + SPFA
最近愉快地决定要由边集数组转向Vector存图,顺便开始图论集训 老惯例,以题写模板 P1339 [USACO09OCT]热浪Heat Wave 题目描述 The good folks in Texa ...
- POJ 1985.Cow Marathon-树的直径-树的直径模板(BFS、DFS(vector存图)、DFS(前向星存图))
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 7536 Accepted: 3559 Case ...
- How far away(DFS+vector存图)
There are n houses in the village and some bidirectional roads connecting them. Every day peole alwa ...
- 存图方式---邻接表&邻接矩阵&前向星
基于vector存图 struct Edge { int u, v, w; Edge(){} Edge(int u, int v, int w):u(u), v(v), w(w){} }; vecto ...
- Safe Path(bfs+一维数组存图)
题目链接:http://codeforces.com/gym/101755/problem/H 题目分析:先bfs一遍怪兽可以到达的点,再bfs人可以走的地方看可不可以到达终点: 很显然读到 2&l ...
- 图论算法(一)存图与STL第六弹——vector容器
图论算法(一)存图 我发现我的博客阅读量贼低,问小伙伴们,ta们都说这些博客太长了QAQ! 今天来个短亿点的(也短不了多少……) 进入正题,图论究竟是什么? 图论就是给你一张图,让你在这张图上进行各种 ...
随机推荐
- redis订阅与发布系统
一.概述 1.redis通过publish.subscribe等命令实现了订阅与发布模式. 2.这个功能提供两种信息机制,分别是订阅/发布到频道和订阅/发布到模式. 二.频道的订阅与信息发送 1.re ...
- YARN-HA高可用集群搭建
YARN-HA配置 1. YARN-HA工作机制 1.1 官方文档:http://hadoop.apache.org/docs/r2.7.2/hadoop-yarn/hadoop-yarn-site/ ...
- Spring AOP创建Throwdvice实例
1.异常发生的时候,通知某个服务对象做处理 2.实现throwsAdvice接口 接口实现: public interface IHello { public void sayHello(String ...
- nginx配置http静态站点服务器
1. 系统环境Windows 10 2. 设置静态站点目录,注意不要出现中文(这里踩了很多坑,可以查看错误日志error.log, “No mapping for the Unicode char ...
- git add提交时关于 LF will be replaced by CRLF in 问题出现的原因以及解决方式
最近在新创建的github项目中add新框架代码时,出现了LF will be replaced by CRLF in的问题,以下为问题截图 查阅资料才知道,LF和FRLF是两种不同的换行格式,这个警 ...
- ubuntu classicmenu-indicator
sudo add-apt-repository ppa:diesch/testing sudo apt-get update sudo apt-get install classicmenu-in ...
- Spring Boot 笔记 (1) - Maven、基本配置、Profile的使用
一. Spring Boot 简介 开箱即用的一站式 Java EE 解决方案 Spring 技术栈的大整合 核心问题 暂时无法回答 Spring Boot 和 SOA 有什么区别? Spring B ...
- django配置*app*登录案例*orm简用
1.静态文件的配置和使用 1.静态文件的配置和使用 没有css样式: 添加样式 结果: <html lang="en"> <head> <meta c ...
- 根据byte数组,生成文件
/** * 根据byte数组,生成文件 * filePath 文件路径 * fileName 文件名称(需要带后缀,如*.jpg.*.java.*.xml) */ public static void ...
- java容器二:List接口实现类源码分析
一.ArrayList 1.存储结构 动态数组elementData transient Object[] elementData; 除此之外还有一些数据 //默认初始容量 private stati ...