[最小点基]LightOJ 1034 - Hit the Light Switches

传送门:http://lightoj.com/volume_showproblem.php?problem=1034

 

1034 - Hit the Light Switches
Time Limit: 2 second(s)Memory Limit: 32 MB

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

 


PROBLEM SETTER: SAMEE ZAHUR
SPECIAL THANKS: JANE ALAM JAN

 

题意:求一个有向图的最小点基,很裸。

思路:直接求出强连通+缩点,然后找入度为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;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值