题目链接: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存图)的更多相关文章

  1. POJ 1655.Balancing Act-树的重心(DFS) 模板(vector存图)

    Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17497   Accepted: 7398 De ...

  2. 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 ...

  3. B - Cow Marathon DFS+vector存图

    After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exerc ...

  4. 【模板】Vector存图 + SPFA

    最近愉快地决定要由边集数组转向Vector存图,顺便开始图论集训 老惯例,以题写模板 P1339 [USACO09OCT]热浪Heat Wave 题目描述 The good folks in Texa ...

  5. POJ 1985.Cow Marathon-树的直径-树的直径模板(BFS、DFS(vector存图)、DFS(前向星存图))

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 7536   Accepted: 3559 Case ...

  6. How far away(DFS+vector存图)

    There are n houses in the village and some bidirectional roads connecting them. Every day peole alwa ...

  7. 存图方式---邻接表&邻接矩阵&前向星

    基于vector存图 struct Edge { int u, v, w; Edge(){} Edge(int u, int v, int w):u(u), v(v), w(w){} }; vecto ...

  8. Safe Path(bfs+一维数组存图)

    题目链接:http://codeforces.com/gym/101755/problem/H 题目分析:先bfs一遍怪兽可以到达的点,再bfs人可以走的地方看可不可以到达终点: 很显然读到  2&l ...

  9. 图论算法(一)存图与STL第六弹——vector容器

    图论算法(一)存图 我发现我的博客阅读量贼低,问小伙伴们,ta们都说这些博客太长了QAQ! 今天来个短亿点的(也短不了多少……) 进入正题,图论究竟是什么? 图论就是给你一张图,让你在这张图上进行各种 ...

随机推荐

  1. Form' threw an exception of type 'System.InvalidOperationException'

    环境:VS2017 NetCore 2.2 Razor Layui 在处理异步请求是遇到"((Microsoft.AspNetCore.Http.Internal.DefaultHttpRe ...

  2. Spring Cloud Alibaba学习笔记(22) - Nacos配置管理

    目前业界流行的统一配置管理中心组件有Spring Cloud Config.Spring Cloud Alibaba的Nacos及携程开源的Apollo,本文将介绍Nacos作为统一配置管理中心的使用 ...

  3. JAVA线程Disruptor核心链路应用(八)

    import java.util.concurrent.atomic.AtomicInteger; /** * Disruptor中的 Event * @author Alienware * */ p ...

  4. 5_PHP数组_3_数组处理函数及其应用_9_数组集合运算函数

    以下为学习孔祥盛主编的<PHP编程基础与实例教程>(第二版)所做的笔记. 数组集合运算函数 1. array_merge() 函数 程序: <?php $array1 = array ...

  5. wc命令——Linux系统高效数据统计工具

    wc(world count)是一个统计文件字词,字节,行数的Linux命令,它可以帮我们非常方便的统计以上信息. 主要参数 常见参数如下: -c 统计字节数. -l 统计行数. -m 统计字符数.这 ...

  6. 【面试突击】- SpringMVC那些事(一)

    1.什么是Spring MVC ?简单介绍下你对springMVC的理解? Spring MVC是一个基于MVC架构的用来简化web应用程序开发的应用开发框架,它是Spring的一个模块,无需中间整合 ...

  7. 1+X证书学习日志 —— css样式表

    ## 因为初级的内容较多,所以选了一些有用的 需要记忆的内容写下 方便日后回顾 CSS语法   选择符{属性:属性值;} ##             所有的css代码 都要放在css样式表里面    ...

  8. 1+X证书学习日志——DOM节点的获取

    var oBox = document.getElementById('box');//获取ID为box的节点 var aBox = document.getElementsByTagName('di ...

  9. Referer和空Referer

    参考CSDN 原文:https://blog.csdn.net/hxl188/article/details/38964743 Referer和空Referer 最近公司有个接口需要针对几个域名加白名 ...

  10. Typescript项目注意点和基本类型介绍

    从typescript源文件到执行的过程 执行者 步骤 说明 TSC 1. TypeScript Source -> TypeScript AST TSC将ts文件转为TS AST(abstra ...