CSU 1580Outing Strong connect + backpack

Source: Internet
Author: User

Topic Link: Click to open the link

Given n individuals, the vehicle's manned capacity m

The A[i] array is given below

If you want to invite me to the car, you must first invite A[i]

Q: How many people can I invite?

It is observed that this is a forward graph, and the resulting graph is similar to the tree, but the tail of the chain is a simple ring, according to I->a[i.

As follows:

5 22 3 4 1 4
We must first invite 1234 at the same time to invite 5.

So create a reverse graph (that is, the opposite side), then strongly connect and indent a bit, so that you get a forest (a graph of multiple trees).

And for a tree, only the root node needs to be invited at the same time (because the root node is a ring, the child nodes are a single point), and the child nodes are able to invite any number of individual invitations.

So actually for a tree we just need to care about the root node of the tree the number of points siz[root] and the root nodes of the tree points Son[root] (not including the root node itself)

DFS handles the siz and son.

and use these two for the backpack.

Must first use the siz backpack, get the base on the son backpack.



#include <iostream> #include <stdio.h> #include <string.h> #include <queue> #include <math.h > #include <set> #include <vector>using namespace std;template <class t>inline BOOL Rd (T &ret) { char c; int sgn;if (C=getchar (), c==eof) return 0;while (c!= '-' && (c< ' 0 ' | | C> ' 9 ')) C=getchar (), sgn= (c== '-'), -1:1;ret= (c== '-')? 0: (c ' 0 '); while (C=getchar (), c>= ' 0 ' &&c<= ' 9 ') ret=ret*10+ (c ' 0 '); Ret*=sgn;return 1;}        Template <class t>inline void pt (T x) {if (x <0) {Putchar ('-');    x =-X;    } if (X>9) pt (X/10); Putchar (x%10+ ' 0 ');}    the const int n = 1005;//n is the maximum number of points const int M = 3000;//m is the largest edge of int n, m;//n M is the number of points and sides of the struct edge{int from, to, NEX;    BOOL sign;//is Bridge}edge[m<<1];int Head[n], edgenum;void Add (int u, int v) {//edge start and end point Edge E={u, V, Head[u], false};    Edge[edgenum] = E; Head[u] = edgenum++;} int dfn[n], low[n], Stack[n], top, time; Low[u] is the dfn[v] value (i.e. V-timestamp) of the point set {U-point and subtree in the root of the U-point (all reverse arcs) that can point to (the nearest ancestor v from the root) int Taj;//Connected Branch designator, starting from 1 int belong[n];//belong[i] denotes the connected branch of the I point belonging to bool instack[n];vector<int> Bcc[n];    Marking starting from 1 void Tarjan (int u, int fa) {Dfn[u] = Low[u] = + + time;    Stack[top + +] = u;    Instack[u] = 1;        for (int i = head[u]; ~i; i = edge[i].nex) {int v = edge[i].to;            if (dfn[v] = =-1) {Tarjan (V, u);            Low[u] = min (Low[u], low[v]);  if (Dfn[u] < Low[v]) {edge[i].sign = 1;//is Cut Bridge}} else if (Instack[v])    Low[u] = min (Low[u], dfn[v]);        } if (low[u] = = Dfn[u]) {int now; Taj + +;        Bcc[taj].clear ();            do{now = stack[--top];            Instack[now] = 0;            Belong [Now] = Taj;        Bcc[taj].push_back (now);    }while (now! = u);    }}void tarjan_init (int all) {memset (DFN,-1, sizeof (DFN));    memset (instack, 0, sizeof (instack));    top = time = Taj = 0; for (int i=1;i<=all;i++) if (dfn[i]==-1) Tarjan (i, I); Notice the start point!!! }vEctor<int>g[n];int Du[n], siz[n], son[n];void Suodian () {memset (Du, 0, sizeof (DU));    for (int i = 1; I <= Taj; i++) G[i].clear ();        for (int i = 0; i < Edgenum; i++) {int u = belong[edge[i].from], v = belong[edge[i].to];    if (u!=v) G[u].push_back (v), du[v]++;    } memset (siz, 0, sizeof siz); for (int i = 1; I <= n; i++) siz[belong[i]]++;//Statistics each point in the new graph actually includes the number of points in the old graph}void init () {memset (head,-1, sizeof (head)); Edgenu m=0;} void Dfs (int u) {son[u] = 0;for (int i = 0; i < g[u].size (); i++) {int v = g[u][i];d fs (v); Son[u] + = Son[v] + siz[v];}} int Dp[n], tmp[n];void bei (int x) {for (int j = 0; j + x <= m; + j) Dp[j] = max (dp[j], dp[j+x]+x);} void work (int x) {DFS (x);//printf ("(%d,%d) \ n", Siz[x], son[x]); memset (TMP,-1, sizeof TMP); for (int j = 0; j + siz[x] <= M J + +) Tmp[j] = max (Tmp[j], dp[j+siz[x]]+siz[x]);//for (int i = 0; I <= m; i++) cout<<tmp[i]<< "";p UTS (""); for ( int i = 1; I <= son[x]; i++) {for (int j = 0; j+1<=m; j + +) if (tmp[j+1]!=-1) tmp[j] = Max (Tmp[j], tmp[j+1]+1);} for (int i = 0; I <= m; i++) dp[i] = max (Dp[i], tmp[i]);} int main () {while (~SCANF ("%d%d", &n, &m)) {init (); for (int i = 1, x; i <= n; i++) {rd (x); Add (x, i);} Tarjan_init (n); Suodian ()//for (int i = 1; I <= Taj; i++) printf ("%d", Siz[i]);p UTS ("--siz"); memset (DP, 0, sizeof DP); f or (int i = 1; I <= Taj; i++) if (du[i] = = 0) work (i); Cout<<dp[0]<<endl;} return 0;} /*4 3 3 4ans:44 3 4 1ans:06 3 4 5 6 3ans:212 32 3 4 5 6 7 3 9 10 11 12 812 112 3 4 5 6 7 3 9 10 11 12 8*/


CSU 1580Outing Strong connect + backpack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.