1831: 周末出游

Time Limit: 1 Sec  Memory Limit:
128 MB

Submit: 22  Solved: 8



SubmitStatusWeb
Board

Description

周末天气真好,大家组织一起出去玩(玩你妹,不好好学习),可是,有些人要知道(有好朋友了不起呀)自己最要好的朋友接受邀请了,他才会去,只有邀请到他们最好的朋友才会去(贱人就是矫情)。可是作为负责人的你(冤大头)必须判断是否能够办好这次聚会(气死爸爸拉,哼),邀请到所有的人。

Input

每组第一行输入n,表示有n个人。然后第二行输入n个数a1,a2,a3.....(0<ai<=n)表示第i个人的希望自己的那位好朋友要去。如果ai=i表示这位同学一定会去(注孤)。(0<n<1000)

Output

如果能邀请到所有输出yes,反之no。

Sample Input

41 2 3 3

Sample Output

yes

HINT

刚开始想的太麻烦了,以为还可以邀请一个人,后来才发现折磨简单

#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int>g[10010];
int vis[10010];
int ans;
void dfs(int u)
{
for(int i=0;i<g[u].size();i++)
{
ans++;
dfs(g[u][i]);
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(vis,0,sizeof(vis));
ans=0;
int a;
for(int i=0;i<=n;i++)
g[i].clear();
for(int i=1;i<=n;i++)
{
scanf("%d",&a);
if(a==i)
{
vis[a]=1;
}
else
g[a].push_back(i);
}
for(int i=1;i<=n;i++)
if(vis[i])
{
ans++;
dfs(i);
} if(ans==n)
printf("yes\n");
else
printf("no\n");
}
return 0;
}

zzulioj--1831-- 周末出游(vector建图+dfs)的更多相关文章

  1. CF467D Fedor and Essay 建图DFS

      Codeforces Round #267 (Div. 2) CF#267D D - Fedor and Essay D. Fedor and Essay time limit per test ...

  2. Codeforces Round #580 (Div. 2)-D. Shortest Cycle(思维建图+dfs找最小环)

    You are given nn integer numbers a1,a2,…,ana1,a2,…,an. Consider graph on nn nodes, in which nodes ii ...

  3. Dijkstra算法堆优化(vector建图)

    #include<iostream> #include<algorithm> #include<string.h> #include<stdio.h> ...

  4. PAT-1154(Vertex Coloring )+map使用+vector建图+set的使用

    Vertex Coloring PAT-1154 #include<iostream> #include<cstring> #include<string> #in ...

  5. hdu 2647 (拓扑排序 邻接表建图的模板) Reward

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2647 老板给员工发工资,每个人的基本工资都是888,然后还有奖金,然后员工之间有矛盾,有的员工希望比某员 ...

  6. B - Cow Marathon DFS+vector存图

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

  7. 7月13日考试 题解(DFS序+期望+线段树优化建图)

    T1 sign 题目大意:给出一棵 N 个节点的树,求所有起点为叶节点的有向路径,其 上每一条边权值和的和.N<=10000 水题.考试的时候毒瘤出题人(学长orz)把读入顺序改了一下,于是很多 ...

  8. UVa 3487 & 蜜汁建图

    题意: 有两家公司都想向政府申请某些资源的使用权,并且他们都提供了一些申请列表,列表中含有申请费用和资源种类,同一家公司的申请列表之间不含有重复的资源.政府只可以完整地接受和拒绝谋一份申请列表,问政府 ...

  9. joj 2453 candy 网络流建图的题

    Problem D: Candy As a teacher of a kindergarten, you have many things to do during a day, one of whi ...

随机推荐

  1. 【转载】黑客内核:编写属于你的第一个Linux内核模块

    黑客内核:编写属于你的第一个Linux内核模块

  2. poj3249 Test for job 【图的DAG dp】

    #include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> ...

  3. sqlplus登录提示:ORA-12162:TNS:net service name is incorrectly specified错误

    [root@localhost ~]# su - oracle [oracle@localhost ~]$ sqlplus '/as sysdba' SQL*Plus: Release 11.2.0. ...

  4. 多项福利回馈会员,且看Hao123怎样玩转“霸权主义”

        谈到"霸权主义",我们非常easy将其与国际政治联系在一起.只是.霸权主义可不全然用来形容政治,在7月14日,Hao123上线了一个会员福利活动,命名为"Hao1 ...

  5. void空类型指针

    ; double db = 120.3; void *p; p = &num; cout << *(int *)p << endl;//转换成int类型的指针,再取值 ...

  6. 洛谷P2118 比例简化(暴力)

    题目描述 在社交媒体上,经常会看到针对某一个观点同意与否的民意调查以及结果.例如,对某一观点表示支持的有1498 人,反对的有 902人,那么赞同与反对的比例可以简单的记为1498:902. 不过,如 ...

  7. 如何解决bib的一些问题

    胡老师留的大作业要求综述,因而有很多文献引用.但是当使用bibtex的方法,特别是中文文献的引用会遇到一些问题. 网上相关的解答有: http://blog.sciencenet.cn/blog-10 ...

  8. hiho 171周 - 水题,并查集

    题目链接 题目描述: 输入4 alice 2 alice@hihocoder.com alice@gmail.com bob 1 bob@qq.com alicebest 2 alice@gmail. ...

  9. SpringBoot学习笔记(11)-----SpringBoot中使用rabbitmq,activemq消息队列和rest服务的调用

    1. activemq 首先引入依赖 pom.xml文件 <dependency> <groupId>org.springframework.boot</groupId& ...

  10. 洛谷P2766 最长不下降子序列问题 网络流_DP

    Code: #include<cstdio> #include<iostream> #include<vector> #include<algorithm&g ...