Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1091 Accepted Submission(s): 404
1 3 2 1 2 1 3
2
强连通缩点+二分匹配
求最小路径覆盖=(顶点数 - 最大匹配数);
题目上说的很清楚,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
,所以显然要先缩点,之后用二分匹配求最小路径覆盖,思路很清晰!
#include<iostream> #include<string.h> #include<stdio.h> #include<vector> using namespace std; #define max_n 5005 #define max_e 250002 #define inf 99999999 int stack[max_n],top;//栈 int isInStack[max_n];//是否在栈内 int low[max_n],dfn[max_n],tim;//点的low,dfn值;time从1开始 int node_id;//强连通分量的个数 int head[max_n],s_edge;//邻接表头 s_edge从1开始 int gro_id[max_n];//记录某个点属于哪个强连通分量 int n,m; bool vis[max_n]; int match[max_n]; vector<int> vec[max_n];//边的后节点存储 struct Node { int to; int next; } edge[max_e]; int head1[max_n]; int NE; struct Node1 { int from; int to; int next; } edge1[max_e]; void init()//初始化 { s_edge=0;//存储 memset(head,0,sizeof(head)); memset(edge,0,sizeof(edge)); top=0;//tarjian初始化 tim=0; node_id=0; memset(isInStack,0,sizeof(isInStack)); memset(low,0,sizeof(low)); memset(dfn,0,sizeof(dfn)); NE=0; memset(head1,-1,sizeof(head1)); memset(match,-1,sizeof(match)); } void addedge(int u,int v) { s_edge++; edge[s_edge].to=v; edge[s_edge].next=head[u]; head[u]=s_edge; } void add(int u,int v) { edge1[NE].from=u; edge1[NE].to=v; edge1[NE].next=head1[u]; head1[u]=NE++; } int min(int a,int b) { if(a<b)return a; else return b; } void tarjan(int u) { //low值为u或u的子树能够追溯到得最早的栈中节点的次序号 stack[top++]=u; isInStack[u]=1; dfn[u]=++tim; //记录点u出现的记录,并放在栈中 low[u]=tim; int e,v; for(e=head[u]; e; e=edge[e].next) //如果是叶子节点,head[u]=0,edge[e].next=0; { v=edge[e].to; if(!dfn[v]) { tarjan(v); low[u]=min(low[u],low[v]); } else if(isInStack[v]) low[u]=min(low[u],dfn[v]); } int j; if(dfn[u]==low[u])//找到一个强连通,元素出栈 { node_id++; while(j=stack[--top]) { isInStack[j]=0; gro_id[j]=node_id; if(j==u)break; } } } void find()//tarjian寻找 { for(int i = 1 ; i <=n ; ++i) { if(!dfn[i]) { tarjan(i); } } } int dfs(int t) { for(int i=head1[t]; i!=-1; i=edge1[i].next) { int idx=edge1[i].to; if(!vis[idx]) { vis[idx]=1; if(match[idx]==-1||dfs(match[idx])) { match[idx]=t; return 1; } } } return 0; } int main() { int t; int a,b; cin>>t; while(t--) { scanf("%d %d",&n,&m); init(); for(int i=1; i<=n; i++) vec[i].clear(); for(int i = 0 ; i <m ; ++i) { scanf("%d%d",&a,&b); vec[a].push_back(b); addedge(a,b); } find(); for(int i=1; i<=n; i++) { for(int j=0; j<vec[i].size(); j++)//求强连通分量的出、入度 { if(gro_id[i]!=gro_id[vec[i][j]]) { add(gro_id[i],gro_id[vec[i][j]]); } } } int ans=0; for(int i=1; i<=node_id; i++) { memset(vis,0,sizeof(vis)); if(dfs(i)) ans++; } cout<<node_id-ans<<endl; } return 0; }