Light OJ 1034-hit The light switches (strong unicom component)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.