UVa11770 - Lighting Away(排序+DFS)
来源:互联网 发布:虚拟机ubuntu怎么联网 编辑:程序博客网 时间:2024/06/06 18:20
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 M (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_JUDGE freopen("e:\\uva_in.txt", "r", stdin);#endif int 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)
- UVA11770 - Lighting Away
- UVA 11770 Lighting Away 强连通缩点+拓扑排序
- UVA 11770 Lighting Away
- How far away ?(DFS)
- UVA 11770 — Lighting Away 强连通+拓扑
- Lighting
- UVA - 11838 Come and Go && UVA - 11770 Lighting Away (强连通分量)
- hdu 2586 How far away ? DFS大水题
- LightOJ 1295 Lighting System Design (排序+dp)
- 拓扑排序+DFS
- poj1128 拓扑排序+DFS
- poj1694 排序+DFS
- 拓扑排序-DFS
- DFS,拓扑排序
- 拓扑排序 dfs
- hdoj538Ponds【dfs+拓扑排序】
- 【bzoj3990】[SDOI2015]排序 DFS
- HDU 1159 Common Subsequence(dp最大公共子串)
- oracle使用exp导出dmp文件后表对象缺失问题的原因及解决办法
- NoSQL & MongoDB
- css练习: 利用锚点做选取相册效果
- java语言,通过反射获得类中的字段,及对应值。
- UVa11770 - Lighting Away(排序+DFS)
- VLC命令行参数详解
- hdu 1022 Train Problem I_栈的应用经典例题
- Spring工作原理及其作用 .
- 读取Jpg和mask图片
- 挺好的图像学习博客
- iOS 的单例模式 dispatch_once
- 各种浏览器的userAgent及如何使用JS来检测游览器类型,或android是什么版本号
- PKU 3716 Panda's Birthday Present (概率问题)