[最小点基]LightOJ 1034 - Hit the Light Switches
来源:互联网 发布:超星电子图书数据库 编辑:程序博客网 时间:2024/06/05 13:34
传送门:http://lightoj.com/volume_showproblem.php?problem=1034
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
Input starts with an integer T (≤ 15), denoting the number of test cases.
Each case contains a blank line two integers N (1 ≤ N ≤ 10000) and M (0 ≤ M ≤ 105), 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 different 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).
Output
For each case, print the case number and 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
题意:求一个有向图的最小点基,很裸。
思路:直接求出强连通+缩点,然后找入度为0的点即可。
代码:
#include<iostream>#include<vector>#include<stack>#include<algorithm>#include<cstdio>#include<cstring>using namespace std;/**最小点基**/const int MAXN = 11111;int low[MAXN],dfn[MAXN],belong[MAXN],deg[MAXN],ans,scc,t,n,m,depth;vector<int> g[MAXN],gs[MAXN];stack<int> s;void init(){ for(int i=0;i<MAXN;i++)g[i].clear(),gs[i].clear(); memset(dfn,-1,sizeof(dfn)); memset(belong,-1,sizeof(belong)); memset(deg,0,sizeof(deg)); ans = scc = depth = 0 ; while(!s.empty())s.pop();}void tarjan(int u){ low[u] = dfn[u] = depth++; s.push(u); for(int i=0;i<(int)g[u].size();i++){ int v = g[u][i]; if(dfn[v]==-1){ tarjan(v); low[u] = min(low[u],low[v]); }else if(belong[v]==-1){ low[u] = min(low[u],dfn[v]); } } if(low[u]==dfn[u]){ int v; do{ v = s.top(); belong[v] = scc; s.pop(); }while(v!=u); scc++; }}void DFS(int u){ dfn[u] = 1; for(int i=0;i<(int)g[u].size();i++){ int v = g[u][i]; if(belong[u]!=belong[v])deg[belong[v]]++; if(!dfn[v])DFS(v); }}void solve(){ for(int i=0;i<scc;i++)if(!deg[i])ans++;}int main(){ scanf("%d",&t); for(int cas=1;cas<=t;cas++){ init(); scanf("%d%d",&n,&m); while(m--){ int a,b; scanf("%d%d",&a,&b);a--,b--; g[a].push_back(b); } for(int i=0;i<n;i++)if(dfn[i]==-1)tarjan(i); memset(dfn,0,sizeof(dfn)); for(int i=0;i<n;i++)if(!dfn[i])DFS(i); solve(); printf("Case %d: %d\n",cas,ans); } return 0;}
- [最小点基]LightOJ 1034 - Hit the Light Switches
- LightOJ 1034 - Hit the Light Switches【强连通最小点基】
- LightOJ - 1034 Hit the Light Switches(强连通)
- lightoj1034 Hit the Light Switches
- HIT the Books!
- Hit the Target!
- LightOJ 1348 - Aladdin and the Return Journey 树链剖分 点权
- ACM hit 1010 The Angle
- lightoj 1034【强连通+缩点】
- loj 1034(最小点基)
- Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖
- Light OJ 1406 Assassin`s Creed 状态压缩DP+强连通缩点+最小路径覆盖
- hit the nail on the head 正中要害
- the part that hit the pavement first
- 【LightOJ】Assassin`s Creed (II) (缩点,传递闭包,二分图匹配,最小路径覆盖)
- Light OJ 1348 Aladdin and the Return Journey(树剖-点)
- The OpenGL Light Bible
- The flash light year
- 一个技术工作者的四大核心价值理念
- Informix 各版本bug及补丁下载URL
- JSP自定义标签开发入门
- 我的WEB SERVER 1.0
- MyEclipse的注册码破解
- [最小点基]LightOJ 1034 - Hit the Light Switches
- WEKA使用教程(经典教程转载)
- mysql 连接池
- org tutorial
- enum枚举的试用笔记 (仅供个人参考)
- CDialog构造函数重载
- poj 2063 zoj 2224 完全背包问题
- 窗口关闭过程(转)
- 何为Web Intents及其目前的实现状态