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! 今天来个短亿点的(也短不了多少……) 进入正题,图论究竟是什么? 图论就是给你一张图,让你在这张图上进行各种 ...
随机推荐
- Java 日期时间与unix时间戳之间转换
日期时间 <--> 时间戳 java.time 包提供的新的日期和时间API LocalDateTime: 本地日期时间类 ZoneId: 时区类 ZonedDateTime: 带时区 ...
- 试用一款网荐的 iOS 快速布局UI库
NerdyUI github: https://github.com/nerdycat/NerdyUI Cupcake (Swift 版本) github: https://github.com/ ...
- git 命令提交项目到git服务器
1.先下载git,然后安装git https://git-scm.com/downloads 2.在电脑任意盘创建一个目录 3.在创建的目录下点击右键 4.初始化git 使用git init 初始化, ...
- SAP Marketing Cloud功能简述(五) : 销售计划管理
Grace前四篇介绍SAP Marketing Cloud的文章: SAP Marketing Cloud功能简述(一) : Contacts和Profiles SAP Marketing Cloud ...
- git stash 缓存本地修改 简介
当我们在使用git的时候,又是会有这种情况:当新的需求了的时候.我们需要为此需求新建一个分支,再次分支上进行修改,当经过测试,提交代码时,在将其合并到主分支,或生产分支上. 但是有时候也有失误的时候, ...
- Delphi对于文件的读写操作
delphi文件操作 取文件名 ExtractFileName(FileName); 取文件扩展名: ExtractFileExt(filename); 取文件名,不带扩展名: 方法一: Functi ...
- python selenium八大定位方法
一.定位方法 注意:元素属性必须唯一存在 #id定位 find_element_by_id() #name定位 find_element_by_name() #class_name定位 find_el ...
- RabbitMQ topic 交换器
topic交换器:"."将路由键分为几个标识符,"*"匹配一个, "#"可以匹配多个 1:路由键为*或者#的时候 *:只能匹配单个的字符串 ...
- [极客-Linux] 05 系统调用
分配内存的系统调用 1)brk 小内存 2)mmap 大内存 sigaction 注册信号处理函数 进程间通信: 1)消息队列,在内核中,msgget 创建 2)共享内存,shmget 内核源代码 ...
- [ipsec][strongswan] strongswan源码分析-- (三) xfrm与strongswan内核接口分析
目录 strongwan sa分析(三) xfrm与strongswan内核接口分析 1. strongswan的实现 2. 交互机制 4. xfrm的消息通信的实现 strongwan sa分析(三 ...