Codeforces 212E IT Restaurants tree dp (water

Source: Internet
Author: User
Tags cos sin

Topic Link: Click to open the link

Test instructions

Given n points of the tree, dyed two colors, different colors can not be adjacent and to be as many nodes to dye. Find the number of possible staining nodes for color A and color B. (Copy from Figo)

And at least one dianran a, at least one Dianran b

Dp[i][j]=1 indicates that I Dianran j a color is feasible =0 expression is not feasible.



Import Java.io.bufferedreader;import Java.io.inputstreamreader;import Java.io.printwriter;import Java.math.biginteger;import Java.text.decimalformat;import Java.util.arraydeque;import Java.util.ArrayList;import Java.util.arrays;import Java.util.collection;import Java.util.collections;import Java.util.Comparator;import Java.util.deque;import Java.util.hashmap;import Java.util.iterator;import Java.util.linkedlist;import Java.util.map;import Java.util.priorityqueue;import Java.util.scanner;import Java.util.stack;import Java.util.stringtokenizer;import Java.util.treemap;import Java.util.treeset;import Java.util.Queue;import Java.io.file;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.FileOutputStream; public class Main {arraylist<integer> L = new ArrayList (), R = new ArrayList (); int[][] dp = new int[n][n];//A subtree representing the I point Can get up to a few blue int[when dyeing j red] Siz = new Int[n], ans = new int[n];int n;void dfs (int u, int fa) {for (int i = 2; I <= N; i++) dp[u][ I] = 0;dp[u][0] = 1;siz[u] = 1;for (int i = head[u]; i!=-1; i = edge[i].nex) {int v = edge[i].to; if (v = = FA) Continue;dfs (V, u); Siz[u] + = s iz[v];for (int j = n-1; J >= 0; j--) if (dp[u][j]>0) dp[u][j+siz[v]] = 1;} int s = n-siz[u];for (int i = n-1; I >= 0; i--) if (dp[u][i]>0) dp[u][i+s] = 1;for (int i = 1; i < n-1; i++) if (dp[u][i ]>0) Ans[i] = 1;}  void work () throws Exception {Init_edge (); n = int (); for (int i = 0; I <= N; i++) ans[i] = 0;for (Int i = 1, u, v; i< N; i++) {u =int (); v = Int (); Add (u,v,1); add (v,u,1);} DFS (1,-1); int len = 0;for (int i = 1; i < n-1; i++) len + ans[i];out.println (len), for (int i = 1; i < n-1; i++) if (ans[ i]>0) {out.println (i+ "" + (N-1-i))}} public static void Main (string[] args) throws Exception {main wo = new main (), in = new BufferedReader (New Inputstreamreade R (system.in)); out = new PrintWriter (system.out);//in = new BufferedReader (New InputStreamReader ( new//file ("Input.txt"))),//out = new PrintWriter (New File ("Output.txt")), Wo.work (); out.Close ();} static int N = 5050;static int M = N * 2;decimalformat df = new DecimalFormat ("0.0000"); static int inf = (int) 1e9;static  Long inf64 = (long) 1e18;static double EPS = 1e-8;static double Pi = math.pi;static int mod = (int) 1e9 + 7;private String Next () throws Exception {while (str = = NULL | |!str.hasmoreelements ()) str = new StringTokenizer (In.readline ()); return str . NextToken ();} private int int () throws Exception {return Integer.parseint (Next ());} Private long Long () throws Exception {return Long.parselong (Next ());} Private double double () throws Exception {return double.parsedouble (Next ());} StringTokenizer str;static Scanner cin = new Scanner (system.in); static BufferedReader in;static printwriter out;class EDG E{int from, to, DIS, NEX; Edge () {} edge (int from, int to, int. dis, int nex) {this.from = from; this.to = to; this.dis = dis; This.nex =nex;}} Edge[] Edge = new edge[m<<1]; Int[] head = new Int[n]; int edgenum; void Init_edge () {for (int i = 0; i < N; i++) head[i] = 1; Edgenum = 0;} void Add (int u, int v, int dis) {Edge[edgenum] = new Edge (u, V, dis, head[u]); Head[u] = edgenum++;}/* */int Upper_bound (int[] A, int l, int r, int val) {//Upper_bound (a+l,a+r,val)-a;int pos = R;r--;while (l <= r) {int mid = (L + R) >> 1;if (A[mid] <= val) {L = Mid + 1;} else {pos = Mid;r = Mid-1;}} return POS;} int Lower_bound (int[] A, int l, int r, int val) {//Upper_bound (a+l,a+r,val)-a;int pos = R;r--;while (l <= r) {int mid = (L + R) >> 1;if (A[mid] < val) {L = mid + 1;} else {pos = Mid;r = Mid-1;}} return POS;} int Pow (int x, int y) {int ans = 1;while (Y > 0) {if ((Y & 1) > 0) ans *= x;y >>= 1;x = x * x;} return ans;} Double Pow (double x, int y) {double ans = 1;while (Y > 0) {if ((Y & 1) > 0) ans *= x;y >>= 1;x = x * x;} return ans;} int pow_mod (int x, int y, int Mod) {int ans = 1;while (Y > 0) {if ((Y & 1) > 0) ans *= x;ans%= mod;y >>= 1 ; x = x * x;x%= mod;} return ans;} Long Pow (long x, long y) {LOng ans = 1;while (Y > 0) {if ((Y & 1) > 0) ans *= x;y >>= 1;x = x * x;} return ans;} Long Pow_mod (long x, long y, long Mod) {Long ans = 1;while (Y > 0) {if ((Y & 1) > 0) ans *= x;ans%= mod;y &GT;&G t;= 1;x = x * x;x%= mod;} return ans;} int gcd (int x, int y) {if (x > y) {int tmp = X;X = Y;y = tmp;} while (x > 0) {y%= x;int tmp = x;x = Y;y = tmp;} return y;} int max (int x, int y) {return x > y x:y;} int min (int x, int y) {return x < y x:y;} Double Max (double x, double y) {return x > y x:y;} Double min (double x, double y) {return x < y x:y;} Long Max (long x, long y) {return x > y x:y;} Long min (long x, long y) {return x < y x:y;} int abs (int x) {return x > 0 ×:-X;} Double abs (double x) {return x > 0 ×:-X;} Long ABS (long x) {return x > 0 ×:-X;} Boolean zero (double x) {return abs (x) < EPS;} Double sin (double x) {return Math.sin (x);} Double cos (double x) {return math.cos (x);} Double tan (double x) {return math.tan (x);} Double sqrt (double x) {return math.sqrt (x);}}


Codeforces 212E IT Restaurants tree dp (water

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.