UVA1220
这是一个树上dp问题,求树的最大独立点集。
首先,将所有名字都赋予一个编号,这里使用map。
定义:
d
p
[
i
]
[
0
]
dp[i][0]
dp[i][0]为以第
i
i
i名员工为根且第
i
i
i名员工不参加晚会的最优答案,
d
p
[
i
]
[
1
]
dp[i][1]
dp[i][1]为第
i
i
i名员工参加晚会的答案;
I
s
R
e
p
e
a
t
[
i
]
[
0
]
IsRepeat[i][0]
IsRepeat[i][0]为
d
p
[
i
]
[
0
]
dp[i][0]
dp[i][0]的答案是否重复,
I
s
R
e
p
e
a
t
[
i
]
[
1
]
IsRepeat[i][1]
IsRepeat[i][1]同理。
初始化:
d
p
[
i
]
[
0
]
=
0
,
d
p
[
i
]
[
1
]
=
1
dp[i][0]=0,dp[i][1]=1
dp[i][0]=0,dp[i][1]=1
即表示该根节点没有孩子的情况
I
s
R
e
p
e
a
t
=
f
a
l
s
e
IsRepeat=false
IsRepeat=false
即一开始没有任何方案,所以都不会重复
dp
\textit{\textbf{dp}}
dp的状态转移:
d
p
[
i
]
[
1
]
+
=
d
p
[
S
o
n
]
[
0
]
dp[i][1]+=dp[Son][0]
dp[i][1]+=dp[Son][0]
即i如果参加了晚会,则他的直接下属就不能参加晚会,将他所有的下属不参加晚会的答案累加即可。
d
p
[
i
]
[
0
]
+
=
m
a
x
(
d
p
[
S
o
n
]
[
1
]
,
d
p
[
S
o
n
]
[
0
]
)
dp[i][0]+=max(dp[Son][1],dp[Son][0])
dp[i][0]+=max(dp[Son][1],dp[Son][0])
即i如果不参加晚会,他的直接下属可来可不来,选择最优的方案。
IsRepeat
\textit{\textbf{IsRepeat}}
IsRepeat的状态转移:
I
s
R
e
p
e
a
t
[
i
]
[
1
]
∣
=
I
s
R
e
p
e
a
t
[
S
o
n
]
[
0
]
IsRepeat[i][1]|=IsRepeat[Son][0]
IsRepeat[i][1]∣=IsRepeat[Son][0]
若i选择参加晚会,当且仅当他存在至少一个直接下属的最优方案发生重复,他的最优方案方发生重复。
//当dp[CurSon][0]方案更优时
if (dp[CurSon][0] > dp[CurSon][1]) {
IsRepeat[Root][0] |= IsRepeat[CurSon][0];
dp[Root][0] += dp[CurSon][0];
}
//当dp[CurSon][1]方案更优时
else if (dp[CurSon][0] < dp[CurSon][1]) {
IsRepeat[Root][0] |= IsRepeat[CurSon][1];
dp[Root][0] += dp[CurSon][1];
}
//如果都最优,则发生重复
else {
dp[Root][0] += dp[CurSon][1];
IsRepeat[Root][0] = true;
}
AC代码:
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<map>
using namespace std;
vector<int> Son[201];
int N, dp[201][2];
bool IsRepeat[201][2];
bool IsSon[6001];
map<const string, int> ID;
int SetID(int&Cnt,const string&Name) {
auto it = ID.find(Name);
if (it != ID.end()) {
return it->second;
}
else {
ID[Name] = ++Cnt;
return Cnt;
}
}
void Clear() {
ID.clear();
for (int i = 1; i <= N; ++i) {
Son[i].clear();
}
memset(IsRepeat, false, sizeof(IsRepeat));
}
bool Input() {
cin >> N;
if (!N) {
return false;
}
Clear();
string Name;
int&& Cnt = 0;
cin >> Name;
ID[Name] = ++Cnt;
for (int i = 1; i < N; ++i) {
string Employee, Boss;
cin >> Employee>> Boss;
Son[SetID(Cnt, Boss)].push_back(SetID(Cnt, Employee));
}
return true;
}
void DP(int Root) {
dp[Root][0] = 0;
dp[Root][1] = 1;
for (int i = 0; i < Son[Root].size(); ++i) {
int CurSon = Son[Root][i];
DP(CurSon);
if (dp[CurSon][0] > dp[CurSon][1]) {
IsRepeat[Root][0] |= IsRepeat[CurSon][0];
dp[Root][0] += dp[CurSon][0];
}
else if (dp[CurSon][0] < dp[CurSon][1]) {
IsRepeat[Root][0] |= IsRepeat[CurSon][1];
dp[Root][0] += dp[CurSon][1];
}
else {
dp[Root][0] += dp[CurSon][1];
IsRepeat[Root][0] = true;
}
IsRepeat[Root][1] |= IsRepeat[CurSon][0];
dp[Root][1] += dp[CurSon][0];
}
}
int main() {
while (Input()) {
DP(1);
if (dp[1][0] == dp[1][1]) {
cout << dp[1][0] << " No" << endl;
}
else if (dp[1][0] < dp[1][1]) {
cout << dp[1][1] << ' ' << (IsRepeat[1][1] ? "No" : "Yes") << endl;
}
else {
cout << dp[1][0] << ' ' << (IsRepeat[1][0] ? "No" : "Yes") << endl;
}
}
return 0;
}