博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 3861 The King’s Problem (Tarjan + 二分匹配)
阅读量:6697 次
发布时间:2019-06-25

本文共 3909 字,大约阅读时间需要 13 分钟。

The King’s Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 1142    Accepted Submission(s): 424

Problem Description
In 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.
 

 

Input
The 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.
 

 

Output
The 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 Input
1 3 2 1 2 1 3
 

 

Sample Output
2
 

 

Source
 

 

Recommend
lcy
 

 

#include
#include
#include
#include
using namespace std;const int VM=50100;const int EM=100010;const int INF=0x3f3f3f3f;struct Edge{ int to,nxt;}edge[EM<<1];int n,m,cnt,head[VM];int dep,top,atype;int dfn[VM],low[VM],vis[VM],stack[VM],belong[VM];vector
vt[VM];void addedge(int cu,int cv){ edge[cnt].to=cv; edge[cnt].nxt=head[cu]; head[cu]=cnt++;}void Tarjan(int u){ dfn[u]=low[u]=++dep; stack[top++]=u; vis[u]=1; //开始这里写成=0,WA~~~~~!!!!!!!! for(int i=head[u];i!=-1;i=edge[i].nxt){ int v=edge[i].to; if(!dfn[v]){ Tarjan(v); low[u]=min(low[u],low[v]); }else if(vis[v]){ low[u]=min(low[u],dfn[v]); } } int j; if(dfn[u]==low[u]){ atype++; do{ j=stack[--top]; belong[j]=atype; vis[j]=0; }while(u!=j); }}void init(){ cnt=0; memset(head,-1,sizeof(head)); dep=0, top=0, atype=0; memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(vis,0,sizeof(vis)); memset(belong,0,sizeof(belong));}int linker[VM];int DFS(int u){ int v; for(int i=0;i<(int)vt[u].size();i++){ v=vt[u][i]; if(!vis[v]){ vis[v]=1; if(linker[v]==-1 || DFS(linker[v])){ linker[v]=u; return 1; } } } return 0;}int Hungary(){ int ans=0,u; memset(linker,-1,sizeof(linker)); for(u=1;u<=atype;u++){ memset(vis,0,sizeof(vis)); if(DFS(u)) ans++; } return ans;}int main(){ //freopen("input.txt","r",stdin); int t; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); init(); for(int i=1;i<=n;i++) vt[i].clear(); int u,v; while(m--){ scanf("%d%d",&u,&v); addedge(u,v); } for(int i=1;i<=n;i++) if(!dfn[i]) Tarjan(i); for(int u=1;u<=n;u++) for(int i=head[u];i!=-1;i=edge[i].nxt){ int v=edge[i].to; if(belong[u]!=belong[v]) vt[belong[u]].push_back(belong[v]); } //printf("atype=%d\n",atype); printf("%d\n",atype-Hungary()); } return 0;}

 

转载地址:http://zcvoo.baihongyu.com/

你可能感兴趣的文章
java.util.zip - Recreating directory structure(转)
查看>>
无需写try/catch,也能正常处理异常
查看>>
(原创)优酷androidclient 下载中 bug 解决
查看>>
Web 前端攻防(2014版)-baidu ux前端研发部
查看>>
[歪谈]拽一个贵人出来给你"当炮架子"
查看>>
用TextPaint来绘制文字
查看>>
iOS开发-Get请求,Post请求,同步请求和异步请求
查看>>
关于 ioctl 的 FIONREAD 參数
查看>>
[翻译] IQAudioRecorderController
查看>>
Linux命令-目录处理命令:mkdir
查看>>
js两个小技巧【看到了就记录一下】
查看>>
[React] React Fundamentals: Accessing Child Properties
查看>>
C#发送电子邮件 (异步) z
查看>>
SQL Server CLR全功略之一---CLR介绍和配置
查看>>
struts2.1.6教程二、struts.xml配置及例程
查看>>
memcached client --ref
查看>>
Oracle PL/SQL之LOOP循环控制语句
查看>>
Codeforces Round #301 (Div. 2) C. Ice Cave BFS
查看>>
logcat使用
查看>>
ajax请求模拟登录
查看>>