关闭

LightOJ 1034

132人阅读 评论(0) 收藏 举报
分类:

从毕业之后  想来已经有很长时间没有写写代码了~  毕竟又要开始一段新的学习生活   coding作为生活的一部分,如果不这么做的话总觉得似乎少了一点什么。

原来楠妹妹问我:“你毕业之后,还会继续写代码做比赛么?”   我当初丝毫没有怀疑的说了:“应该吧~”    想来重新开始既是因为很有必要,也是为了不忘初心,仅此而已。

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.

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

题意:给出一个图要求选择最小的点数,使得通过这些点可以走到图内的任意一个点。

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

其他:并没有还有很多需要注意的,就是染色的时候居然妄图用low直接做颜色,WA了若干次。求强连通之后的染色过程需要在出栈时进行XD,这大概就是这题需要注意的了。

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node{
	int vi,nex;
};
node edge[100005];
int head[10005];
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){
	cnt++;edge[cnt].vi=vi;edge[cnt].nex=head[ui];head[ui]=cnt;
}
void init(){
	sop=cnt=colnum=0;
	memset(head,0,sizeof(head));
	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;
	for (int i=head[now];i!=0;i=edge[i].nex){
		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);
		addedge(ui,vi);
	}
	for (int i=1;i<=n;i++)
		if (dfn[i]==-1)
			tarjan(i);

	for (int i=1;i<=n;i++)
		for (int j=head[i];j!=0;j=edge[j].nex)
		{
			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条
    文章分类
    文章存档