LightOJ 1034

原创 2015年07月07日 21:46:46

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

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

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


1034 - Hit the Light Switches
PDF (English) Statistics Forum
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

相关文章推荐

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

传送门:http://lightoj.com/volume_showproblem.php?problem=1034   1034 - Hit the Light Switches ...

LightOJ 1034 - Hit the Light Switches【强连通最小点基】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1034题意:有向图,选择尽量少的点,从这些点出发,使得所有点都被到达过。思路:首...

RFC1034_dns

  • 2013年09月18日 12:50
  • 126KB
  • 下载

PAT (Basic Level)1034. 有理数四则运算

https://www.patest.cn/contests/pat-b-practise/1034 (刷完这题,Basic Level就清啦~~为什么最后是1034呢,因为这儿有坑啊!) 题目描...

RFC1034(中文

  • 2013年08月22日 14:34
  • 959KB
  • 下载

dns rfc1034 rfc1035 中文 英文

  • 2013年02月06日 16:12
  • 933KB
  • 下载

sicily1034. Forest

Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description In the field of computer scienc...

clientpack-7.3.7-1034.ubuntu13-64.x86_64.deb

  • 2017年05月31日 00:31
  • 5.18MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LightOJ 1034
举报原因:
原因补充:

(最多只允许输入30个字)