LightOJ 1034

132人阅读 评论(0)

5月份左右开始写LightOj 今次看到就继续下去而已。

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.

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

idea：乍看之下没怎么多想就开始缩点，所点之后一开始想用树dp，后来想想好像有点复杂Q=Q  就拍拍脑瓜说   拓扑用用用~然而写完之后  感觉是不是只要求个入度就可以了啊喂，从这点上看，确实最近写的东西太少了。

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node{
int vi,nex;
};
node edge[100005];
int cnt;

int idx,sop;
int dfn[10005],low[10005];
bool atq[10005];
int sta[10005];
int color[10005];
int colnum;

int inde[10005];
int oude[10005];
void addedge(int ui,int vi){
}
void init(){
sop=cnt=colnum=0;
memset(dfn,-1,sizeof(dfn));
memset(low,-1,sizeof(low));
memset(color,-1,sizeof(color));
memset(inde,0,sizeof(inde));
memset(oude,0,sizeof(oude));
}
int n,m;

void tarjan(int now){
dfn[now]=low[now]=++idx;
sta[++sop]=now;
atq[now]=true;
int nex=edge[i].vi;
if (dfn[nex]==-1){
tarjan(nex);
low[now]=min(low[now],low[nex]);
}
else if (atq[nex])
low[now]=min(low[now],low[nex]);
}
if (dfn[now]==low[now]){
colnum++;
while (1){
int tmp=sta[sop];
color[tmp]=colnum;
atq[tmp]=false;
sop--;
if (tmp==now)
break;
}
}
}

void gao(int cas){
init();
scanf("%d%d",&n,&m);
for (int i=0;i<m;i++){
int ui,vi;
scanf("%d%d",&ui,&vi);
}
for (int i=1;i<=n;i++)
if (dfn[i]==-1)
tarjan(i);

for (int i=1;i<=n;i++)
{
int nex=edge[j].vi;
if (color[i]!=color[nex]){
inde[color[nex]]++;
oude[color[i]]++;
}
}

int ret=0;
for (int i=1;i<=colnum;i++) if (inde[i]==0) ret++;
printf("Case %d: %d\n",cas,ret);
}

int main(){
int cas;
scanf("%d",&cas);
for (int i=1;i<=cas;i++)
gao(i);
}

lightOj 算是挺小众的吧，不过找个不为人知的小oj慢慢刷 大概也是我目前的想法，以上！

—————————————————————————————————————————————————————————————————————— MIA_Rodney

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：1732次
• 积分：122
• 等级：
• 排名：千里之外
• 原创：11篇
• 转载：0篇
• 译文：0篇
• 评论：0条
文章分类
文章存档
评论排行