HDU 3861.The King’s Problem 強聯通分量+最小路徑覆蓋

來源:互聯網
上載者:User

標籤:from   其他   next   www   ios   mis   others   城市   相同   

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 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 effectively, 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 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 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 should contain T lines. For each test case you should just output an integer which is the least number of states the king have to divide into. 

 

Sample Input13 21 21 3 

 

Sample Output2 

 

Source 2011 Multi-University Training Contest 3 - Host by BIT 題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=3861題意:有n個城市,m條有向路徑。現在要建一些州,每個城市屬於一個州,如果兩個城市u,v可以互相到達,那麼u,v屬於同一個州。如果u,v在同一個州,那麼u可以到達v或者v可以到達u,並且不經過其他州的城市。求最少要建幾個州。思路:因為相互可達的城市屬於同一個州,進行tarjan縮點。建立的新圖是一個DAG。在一個有向圖中,找出最少的路徑,使得這些路徑經過了所有的點,並且每一條路徑經過的點各不相同。這是一種最小路徑覆蓋問題。有向非循環圖(DAG)的最小路徑覆蓋

代碼:

#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<map>#include<queue>#include<stack>#include<vector>#include<set>using namespace std;#define PI acos(-1.0)typedef long long ll;typedef pair<int,int> P;const int maxn=1e4+100,maxm=1e5+100,inf=0x3f3f3f3f,mod=1e9+7;const ll INF=1e13+7;struct edge{    int from,to;    int cost;};edge es[maxm];priority_queue<P,vector<P>,greater<P> >que;vector<int>G[maxn],T[maxn];int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt;stack<int>s;void dfs(int u){    pre[u]=lowlink[u]=++dfs_clock;    s.push(u);    for(int i=0; i<G[u].size(); i++)    {        int v=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)        {            int x=s.top();            s.pop();            sccno[x]=scc_cnt;            if(x==u) break;        }    }}void find_scc(int n){    dfs_clock=scc_cnt=0;    memset(sccno,0,sizeof(sccno));    memset(pre,0,sizeof(pre));    for(int i=1; i<=n; i++)        if(!pre[i]) dfs(i);}void build(int m){    for(int i=1; i<=scc_cnt; i++) T[i].clear();    for(int i=1; i<=m; i++)    {        int u=es[i].from,v=es[i].to;        if(sccno[u]==sccno[v]) continue;        T[sccno[u]].push_back(sccno[v]);    }}int cy[maxn],vis[maxn];bool dfs2(int u){    for(int i=0; i<T[u].size(); i++)    {        int v=T[u][i];        if(vis[v]) continue;        vis[v]=true;        if(cy[v]==-1||dfs2(cy[v]))        {            cy[v]=u;            return true;        }    }    return false;}int solve(int n){    int ret=0;    memset(cy,-1,sizeof(cy));    for(int i=1; i<=n; i++)    {        memset(vis,0,sizeof(vis));        ret+=dfs2(i);    }    return n-ret;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        int n,m;        scanf("%d%d",&n,&m);        for(int i=1;i<=n;i++) G[i].clear();        for(int i=1; i<=m; i++)        {            int u,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縮點+最小路徑覆蓋

 

 

HDU 3861.The King’s Problem 強聯通分量+最小路徑覆蓋

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.