Skip to main content
  1. Posts/

BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 (dfs)

·2 mins
Table of Contents
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐
#

Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 562  Solved: 352 [Submit][Status][Discuss]

Description
#

The cows are having a picnic! Each of Farmer John’s K (1 <= K <= 100) cows is grazing in one of N (1 <= N <= 1,000) pastures, conveniently numbered 1…N. The pastures are connected by M (1 <= M <= 10,000) one-way paths (no path connects a pasture to itself). The cows want to gather in the same pasture for their picnic, but (because of the one-way paths) some cows may only be able to get to some pastures. Help the cows out by figuring out how many pastures are reachable by all cows, and hence are possible picnic locations.

  K(1≤K≤100)只奶牛分散在N(1≤N≤1000)个牧场.现在她们要集中起来进餐.牧场之间有M(1≤M≤10000)条有向路连接,而且不存在起点和终点相同的有向路.她们进餐的地点必须是所有奶牛都可到达的地方.那么,有多少这样的牧场呢?

Input
#

  • Line 1: Three space-separated integers, respectively: K, N, and M * Lines 2..K+1: Line i+1 contains a single integer (1..N) which is the number of the pasture in which cow i is grazing. * Lines K+2..M+K+1: Each line contains two space-separated integers, respectively A and B (both 1..N and A != B), representing a one-way path from pasture A to pasture B.

 第1行输入K,N,M.接下来K行,每行一个整数表示一只奶牛所在的牧场编号.接下来M行,每行两个整数,表示一条有向路的起点和终点

Output
#

  • Line 1: The single integer that is the number of pastures that are reachable by all cows via the one-way paths.

    所有奶牛都可到达的牧场个数

Sample Input
#

2 4 4 2 3 1 2 1 4 2 3 3 4

INPUT DETAILS:

4<–3 ^ ^ | | | | 1–>2

The pastures are laid out as shown above, with cows in pastures 2 and 3.

Sample Output
#

2

牧场3,4是这样的牧场.

思路:爆搜,从每个奶牛开始做dfs,统计每个点到达的次数,到达为k次的就是合法的牧场。 我一开始的思路有点麻烦,也是从每个奶牛做dfs,然后把所有路径上经过的奶牛所在地存到一个数组里,每次访问一个新的点,就把所有奶牛数组里存进这个点的set里。。。但是实际上并不需要知道有哪些奶牛。。所以一个计数数组足矣。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年04月11日 星期一 15时45分34秒
 4File Name :code/bzoj/1648.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=1E3+7;
34int n,m,k;
35
36vector <int> edge[N];
37bool vis[N];
38
39int cow[105];
40int num;
41int cnt[N];
42void dfs(int x)
43{
44    vis[x] = true;
45    cnt[x]++;
46    for ( int i = 0 ; i < (int)edge[x].size() ; i++)
47    {
48	int v = edge[x][i];
49	if (!vis[v])
50	{
51	    dfs(v);
52	}
53    }
54}
55int main()
56{
57	#ifndef  ONLINE_JUDGE
58	freopen("code/in.txt","r",stdin);
59  #endif
60
61	scanf("%d %d %d",&k,&n,&m);
62	for ( int i = 1 ;i  <= k ; i++)
63	{
64	    scanf("%d",&cow[i]);
65	}
66	for ( int i = 1 ; i <= m ; i++)
67	{
68
69	    int u,v;
70	    scanf("%d %d",&u,&v);
71	    edge[u].push_back(v);
72	}
73
74	ms(cnt,0);
75	for (  int i = 1 ; i <= k ; i++)
76	{
77	    ms(vis,false);
78	    dfs(cow[i]);
79	}
80
81	int ans = 0 ;
82	for  ( int i = 1 ; i <= n ; i++) if (cnt[i]==k) ans++;  //不需要知道到底是哪个牛,只需要个数。
83	printf("%d\n",ans);
84  #ifndef ONLINE_JUDGE
85  fclose(stdin);
86  #endif
87    return 0;
88}

Related

hdu 5416 CRB and Tree ( 2015 多校 #10 )

·2 mins
http://acm.hdu.edu.cn/showproblem.php?pid=5416 # 题意:给出一棵树(n<=1E5),定义二元函数函数f(u,v) (u可以等于v)表示节点u到节点v经过的路径的权值的异或和。给出q组查询(q<=10),每组一个s,问有多少对无序点对(u,v)满足f(u,v)=s. 思路:类似codeforces #340 div 2 E XOR and Favorite Number 先dfs,处理出从根节点都任意节点的异或前缀和。然后对于每个询问o(n)扫一遍,统计sum[i]^s出现多少次。 总的时间复杂度为O(Tqn);

hdoj 5606 ||bc #68 div 2 B tree

·2 mins
http://acm.hdu.edu.cn/showproblem.php?pid=5606 题意:一棵树,边权为0或者1,问对于每个点,距离它最近的点(包括自身)的个数是多少。输出将所有点的答案异或后的值。 思路:由于包括自身,自己与自己距离为0,那么最近的点一定也距离为0,所以就是找对于每个点与它相连的边权为0 的点的个数**。建图的时候可以不管边权为1的点。。因为这样的点不会对任何点的答案有贡献。**正解貌似是冰茶几。。我就是dfs搞了下。。找到每一个联通快的点数。。然后把某个联通快的所有点的答案都更新成点的个数。。。

codeforces 29 C. Mail Stamps

http://codeforces.com/contest/29/problem/C 题意:给出n个边的关系,保证可以构成一条链。正向或者反向输出这个链。 思路:由于下标很大(1E9),而关系个数只有1E5..需要离散化。。而且离散化的同时不能丢失边的关系。。。实际上。。直接用vector+map就好了。。。 map >e;即可。然后找到一个度为1的点。。做个dfs…