百练3383:Cell Phone Network

传送门:http://bailian.openjudge.cn/practice/3383/

【题解】

题目就是最小支配集。

学习了最小支配集的解法:

树形dp(有空可以推一推)

贪心:DFS遍历后逆DFS序进行处理,如果当前这个点不在支配集而且没和支配集连边,那么标记它父亲为支配集成员并处理父亲的父亲和自己(标为和支配集连边的)

具体实现用一个数组记录支配集一个数组记录是否是支配集 or 连过边。

# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 5e5 + 10;
const int mod = 1e9+7;

# define RG register
# define ST static
/*
首先选择一点为树根,再按照深度优先遍历得到遍历序列,按照所得序列
的反向序列的顺序进行贪心,对于一个即不属于支配集也不与支配集中的
点相连的点来说,如果他的父节点不属于支配集,将其父节点加入到支配集
*/

int n;
int head[M], nxt[M], to[M], tot=0;
bool s[M], is[M]; // have been covered yet(in set or connected with ... in set),  in set 

inline void add(int u, int v) {
    ++tot; nxt[tot] = head[u];
    head[u] = tot; to[tot] = v;
}
inline void adde(int u, int v) {
    add(u, v), add(v, u);
}

int dfn[M], DFN=0, fa[M];
inline void dfs(int x, int fat) {
    ++DFN; fa[x] = fat;
    dfn[DFN] = x;
    for (int i=head[x]; i; i=nxt[i])
        if(to[i] != fat) dfs(to[i], x);
}

int main() {
    while(cin >> n) {
        memset(head, 0, sizeof head); tot = 0;
        DFN = 0; memset(s, 0, sizeof s); memset(is, 0, sizeof is);
        memset(fa, 0, sizeof fa);
        for (int i=1, u, v; i<n; ++i) {
            scanf("%d%d", &u, &v);
            adde(u, v);
        }
        dfs(1, 1);
        for (int i=n; i>=1; --i) {
            int x = dfn[i];
            if(!s[x]) {
                if(!is[fa[x]]) is[fa[x]] = 1;
                s[x] = 1; s[fa[x]] = 1; s[fa[fa[x]]] = 1;
            }
        }
        int ans = 0;
        for (int i=1; i<=n; ++i) ans += is[i];
        if(n == 1) puts("1");
        else printf("%d\n", ans);
    }
    return 0;
}

时间: 2024-08-08 11:24:23

百练3383:Cell Phone Network的相关文章

[OpenJudge] 百练2754 八皇后

八皇后 Description 会下国际象棋的人都很清楚:皇后可以在横.竖.斜线上不限步数地吃掉其他棋子.如何将8个皇后放在棋盘上(有8 * 8个方格),使它们谁也不能被吃掉!这就是著名的八皇后问题. 对于某个满足要求的8皇后的摆放方法,定义一个皇后串a与之对应,即a=b1b2...b8,其中bi为相应摆法中第i行皇后所处的列数.已经知道8皇后问题一共有92组解(即92个不同的皇后串).给出一个数b,要求输出第b个串.串的比较是这样的:皇后串x置于皇后串y之前,当且仅当将x视为整数时比y小. I

poj 百练 2765 八进制小数(精度问题)

2765:八进制小数 查看 提交 统计 提示 提问 总时间限制:  1000ms  内存限制:  65536kB 描述 八进制小数可以用十进制小数精确的表示.比如,八进制里面的0.75等于十进制里面的0.963125 (7/8 + 5/64).所有小数点后位数为n的八进制小数都可以表示成小数点后位数不多于3n的十进制小数. 你的任务是写一个程序,把(0, 1)中的八进制小数转化成十进制小数. 输入 输入包括若干八进制小数,每个小数占用一行.每个小数的形式是0.d1d2d3 ... dk,这里di

洛谷P2899 [USACO08JAN]手机网络Cell Phone Network

P2899 [USACO08JAN]手机网络Cell Phone Network 题目描述 Farmer John has decided to give each of his cows a cell phone in hopes to encourage their social interaction. This, however, requires him to set up cell phone towers on his N (1 ≤ N ≤ 10,000) pastures (co

[USACO08JAN]手机网络Cell Phone Network

[USACO08JAN]手机网络Cell Phone Network 题目描述 Farmer John has decided to give each of his cows a cell phone in hopes to encourage their social interaction. This, however, requires him to set up cell phone towers on his N (1 ≤ N ≤ 10,000) pastures (convenie

POJ 3659 Cell Phone Network(树的最小支配集)(贪心)

Cell Phone Network Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6781   Accepted: 2429 Description Farmer John has decided to give each of his cows a cell phone in hopes to encourage their social interaction. This, however, requires hi

ACM/ICPC 之 递归(POJ2663-完全覆盖+POJ1057(百练2775)-旧式文件结构图)

POJ2663-完全覆盖 题解见首注释 //简单递推-三个米诺牌(3*2)为一个单位打草稿得出规律 //题意-3*n块方格能被1*2的米诺牌以多少种情况完全覆盖 //Memory 132K Time: 0 Ms #include<iostream> #include<cstring> #include<cstdio> using namespace std; int ans; //开始平铺 int Tiling(int n) { int sum = 0; if (n =

【POJ 3659】Cell Phone Network

Cell Phone Network Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6012   Accepted: 2163 Description Farmer John has decided to give each of his cows a cell phone in hopes to encourage their social interaction. This, however, requires hi

百练8216-分段函数-2016正式A题

百练 / 2016计算机学科夏令营上机考试 已经结束 题目 排名 状态 统计 提问 A:分段函数 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 编写程序,计算下列分段函数y=f(x)的值. y=-x+2.5; 0 <= x < 5 y=2-1.5(x-3)(x-3); 5 <= x < 10 y=x/2-1.5; 10 <= x < 20 输入 一个浮点数N,0 <= N < 20 输出 输出N对应的分段函数值:f

POJ3659 Cell Phone Network【最小支配集】【贪心】

Cell Phone Network Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5735Accepted: 2053 Description Farmer John has decided to give each of his cows a cell phone in hopes to encourage their social interaction. This, however, requires him to