HDU 3861 the king's problem (strongly connected + least path overwrite)

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): 1637 accepted submission (s): 600


Problem descriptionin the kingdom of silence, the King has a new problem. there are n cities in the Kingdom and there are m directional roads between the cities. that means that if there is a road from u to V, you can only go from City U to city V, but can't go from city V to City U. in order to rule his kingdom more than tively, the King want to divide his kingdom into several States, and each city must belong to exactly one state. what's more, for each pair of city (u, v), if there is one way to go from u to V and go from V to U, (u, v) have to belong 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 for your help, he wants to know the least number of states he have to divide the kingdom.
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 two integers n, m (0 <n <= 5000,0 <= m <= 100000), the number of cities and roads in the Kingdom. the next M lines each contains two integers U and V (1 <= u, v <= N), indicating that there is a road going from City U to city v.
Outputthe output shoshould contain t lines. For each test case you shoshould just output an integer which is the least number of states the king have to divide.
Sample Input
13 21 21 3
 
Sample output
2


#include"stdio.h"#include"string.h"#include"queue"#include"vector"#include"algorithm"using namespace std;#define N 5005#define M 100005#define min(a,b) (a<b?a:b)vector<int>g[N];struct node{    int u,v,next;}e[M];int t,stop,index,bcnt,head[N],link[N];int mark[N],dfn[N],low[N],stap[N],be[N];bool vis[N];int find(int k){    int i,v;    for(i=0;i<g[k].size();i++)    {        v=g[k][i];        if(!vis[v])        {            vis[v]=true;            if(link[v]==-1||find(link[v]))            {                link[v]=k;                return 1;            }        }    }    return 0;}void add(int u,int v){    e[t].u=u;    e[t].v=v;    e[t].next=head[u];    head[u]=t++;}void tarjan(int u){    int i,v;    dfn[u]=low[u]=++index;    stap[++stop]=u;    mark[u]=1;    for(i=head[u];i!=-1;i=e[i].next)    {        v=e[i].v;        if(!dfn[v])        {            tarjan(v);            low[u]=min(low[u],low[v]);        }        else if(mark[v])            low[u]=min(low[u],dfn[v]);    }    if(dfn[u]==low[u])    {        bcnt++;        do        {            v=stap[stop--];            mark[v]=0;            be[v]=bcnt;        }        while(u!=v);    }}void solve(int n){    int i;    index=bcnt=stop=0;    memset(dfn,0,sizeof(dfn));    for(i=1;i<=n;i++)    {        if(!dfn[i])            tarjan(i);    }}void work(int n){    int i,j,u,v;    for(i=0;i<N;i++)        g[i].clear();    for(i=1;i<=n;i++)    {        u=be[i];        for(j=head[i];j!=-1;j=e[j].next)        {            v=be[e[j].v];            if(u!=v)            {                g[u].push_back(v);               // g[v].push_back(u);            }        }    }    int ans=0;    memset(link,-1,sizeof(link));    for(i=1;i<=bcnt;i++)    {        memset(vis,false,sizeof(vis));        ans+=find(i);    }    printf("%d\n",bcnt-ans);}int main(){    int T,n,m,u,v,i;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&m);        t=0;        memset(head,-1,sizeof(head));        for(i=0;i<m;i++)        {            scanf("%d%d",&u,&v);            add(u,v);        }        solve(n);        //printf("bcnt%d\n",bcnt);        work(n);    }    return 0;}


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.