Browse Source

Adaugare problema tricolor

master
Matei-Alexandru Gardus 6 months ago
parent
commit
44e986ebe9
No known key found for this signature in database
3 changed files with 93 additions and 1 deletions
  1. 2
    1
      infoarena/CMakeLists.txt
  2. 7
    0
      infoarena/tricolor/CMakeLists.txt
  3. 84
    0
      infoarena/tricolor/main.cpp

+ 2
- 1
infoarena/CMakeLists.txt View File

@@ -13,4 +13,5 @@ add_subdirectory("mexitate")
13 13
 add_subdirectory("petrol")
14 14
 add_subdirectory("rmq")
15 15
 add_subdirectory("stramosi")
16
-add_subdirectory("teatru")
16
+add_subdirectory("teatru")
17
+add_subdirectory("tricolor")

+ 7
- 0
infoarena/tricolor/CMakeLists.txt View File

@@ -0,0 +1,7 @@
1
+CMAKE_MINIMUM_REQUIRED(VERSION 3.2.0)
2
+
3
+get_filename_component(ProjectId ${CMAKE_CURRENT_SOURCE_DIR} NAME)
4
+string(REPLACE " " "_" ProjectId ${ProjectId})
5
+project(${ProjectId})
6
+
7
+add_executable(${PROJECT_NAME} main.cpp)

+ 84
- 0
infoarena/tricolor/main.cpp View File

@@ -0,0 +1,84 @@
1
+#include <fstream>
2
+#include <vector>
3
+#include <algorithm>
4
+#define NMAX 5001
5
+#define INF 1000000000
6
+
7
+using namespace std;
8
+
9
+ifstream fin("tricolor.in");
10
+ofstream fout("tricolor.out");
11
+
12
+int T,              // numarul de teste din fisierul de intrare
13
+    N,              // numarul de elemente in v
14
+    sol,            // rezultatul pentru fiecare test
15
+    nr[NMAX],       // numar de noduri descendenti al fiecarui nod in parte
16
+    dp[NMAX][NMAX]; // dinamica pe arbore. In general, dp[i][j] = numarul de noduri albe conectate din arbore cu radacina i si j descendenti
17
+
18
+vector<int> v[NMAX];
19
+
20
+void dfs(int nod, int parinte)
21
+{
22
+    int i,
23
+        j,
24
+        sum = 0,
25
+        partSum = 0,
26
+        k = 0;
27
+    for (auto vecin : v[nod])
28
+    {
29
+        if (vecin == parinte) {
30
+            continue;
31
+        }
32
+	
33
+        dfs(vecin, nod);
34
+        nr[nod] += nr[vecin];
35
+
36
+        for (i = k; i >= 0; i--) {
37
+            for (j = 0; j <= nr[vecin]; j++) {
38
+                dp[nod][i + j] = max(dp[nod][i + j], dp[nod][i] + dp[vecin][j] + i * j);
39
+            }
40
+        }
41
+
42
+	    k += nr[vecin];
43
+        partSum = 0;
44
+        
45
+        for (i = 0; i <= nr[vecin]; i++) {
46
+            partSum = max(partSum, dp[vecin][i] + i);	
47
+        }
48
+
49
+        sum += partSum;	
50
+    }
51
+	
52
+    if (v[nod].size() == 1)
53
+        nr[nod] = 1;
54
+	
55
+    dp[nod][1] = sum;
56
+}
57
+
58
+int main()
59
+{
60
+  fin >> T;	
61
+  for(int x = 1; x <= T; x++) {
62
+    fin >> N;
63
+    for(int i = 1; i < N; i++) {
64
+      int x, y;
65
+      fin >> x >> y;
66
+	
67
+      v[x].push_back(y);
68
+      v[y].push_back(x);
69
+    }
70
+    dfs(1, 0);
71
+    sol = 0;
72
+	
73
+    for(int i = 0; i <= N; i++) {
74
+      sol = max(sol, dp[1][i]);
75
+    }
76
+
77
+    fout << sol << "\n";	
78
+    for(int i = 1; i <= N; i++)
79
+      v[i].clear();
80
+
81
+  }
82
+
83
+  return 0;	
84
+}

Loading…
Cancel
Save