跳过正文
  1. Posts/

codeforces 580 C. Kefa and Park

·1 分钟

http://codeforces.com/contest/580/problem/C

题意:给出一棵树。每个叶子节点上有一个饭店。某些节点上有cat.现在问从根节点出发可以到达多少个饭店,保证在到达饭店的路径中补连续遇到m个以上的cat.

思路:建图,然后dfs..判断为叶子节点(饭店)的方法是某个点的叶子节点数为0.

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2015年12月05日 星期六 10时17分01秒
 4File Name :580C.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
26
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=1E5+7;
34
35int m,n;
36bool vis[N];
37vector<int>edge[N];
38int hascat[N];
39int ans;
40
41void dfs( int cur,int num)
42{
43    vis[cur] = true;
44    if (hascat[cur])
45	num++;
46    else num = 0 ;
47
48 //   cout<<"cur:"<<cur<<" num:"<<num<<endl;
49
50    if (num>m) return;
51
52    int leafnum = 0;
53    for ( int i = 0 ; i <edge[cur].size(); i++ )
54    {
55	int v = edge[cur][i];
56//	cout<<"cur:"<<cur<<" v:"<<v<<endl;
57	if (!vis[v])
58	{
59	    dfs(v,num);
60	    leafnum++;
61	}
62    }
63    if (leafnum==0) ans++;
64
65}
66int main()
67{
68	#ifndef  ONLINE_JUDGE
69	freopen("code/in.txt","r",stdin);
70	#endif
71    ms(vis,false);
72    ms(hascat,0);
73    scanf("%d %d",&n,&m);
74    for ( int i =  1 ; i <= n ; i++) scanf("%d",&hascat[i]);
75    for ( int i = 1  ; i <= n-1 ; i++)
76    {
77	int u,v;
78	scanf("%d %d",&u,&v);
79	edge[u].push_back(v);
80	edge[v].push_back(u);
81    }
82
83    ans = 0 ;
84    dfs(1,0);
85    printf("%d\n",ans);
86
87  #ifndef ONLINE_JUDGE
88  fclose(stdin);
89  #endif
90    return 0;
91}

相关文章

codeforces 115A A. Party

·1 分钟
http://codeforces.com/problemset/problem/115/A 题意:给出n个人之间的上级下级关系。问如何分得最少的组,使得没一组中的人不存在上下级关系。 思路:用树的观点来考虑会很容易。可以看成给了一棵森冷。对于不同的树的相同层的点,不存在上下级关系,可以放在一个group.对于同一棵树,每一层要单独放一个group.所以答案是所有树的深度的最大值。

codeforces edu1 D. Igor In the Museum

·2 分钟
http://codeforces.com/contest/598/problem/D 题意:给第一个地图。 ‘.’是能走的,‘’是不能走的。**每个‘.’和’‘之间有一幅画,**给出k个起点,问对于每组起点,最多能观察到多少副画。

hdu 5305 Friends (dfs)

·1 分钟
dfs 1A 1/************************************************************************* 2 > File Name: code/whust/#9/K.cpp 3 > Author: 111qqz 4 > Email: rkz2013@126.com 5 > Created Time: 2015年08月05日 星期三 15时02分30秒 6 ************************************************************************/ 7 8#include<iostream> 9#include<iomanip> 10#include<cstdio> 11#include<algorithm> 12#include<cmath> 13#include<cstring> 14#include<string> 15#include<map> 16#include<set> 17#include<queue> 18#include<vector> 19#include<stack> 20#define y0 abc111qqz 21#define y1 hust111qqz 22#define yn hez111qqz 23#define j1 cute111qqz 24#define tm crazy111qqz 25#define lr dying111qqz 26using namespace std; 27#define REP(i, n) for (int i=0;i<int(n);++i) 28typedef long long LL; 29typedef unsigned long long ULL; 30const int inf = 0x7fffffff; 31int n ,m,ans; 32int d[50]; 33int on[50],off[50]; 34int u[50],v[50]; 35 36bool ok(int x,int y) 37{ 38 if ( !d[x] && !d[y] && on[x] == off[x] && on[y] == off[y] ) 39 return true; 40 if ( !d[x] && d[y] && on[x] == off[x] ) 41 return true; 42 if ( d[x] && !d[y] && on[y] == off[y] ) 43 return true; 44 if ( d[x] && d[y] ) 45 return true; 46 return false; 47} 48 49void dfs ( int i) 50{ 51 if (i==m) 52 { 53 ans++; 54 return; 55 } 56 int x = u[i]; 57 int y = v[i]; 58 d[x]--; 59 d[y]--; 60 on[x]++; 61 on[y]++; 62 if (ok(x,y)) 63 dfs (i+1); 64 on[x]--;on[y]--; 65 off[y]++;off[x]++; 66 if ( ok( x,y)) 67 dfs (i+1); 68 off[y]--; 69 off[x]--; 70 d[x]++; 71 d[y]++; 72} 73 74int main ( ) 75{ 76 int T; 77 cin>>T; 78 while ( T-- ) 79 { 80 ans = 0; 81 scanf ("%d %d",&n,&m); 82 memset (d,0,sizeof(d)); 83 memset (on,0,sizeof(on)); 84 memset (off,0,sizeof(off)); 85 for ( int i = 0 ; i < m ; i++ ) 86 { 87 scanf ("%d%d",&u[i],&v[i]); 88 d[u[i]]++; 89 d[v[i]]++; 90 } 91 dfs (0); 92 printf ( "%d\n" , ans ); 93 } 94}

uva 12442 . Forwarding Emails

·2 分钟
“… so forward this to ten other people, to prove that you believe the emperor has 题意是说发短信,每个人只会给一个人发,问从哪个人开始发,能传到的人最多