标签:from 其他 next www ios mis others 城市 相同
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2947 Accepted Submission(s): 1049
代码:
#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; }
HDU 3861.The King’s Problem 强联通分量+最小路径覆盖
标签:from 其他 next www ios mis others 城市 相同
原文地址:http://www.cnblogs.com/GeekZRF/p/7107093.html