DFS/BFS+思维 HDOJ 5325 Crazy Bobo

题目传送门

 1 /*
 2     题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点
 3         在树上的路径权值都小于这两个点
 4     DFS/BFS+思维:按照权值的大小,从小的到大的连有向边,搜索最多连接点数即是答案。因为排序后,他们之间的路径,
 5         可定都是从当前节点u连过去的,那么都是小于这两个节点的。DFS需手动加栈,BFS类似拓扑排序的思路
 6 */
 7 #pragma comment (linker, "/STACK:1024000000,1024000000")
 8 #include <cstdio>
 9 #include <cstring>
10 #include <algorithm>
11 #include <vector>
12 using namespace std;
13
14 const int MAXN = 5e5 + 10;
15 const int INF = 0x3f3f3f3f;
16 int w[MAXN];
17 int cnt[MAXN];
18 vector<int> G[MAXN];
19 int n;
20
21 void DFS(int u) {
22     cnt[u] = 1;
23     for (int i=0; i<G[u].size (); ++i)  {
24         int v = G[u][i];
25         if (!cnt[v])    DFS (v);
26         cnt[u] += cnt[v];
27     }
28 }
29
30 int main(void)  {       //HDOJ 5325 Crazy Bobo
31     //freopen ("J.in", "r", stdin);
32
33     while (scanf ("%d", &n) == 1)   {
34         for (int i=1; i<=n; ++i)    scanf ("%d", &w[i]);
35         for (int i=1; i<=n; ++i)    G[i].clear ();
36         for (int i=1; i<=n-1; ++i)  {
37             int u, v;   scanf ("%d%d", &u, &v);
38             if (w[u] < w[v])  G[u].push_back (v);
39             else    G[v].push_back (u);
40         }
41         memset (cnt, 0, sizeof (cnt));
42         for (int i=1; i<=n; ++i)    {
43             if (cnt[i]) continue;
44             DFS (i);
45         }
46         int ans = 0;
47         for (int i=1; i<=n; ++i)    ans = max (ans, cnt[i]);
48         printf ("%d\n", ans);
49     }
50
51     return 0;
52 }

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <vector>
 5 #include <queue>
 6 using namespace std;
 7
 8 const int MAXN = 5e5 + 10;
 9 const int INF = 0x3f3f3f3f;
10 int w[MAXN];
11 int cnt[MAXN];
12 int deg[MAXN];
13 vector<int> G[MAXN];
14 int n;
15
16 int BFS(void)   {
17     queue<int> Q;   int ret = 0;
18     for (int i=1; i<=n; ++i)    cnt[i] = 1;
19     for (int i=1; i<=n; ++i)    {
20         if (!deg[i])    Q.push (i);
21     }
22     while (!Q.empty ()) {
23         int u = Q.front (); Q.pop ();
24         ret = max (ret, cnt[u]);
25         for (int i=0; i<G[u].size (); ++i)   {
26             int v = G[u][i];
27             cnt[v] += cnt[u];
28             if (!(--deg[v]))    Q.push (v);
29         }
30     }
31     return ret;
32 }
33
34 int main(void)  {
35     //freopen ("J.in", "r", stdin);
36
37     while (scanf ("%d", &n) == 1)   {
38         for (int i=1; i<=n; ++i)    scanf ("%d", &w[i]);
39         for (int i=1; i<=n; ++i)    G[i].clear ();
40         memset (deg, 0, sizeof (deg));
41         for (int i=1; i<=n-1; ++i)  {
42             int u, v;   scanf ("%d%d", &u, &v);
43             if (w[u] < w[v])    swap (u, v);
44             G[u].push_back (v); deg[v]++;
45         }
46         printf ("%d\n", BFS ());
47     }
48
49     return 0;
50 }

BFS 标程做法

时间: 2024-11-10 13:25:05

DFS/BFS+思维 HDOJ 5325 Crazy Bobo的相关文章

HDOJ 5325 Crazy Bobo 树形DP

按照升序或者降序选择的点集可以满足条件..... 树上的每个节点可以从子节点转移,也可以从父亲节点转移 Crazy Bobo Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 325    Accepted Submission(s): 100 Problem Description Bobo has a tree,whose vert

hdu 5325 Crazy Bobo dfs

// hdu 5325 Crazy Bobo // // 题目大意: // // 给你一棵树,树上每一个节点都有一个权值w,选择尽可能多的节点, // 这些节点相互联通,而且依照权值升序排序之后得到节点编号, // 需相邻节点之间的随意节点都要大于较小的节点. // // 解题思路: // 对于每一对u,v,我们建一条这种边:权值小的像权值大的连一条边. // 这样,问题就转化为求以u最小的权值为根的子树上点集的最大值. // dfs就可以. // // // 感悟: // // 多校的一道题目

HDU 5325 Crazy Bobo(思路+dfs 记忆化)

Crazy Bobo Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 612    Accepted Submission(s): 189 Problem Description Bobo has a tree,whose vertices are conveniently labeled by 1,2,...,n.Each node

hdu 5325 Crazy Bobo 乱搞+搜索

Crazy Bobo Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 218    Accepted Submission(s): 60 Problem Description Bobo has a tree,whose vertices are conveniently labeled by 1,2,...,n.Each node h

HDU 5325 Crazy Bobo

对原来的边(u, v)  方向定为u->v当w[u] > w[v] 最大Set是max{u到达的点集合} Crazy Bobo Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 382    Accepted Submission(s): 116 Problem Description Bobo has a tree,whose ve

HDU 5325 CRAZY BOBO 排序

链接 Crazy Bobo Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 252    Accepted Submission(s): 74 Problem Description Bobo has a tree,whose vertices are conveniently labeled by 1,2,...,n.Each no

HDU 5325 Crazy Bobo(深搜)

题意:给一个n,接着输入n个数,表示n个点的值,接着输入n-1条边形成一个生成树 问最大有多少个点的集合使得该集合内的所有点都满足如下:对于集合内点大小相邻的两个点,该两点之间经过的所有点的大小都小于该两点 eg  7 3 30 350 100 200 300 400 1 2 2 3 3 4 4 5 5 6 6 7 该无向图可表示为1--2--3--4--5--6--7   取点6对于集合(3-7)来说,正好比他大的点为点3,且他们之间的所有点的 大小都小于该两点,满足条件   同理取该集合内的

hdoj 3157 Crazy Circuits 【有下界最小流】

题目:hdoj 3157 Crazy Circuits 题意:现在要制造一个电路板,电路板上有 n 个电子元件,各个元件之间有单向的电流流向,然后有一个 + ,电流进入, -- 电流汇入,然后判断能不能让电路板工作,如果能的话求最小电流. 分析:有上下界网络流,求最小流 首先是判断能不能够让电路板工作,能工作的条件就是流量平衡,判断方法前面题目中讲过. 同样先转换为无源汇网络流问题,添加t→ s边权为无穷.那么最小流不就是在满足所有下界的情况的流么.即上面提到的,求得SS→ TT的最大流之后,其

解题报告 之 HDU5325 Crazy Bobo

解题报告 之 HDU5325 Crazy Bobo Description Bobo has a tree,whose vertices are conveniently labeled by 1,2,...,n.Each node has a weight . All the weights are distrinct. A set with m nodes  is a Bobo Set if: - The subgraph of his tree induced by this set is