Title Link: http://www.lightoj.com/volume_showproblem.php?problem=1034
The main topic: There are n lights, there is m relationship, the relationship A, a, a, b means that if the a light switch on the light will also light up, now ask for at least how many switches to open so that all lights are lit.
The topic thought: first by the strong Unicom component contraction point, obtains the DAG diagram, then according to the Dag graph, to find out how many degrees 0 points, namely is asks.
The code is as follows:
#include <bits/stdc++.h>using namespacestd;Const intN =10007; Vector<int>Vec[n], STK;intLow[n], Dfn[n], Belong[n],inch[N];BOOLMk[n];inttot, COU_SCC;voidTarjan (intUintf) {Dfn[u]= low[u]= tot + +; Stk.push_back (U), Mk[u]=true; for(intI=0; I<vec[u].size (); ++i) {intv =Vec[u][i]; if(Dfn[v] = =-1) {Tarjan (V, u); Low[u]=min (Low[u], low[v]); } Else if(Mk[v]) {Low[u]=min (Low[u], dfn[v]); } } if(Low[u] = =Dfn[u]) { ++COU_SCC; while(1) { intv =Stk.back (); Stk.pop_back (); MK[V]=false; BELONG[V]=COU_SCC; if(U = =v) Break; } }}voidSolveintcases) { intN, M; scanf ("%d%d", &n, &m); for(intI=1; i<=n; ++i) vec[i].clear (); intu, v; for(intI=1; i<=m; ++i) {scanf ("%d%d", &u, &v); Vec[u].push_back (v); } memset (Low,-1,sizeof(low)); memset (DFN,-1,sizeof(DFN)); Memset (MK,false,sizeof(MK)); Tot=0, COU_SCC =0; for(intI=1; i<=n; ++i) {if(Dfn[i] = =-1) Tarjan (i,-1); } memset (inch,0,sizeof(inch)); for(intI=1; i<=n; ++i) { for(intj=0; J<vec[i].size (); ++j) {intv =Vec[i][j]; if(Belong[i]! =Belong[v])inch[Belong[v]] + +; } } intAns =0; for(intI=1; i<=cou_scc; ++i) {if(inch[I] = =0) ans++; } printf ("Case %d:%d\n", cases, ans);}intMain () {intT; scanf ("%d", &t); for(intI=1; i<=t; ++i) solve (i); return 0;}
Light OJ 1034-hit The light switches (strong unicom component)