祥瑞御免 @ 2019-03-16 20:31:56
这道题与
#include <bits/stdc++.h>
using namespace std;
#define N 2010
int n, cnt, ans;
int head[N], to[N << 1], nxt[N << 1];
int depth[N], fa[N], dfn[N], dis[N];
bool compare(int x, int y) {
return depth[x] > depth[y];
}
inline void add(int x, int y) {
to[++cnt] = y, nxt[cnt] = head[x], head[x] = cnt;
}
void dfs(int now, int f) {
fa[now] = f, depth[now] = depth[f] + 1;
for (int i = head[now]; i; i = nxt[i]) {
if (to[i] == f) continue;
dfs(to[i], now);
}
}
int main() {
// freopen("in.in", "r", stdin);
scanf("%d", &n);
int u, k, x;
for (int i = 1; i <= n; ++i) {
scanf("%d%d", &u, &k);
for (int j = 1; j <= k; ++j) {
scanf("%d", &x);
add(u + 1, x + 1), add(x + 1, u + 1);
}
}
dfs(1, 0);
for (int i = 1; i <= n; ++i) dfn[i] = i;
memset(dis, 0x3f, sizeof dis);
sort(dfn + 1, dfn + n + 1, compare);
for (int i = 1; i <= n; ++i) {
int now = dfn[i], f = fa[now];
dis[now] = min(dis[now], dis[f] + 1);
if (dis[now] > 1) {
dis[f] = 0, dis[fa[f]] = min(dis[fa[f]], 1);
++ans;
}
}
printf("%d\n", ans);
return 0;
}
by Broadway @ 2019-03-16 20:39:05
贪心……感觉显然不太行吧,这是个树形dp裸题啊……
by 祥瑞御免 @ 2019-03-16 20:41:33
@Broadway 那这道题和另两道题有什么区别呢?
by 萌田薰子 @ 2019-03-16 20:52:59
两个相邻的点都占的时候可能比两个分开占的优emm
by Broadway @ 2019-03-16 20:56:19
@祥瑞御免
没仔细看懂你的代码,但是这道题要求是要守卫所有的边,看起来你的代码是要求选点的题目改的,可能会跳过一些边没选
比如这个有可能选了1,4,就会漏掉2->3的
我是萌新,万一说错了请无视我QAQ
by 祥瑞御免 @ 2019-03-16 20:57:04
@一之濑琴美 嗯?什么意思?可以说的更明白一点吗奆佬
by 祥瑞御免 @ 2019-03-16 20:58:24
@Broadway 哦是的是的!谢谢!题目看错了,是占据边不是占据点!太感谢了!
by 萌田薰子 @ 2019-03-16 20:59:40
qaq不止吧。。算了算了我太菜了