UVA 10859 placing lampposts tree DP (water

Source: Internet
Author: User

Topic Link: Click to open the link

Test instructions

Petition P70


Simple questions, each point divided or not put.

Import Java.io.printwriter;import Java.util.arraylist;import Java.util.scanner;public class Main {int min (int a,int b) { return a>b?b:a;} int max (int a,int b) {return a>b?a:b;} static int N = 1005;static int M = 2005; arraylist<integer>[] G = new Arraylist[n];int N, m;boolean[] vis = new boolean[n];int[][] dp = new Int[n][2];int DFS (int u) {Vis[u] = true;int ans = 0;dp[u][0] = 0; dp[u][1] = m;//0 means do not put for (int i = 0; i < g[u].size (); i++) {int v = g[u].get (i); if (Vis[v]) Continue;dfs (v);DP [u][1] + = min (dp[v][0]+1, dp[v][1]);DP [u][0] + = dp[v][1]+1;} return min (dp[u][0], dp[u][1]);} void Init () {n = cin.nextint (); m = Cin.nextint (); for (int i = 0; i < n; i++) {vis[i] = false; G[i].clear ();} for (int i = 1, u, v; I <= m; i++) {u = cin.nextint (); v = cin.nextint (); G[u].add (v); G[v].add (U);}} void work () {for (int i = 0; i < N; i++) g[i] = new ArrayList (); int T; T = Cin.nextint (), while (t-->0) {init (); int ans = 0;for (int i = 0; i < n; i++) if (vis[i] = = false) ans + = DFS (i); out.pri NTF ("%d%d%d ", ans/m, M-ans%m, ans%m); Out.println ();}} Main () {cin = new Scanner (system.in); out = new PrintWriter (System.out);} public static void Main (string[] args) {main E = new main (); E.work (); Out.close ();} Public Scanner cin;public static printwriter out;}

UVA 10859 placing lampposts 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.