-
Notifications
You must be signed in to change notification settings - Fork 12
/
1230-Placing_Lampposts.cpp
117 lines (92 loc) · 2.12 KB
/
1230-Placing_Lampposts.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <bits/stdc++.h>
#define ll long long
#define MAX 100100
using namespace std;
ll visited[1010];
ll dp[1010][2];
ll dpcountr[1010][2];
vector <ll > edge[1010];
ll dfs(ll curr, ll taken, ll prev)
{
visited[curr] = 1;
if (edge[curr].size() == 1 and edge[curr][0] == prev) {
if (taken == 1)
return 1;
else
return 0;
}
if (dp[curr][taken])
return dp[curr][taken];
ll ans = 0;
dpcountr[curr][taken] = 0;
if (taken == 0) {
for (int i = 0; i < edge[curr].size(); i++) {
if (edge[curr][i] != prev) {
ans += dfs(edge[curr][i], 1 , curr);
dpcountr[curr][taken] += dpcountr[edge[curr][i]][1];
}
}
} else {
ans = 1;
for (int i = 0; i < edge[curr].size(); i++) {
if (edge[curr][i] != prev) {
ll a1 = dfs(edge[curr][i], 0 , curr);
ll a2 = dfs(edge[curr][i], 1 , curr);
if (a1 < a2) {
ans += a1;
dpcountr[curr][taken] += dpcountr[edge[curr][i]][0];
} else if (a2 < a1) {
ans += a2;
dpcountr[curr][taken] += 1 + dpcountr[edge[curr][i]][1];
} else {
ans += a2;
dpcountr[curr][taken] += max(dpcountr[edge[curr][i]][0], 1 + dpcountr[edge[curr][i]][1]);
}
}
}
}
return dp[curr][taken] = ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("/home/sameer/Documents/sameer/input.sam", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
ll i, j, k, n, m, t, cont, a, b;
cin >> t;
ll cases = t;
while (t--) {
cin >> n >> m;
memset(visited, 0, sizeof visited);
memset(dp, 0, sizeof dp);
memset(dpcountr, 0, sizeof dpcountr);
for (i = 0; i < 1010; i++)
edge[i].clear();
for (i = 0; i < m; i++) {
cin >> a >> b;
edge[a].push_back(b);
edge[b].push_back(a);
}
ll ans = 0;
ll cnt = 0;
for (i = 0; i < n; i++) {
if (visited[i] == 0) {
ll a1 = dfs(i, 0, -1);
ll a2 = dfs(i, 1, -1);
if (a1 < a2) {
ans += a1;
cnt += dpcountr[i][0];
} else if (a2 < a1) {
ans += a2;
cnt += dpcountr[i][1];
} else {
ans += a1;
cnt += max(dpcountr[i][0], dpcountr[i][1]);
}
}
}
cout << "Case " << cases - t << ": " << ans << " " << cnt << " " << m - cnt << endl;
}
return 0;
}