J

Lighting Away

Input: Standard Input

Output: Standard Output

Ronju is a night-guard at the “Lavish office buildings Ltd.” headquarters. The office has a large grass field in front of the building. So every day, when Ronju comes to duty at the evening, it is his duty to turn on all the lights in the field. However, given the large size of the field and the large number of lights, it is very tiring for him to walk to each and every individual light to turn it on.

So he has devised an ingenious plan – he will swap the switches for light-sensitive triggers. A local electronic store nearby sells these funny trigger switches at a very cheap price. Once installed at a light-post, it will automatically turn that light on whenever it can sense some other light lighting up nearby. So from now on, Ronju can just manually flip a few switches, and the light from those will trigger nearby sensors, which will in turn light up some more lights nearby, and so on, gradually lighting up the whole field.

Now Ronju wonders: how many switches does he have to flip manually for this?

Input

The input starts with an integer T, the number of test cases to follow. Each test case will start with two integers N (1 <= N <= 10000) and (0 <= M <= 100000), where N is the number of lights in the field, and M more lines of input follows in this input case. Each of these extra M lines will have two integers a and b separated by a space, where 1 <= a, b <= N, indicating that if the light a lights up, it will trigger the light b to turn on as well (according to their distance, brightness, sensor sensitivity, orientation and other complicated factors). Finally, every test case in the input will be followed by a blank line.

Output

For each input test case, the output must be a single line of the format “Case k: c” where k is the case number starting with 1, and c is the minimum number of lights that Ronju must turn on manually before all the lights in the whole field gets lit up.

Sample Input                               Output for Sample Input

2

5 4

1 2

1 3

3 4

5 3

4 4

1 2

1 3

4 2

4 3

Case 1: 2

Case 2: 2

将点先按入度由小到大排序,然后按出度从大到大排序,注意图用邻接表表示,刚开始用邻接矩阵表示,提交几次超时

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>using namespace std;const int N = 10010;struct Node
{int id, degin, degout;bool operator < (const Node &other) const{if (degin != other.degin) return degin < other.degin;return degout > other.degout;}
};vector<int> g[N];
//int g[N][N];
int n, m;
Node node[N];
bool vis[N];void input();
int solve();
void dfs(int u);int main()
{
#ifndef ONLINE_JUDGEfreopen("e:\\uva_in.txt", "r", stdin);
#endifint t;scanf("%d", &t);for (int i = 1; i <= t; i++) {input();printf("Case %d: %d\n", i, solve());}return 0;
}void input()
{scanf("%d%d", &n, &m);for (int i = 1; i <= n; i++) g[i].clear();//memset(g, 0x00, sizeof(g));for (int i = 1; i <= n; i++) {node[i].degin = node[i].degout = 0;node[i].id = i;}for (int i = 0; i < m; i++) {int a, b;scanf("%d%d", &a, &b);g[a].push_back(b);;//g[b].push_back(a);//g[a][b] = 1;node[b].degin++;node[a].degout++;node[b].id = b;}
}void dfs(int u)
{vis[u] = true;/*for (int v = 1; v <= n; v++) {if (!vis[v] && g[u][v]) {dfs(v);}}*/for (size_t i = 0; i < g[u].size(); i++) {int v = g[u][i];if (!vis[v]) {dfs(v);}}}int solve()
{int ans = 0;memset(vis, false, sizeof(vis));sort(&node[1], &node[n + 1]);/*for (int i = 1; i <= n; i++) {printf("%d:%d %d\n", node[i].id, node[i].degin, node[i].degout);}*/for (int i = 1; i <= n; i++) {if (!vis[node[i].id]) {ans++;dfs(node[i].id);}}return ans;}

UVa11770 - Lighting Away(排序+DFS)相关推荐

  1. python 拓扑排序 dfs bfs_拓扑排序的DFS和BFS

    博主以前有一个疑问,DFS和BFS各自的适用范围是?我想你今天看了这篇文章之后会有一个判断! BFS 数据结构与算法分析:c语言描述(p217) 已经存在一个Indgree入度数组(indgree[v ...

  2. python 拓扑排序 dfs bfs_bfs与dfs的优缺点?

    刚好今天作业有道题问了相似问题= =,顺便来回答一下 DFS和BFS主要差别在于BFS遍历方式是围绕着根结点一圈一圈(level by level)向外遍历,借助队列的方式实现,而DFS的方式则是从一 ...

  3. [Leetcode][第207题][JAVA][课程表][拓扑排序][DFS]

    [问题描述][中等] [解答思路] 1. 拓扑排序 复杂度分析 HashSet[] 数组 import java.util.HashSet; import java.util.LinkedList; ...

  4. LeetCode 1273. 删除树节点(拓扑排序/DFS)

    文章目录 1. 题目 2. 解题 2.1 取巧解 2.2 拓扑排序 2.3 建图+DFS 1. 题目 给你一棵以节点 0 为根节点的树,定义如下: 节点的总数为 nodes 个: 第 i 个节点的值为 ...

  5. python 拓扑排序 dfs bfs_图遍历算法之DFS/BFS

    在计算机科学, 图遍历(Tree Traversal,也称图搜索)是一系列图搜索的算法, 是单次访问树结构类型数据(tree data structure)中每个节点以便检查或更新的一系列机制.图遍历 ...

  6. 14.2 拓扑排序DFS算法

    文章目录 原理 Python代码 测试数据 原理   三色标记DFS天然适合拓扑排序.因为三色标记DFS是先查看栈顶元素,如果不是黑色,说明子元素(邻居)还没压栈.这个时候再将子元素压入栈,后续出栈的 ...

  7. BZOJ.3990.[SDOI2015]排序(DFS)

    题目链接 操作序列的顺序显然是无关的,所以只需按特定顺序求出一个长度为\(l\)的操作序列,它对答案的贡献为\(l!\). 我们从小到大枚举所有选择.若当前为第\(i\)个,如果有一段长度为\(2^i ...

  8. [并查集][排序][dfs][启发式合并] JZOJ P3635 Peaks

    Description 有一个居住在多山岛屿的登山家,已经攀上了一座山峰,并且要攀爬另外一座更高的山峰. 更精确地说,岛上的每一点都有一个大于零的海拔(海面的海拔为零),并且如果登山家位于海拔Ei的山 ...

  9. [Leedcode][JAVA][第210 题][课程表 II][拓扑排序][BFS][DFS][有向图]

    [问题描述][第210 题][课程表 II][中等] 现在你总共有 n 门课需要选,记为 0 到 n-1.在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用 ...

最新文章

  1. 阿里云面向企业效率的云上产品全解析——云呼叫中心
  2. python webdriver点击指令_测开系列Selenium Webdriver Python(20)--Webdriver运行原理
  3. Fedora 安装后需要做的第一件事
  4. 训练赛第三场A题 zoj 559
  5. VC++可视化编程——创建空白窗口
  6. HDU 4699 对顶栈
  7. 公有云、私有云、混合云
  8. 用c语言求积分程序,菜鸟学C语言(五)之求定积分
  9. 码云上面优秀的java项目_秒建一个后台管理系统?用这5个开源免费的Java项目就够了...
  10. 计算机一级胶卷出现文件异常,解决IOS相机胶卷导入照片后堆在最新照片的问题...
  11. hibernate中的dialect解释
  12. 从ZigBee到Matter,智能家居碎片化时代或将终结
  13. linux监控工具等--zz
  14. js实现表格的增添和删除操作
  15. BetaFlight模块设计之二十:CMS菜单模块分析
  16. 单相逆变电源软件设计
  17. 1.基于S5PV210的图片解码播放器(详解)
  18. ssm+mysql+小程序+ssm智慧社区管理系统 毕业设计源码101635
  19. 会员注册 php,PHP实现会员注册系统
  20. 27 pandas 数据透视

热门文章

  1. 【c_prime_plus】第十七章笔记
  2. 配置nginx反向代理时,要注意的权限设置
  3. ViewState与Session 的重要区别
  4. python软件是免费的吗-谁说程序员不懂浪漫?用Python每天自动给女朋友免费发短信...
  5. python编程小游戏代码-Python小游戏之300行代码实现俄罗斯方块
  6. 如何在python中显示电脑中的图片-python如何在终端里面显示一张图片
  7. python绘制3维图-Python 画出来六维图
  8. python利器怎么用-bluepy 一款python封装的BLE利器简单介绍
  9. python官网下载步骤64位-电脑64位怎么下载python
  10. 用python画皮卡丘画法-实现童年宝可梦,教你用Python画一只属于自己的皮卡丘