HDU 3861.The King's Problem strong Unicom component + minimum path coverage

Source: Internet
Author: User

The King ' s problem

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 2947 Accepted Submission (s): 1049


problem DescriptionThe Kingdom of Silence, the king has a new problem. There is N cities in the kingdom and there is M directional roads between the cities. That means that if there are a road from U to V, and you can only go from the city u to City v, and can ' t go from the City v to the city U. In order to rule he kingdom more effectively, the king want to divide he kingdom into several states, and all city mus T belong to exactly one state.What's more , for each pair of the city (U, v), if there is one-to-go from U-V and go from V to U, (U, v) has to be Long to a same state. And the king must insure that in each state we can ether go from u to V or go from V to u between every pair of cities (U, V) without passing any city which belongs to other state.
Now the king asks-your help, he wants to know the least number of states he has to divide the kingdom into.

InputThe first line contains a single integer T, the number of test cases. And then followed T cases.

The first line for each case contains integers n, m (0 < n <= 5000,0 <= m <= 100000), the number of cities And roads in the kingdom. The next m lines each contains the integers u and V (1 <= u, v <= N), indicating that there was a road going from CIT Y u to City v.

OutputThe output should contain T lines. For each test case, you should just output a integer which is the least number of states the king has to divide into.

Sample Input13 21 21 3

Sample Output2

SourceMulti-university Training Contest 3-host by BITtitle Link: http://acm.hdu.edu.cn/showproblem.php?pid=3861Test Instructions: There are n cities, and M-strips have direction paths. Now to build some states, each city belongs to a state, if two cities u,v can reach each other, then u,v belong to the same state. If U,v is in the same state, then you can reach V or V to reach U and not go through the cities of other states. The minimum number of States to be built. idea: Because cities that can reach each other belong to the same state, the Tarjan are reduced. The new diagram created is a dag. In a graph, find the fewest paths so that the paths pass through all the points, and each path passes through a different point. This is a minimal path coverage problem. A minimal path overlay to a acyclic graph (DAG)

Code:

#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<map>#include<queue>#include<stack>#include<vector>#include<Set>using namespacestd;#definePI ACOs (-1.0)typedefLong LongLl;typedef pair<int,int>P;Const intmaxn=1e4+ -, maxm=1e5+ -, inf=0x3f3f3f3f, mod=1e9+7;Constll inf=1e13+7;structedge{int  from, to; intCost ;}; Edge ES[MAXM];p riority_queue<P,vector<P>,greater<P> >Que;vector<int>G[MAXN],T[MAXN];intPre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt;stack<int>s;voidDfsintu) {Pre[u]=lowlink[u]=++Dfs_clock;    S.push (U);  for(intI=0; I<g[u].size (); i++)    {        intv=G[u][i]; if(!Pre[v])            {DFS (v); Lowlink[u]=min (lowlink[u],lowlink[v]); }        Else if(!Sccno[v]) Lowlink[u]=min (lowlink[u],pre[v]); }    if(lowlink[u]==Pre[u]) {scc_cnt++;  while(true)        {            intx=S.top ();            S.pop (); SCCNO[X]=scc_cnt; if(X==u) Break; }    }}voidFIND_SCC (intN) {Dfs_clock=scc_cnt=0; memset (Sccno,0,sizeof(SCCNO)); memset (PRE,0,sizeof(pre));  for(intI=1; i<=n; i++)        if(!Pre[i]) DFS (i);}voidBuildintm) {     for(intI=1; i<=scc_cnt; i++) t[i].clear ();  for(intI=1; i<=m; i++)    {        intU=es[i]. from, v=es[i].to; if(Sccno[u]==sccno[v])Continue;    T[sccno[u]].push_back (Sccno[v]); }}intCY[MAXN],VIS[MAXN];BOOLDFS2 (intu) {     for(intI=0; I<t[u].size (); i++)    {        intv=T[u][i]; if(Vis[v])Continue; VIS[V]=true; if(cy[v]==-1||DFS2 (Cy[v])) {Cy[v]=u; return true; }    }    return false;}intSolveintN) {    intret=0; memset (CY,-1,sizeof(CY));  for(intI=1; i<=n; i++) {memset (Vis,0,sizeof(VIS)); RET+=DFS2 (i); }    returnN-ret;}intMain () {intT; scanf ("%d",&t);  while(t--)    {        intn,m; scanf ("%d%d",&n,&m);  for(intI=1; i<=n;i++) g[i].clear ();  for(intI=1; i<=m; i++)        {            intu,v; scanf ("%d%d",&u,&v); Es[i]. from=u,es[i].to=v;        G[u].push_back (v);        } FIND_SCC (n);        Build (m); cout<<solve (scc_cnt) <<Endl; }    return 0;}
Tarjan indent + Minimum path overlay

HDU 3861.The King's Problem strong Unicom component + minimum path coverage

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.