Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
200题纪念!先拆点,依照题目要求连边。 匈牙利即可。 贴代码: #include <cstdio> #include <string.h> #include <vector> using namespace std; const int NMax=200; int N,M,connect[NMax]; bool ok[NMax],state[NMax]; vector<int> G[NMax]; bool DFS(int a) { state[a]=1; for(int i=0;i<G[a].size();i++) { if(!connect[G[a][i]]) { ok[i]=1; connect[G[a][i]]=a; return 1; }else if(!state[connect[G[a][i]]]&&DFS(connect[G[a][i]])) { ok[i]=1; connect[G[a][i]]=a; return 1; } } return 0; } int main() { int T,x,y; scanf("%d",&T); while(T--) { scanf("%d%d",&N,&M); for(int i=1;i<=N;i++) {G[i].clear();ok[i]=0;connect[i]=0;} for(int i=1;i<=M;i++) { scanf("%d%d",&x,&y); G[x].push_back(y); } int ret=0; for(int i=1;i<=N;i++) { for(int j=0;j<G[i].size();j++) if(!connect[G[i][j]]){ connect[G[i][j]]=i; ok[i]=1; ret++; break; } } for(int i=1;i<=N;i++) { memset(state,0,sizeof(state)); if(!ok[i]) ret+=DFS(i); } printf("%d\n",N-ret); } //getchar();getchar(); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator