Topic Portal
1 /*2 Test instructions: Given a tree, the node has the right value, and asks the maximum number of points that can be found to satisfy the adjacent points in the tree that are connected and sorted by weight3 The path weights on the tree are smaller than these two points4 dfs/bfs+ thinking: According to the weight value of the size, from small to large connected to the edge, the search for the most connection points is the answer. Since the sort of paths between them ,5 The capital is from the current node u connected past, then is less than the two nodes. DFS needs manual stack, BFS-like topology sequencing idea6 */7 #pragmaComment (linker, "/stack:1024000000,1024000000")8#include <cstdio>9#include <cstring>Ten#include <algorithm> One#include <vector> A using namespacestd; - - Const intMAXN = 5e5 +Ten; the Const intINF =0x3f3f3f3f; - intW[MAXN]; - intCNT[MAXN]; -vector<int>G[MAXN]; + intN; - + voidDFS (intu) { ACnt[u] =1; at for(intI=0; I<g[u].size (); ++i) { - intv =G[u][i]; - if(!Cnt[v]) DFS (v); -Cnt[u] + =Cnt[v]; - } - } in - intMainvoid) {//hdoj 5325 Crazy Bobo to //freopen ("j.in", "R", stdin); + - while(SCANF ("%d", &n) = =1) { the for(intI=1; i<=n; ++i) scanf ("%d", &w[i]); * for(intI=1; i<=n; ++i) g[i].clear (); $ for(intI=1; i<=n-1; ++i) {Panax Notoginseng intU, v; scanf ("%d%d", &u, &v); - if(W[u] <W[v]) G[u].push_back (v); the Elseg[v].push_back (u); + } Amemset (CNT,0,sizeof(CNT)); the for(intI=1; i<=n; ++i) { + if(Cnt[i])Continue; - DFS (i); $ } $ intAns =0; - for(intI=1; i<=n; ++i) ans =Max (ans, cnt[i]); -printf ("%d\n", ans); the } - Wuyi return 0; the}
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <vector>5#include <queue>6 using namespacestd;7 8 Const intMAXN = 5e5 +Ten;9 Const intINF =0x3f3f3f3f;Ten intW[MAXN]; One intCNT[MAXN]; A intDEG[MAXN]; -vector<int>G[MAXN]; - intN; the - intBFS (void) { -queue<int> Q;intRET =0; - for(intI=1; i<=n; ++i) Cnt[i] =1; + for(intI=1; i<=n; ++i) { - if(!Deg[i]) Q.push (i); + } A while(!Q.empty ()) { at intU =Q.front (); Q.pop (); -RET =Max (ret, cnt[u]); - for(intI=0; I<g[u].size (); ++i) { - intv =G[u][i]; -CNT[V] + =Cnt[u]; - if(! (--Deg[v])) Q.push (v); in } - } to returnret; + } - the intMainvoid) { * //freopen ("j.in", "R", stdin); $ Panax Notoginseng while(SCANF ("%d", &n) = =1) { - for(intI=1; i<=n; ++i) scanf ("%d", &w[i]); the for(intI=1; i<=n; ++i) g[i].clear (); +memset (deg,0,sizeof(deg)); A for(intI=1; i<=n-1; ++i) { the intU, v; scanf ("%d%d", &u, &v); + if(W[u] <W[v]) Swap (U, v); -G[u].push_back (v); deg[v]++; $ } $printf ("%d\n", BFS ()); - } - the return 0; -}
BFS Standard procedure
dfs/bfs+ thinking Hdoj 5325 Crazy Bobo