从毕业之后 想来已经有很长时间没有写写代码了~ 毕竟又要开始一段新的学习生活 coding作为生活的一部分,如果不这么做的话总觉得似乎少了一点什么。
原来楠妹妹问我:“你毕业之后,还会继续写代码做比赛么?” 我当初丝毫没有怀疑的说了:“应该吧~” 想来重新开始既是因为很有必要,也是为了不忘初心,仅此而已。
5月份左右开始写LightOj 今次看到就继续下去而已。
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 |
#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);
}
—————————————————————————————————————————————————————————————————————— MIA_Rodney