POJ 3894 System Engineer bipartite graph matching the maximum stream of Hopcroft_Carp

Source: Internet
Author: User

 

System Engineer
Time Limit:1000 MS   Memory Limit:65536 K
Total Submissions:236   Accepted:98

Description

Bob is a skilled system engineer. he is always facing challenging problems, and now he must solve a new one. he has to handle a set of servers with differing capabilities available to process job requests from persistent sources
-Jobs that need to be processed over a long or indefinite period of time. A sequence of persistent job requests arrives revealing a subset of servers capable of servicing their request. A job is processed on a single server and a server processes only one
Job. bob has to schedule the maximum number of jobs on the servers. for example, if there are 2 jobs J1, J2 and 2 servers S1, S2, job J1 requiring the server S1, and job J2 requiring also the server S1. In this case Bob can schedule only one job. can you help
Him?
In the general case there are n jobs numbered from 0 to n-1, n servers numbered from n to 2 * n-1, and a sequence of job requests. the problem asks to find the maximum number of jobs that can be processed.

Input

The program input is at most 1 MB. each data set in the file stands for a participant set of jobs. A data set starts with the number n (n <= 10000) of jobs, followed by the list of required servers for each job, in the format: Jobnumber:
(Nr_servers) S1... snr_servers
The program prints the maximum number of jobs that can be processed.
White spaces can occur freely in the input. The input data are correct and terminate with an end of file.

Output

For each set of data the program prints the result to the standard output from the beginning of a line.

Sample Input

2 0: (1) 2 1: (1) 2 1 0: (1) 1

Sample output

11

Hint

There are two data sets. in the first case, the number of jobs N is 2, numbered 0 and 1. the sequence of requests for job 0 is: 0: (1) 2, meaning that job 0 requires 1 sever, the server numbered 2. the sequence of requests
Job 1 is: 1: (1) 2, meaning that job 1 requires 1 sever, the server numbered 2. the result for the data set is the length of the maximum number of scheduled jobs, 1.

Source

Southeastern European regional programming contest 2009 bipartite graph matching. N is 10000 at maximum, and m is unknown. The normal Hungary algorithm should time out.

The time complexity of the hopcroft_carp algorithm is O (n ^ 0.5 * m ).

Using the dinic algorithm to find the maximum stream is faster than matching.

 

Hopcroft_carp code:

#include<cstdio>#include<cstring>#include<vector>#include<queue>using namespace std;const int N=10005;const int INF=1<<28;int Mx[N],My[N],Nx,Ny;int dx[N],dy[N],dis;vector<int> adj[N];bool vst[N];bool searchP(){    queue<int> q;    dis=INF;    memset(dx,-1,sizeof(dx));    memset(dy,-1,sizeof(dy));    for(int i=0;i<Nx;i++)        if(Mx[i]==-1)        {            q.push(i);            dx[i]=0;        }    while(!q.empty())    {        int u=q.front(),v;        q.pop();        if(dx[u]>dis)break;for(int i=0;i<adj[u].size();i++)            if(dy[v=adj[u][i]]==-1)            {                dy[v]=dx[u]+1;                if(My[v]==-1)dis = dy[v];                else                {                    dx[My[v]]=dy[v]+1;                    q.push(My[v]);                }            }    }    return dis!=INF;}bool dfs(int u){int i,v;for(i=0;i<adj[u].size();i++)        if(!vst[v=adj[u][i]]&&dy[v]==dx[u]+1)        {            vst[v]=1;            if(My[v]!=-1&&dy[v]==dis)continue;            if(My[v]==-1||dfs(My[v]))            {                My[v]=u;                Mx[u]=v;                return 1;            }        }    return 0;}int MaxMatch()//O(n^0.5*m){    int ans=0;    memset(Mx,-1,sizeof(Mx));    memset(My,-1,sizeof(My));    while(searchP())    {        memset(vst,0,sizeof(vst));        for(int i=0;i<Nx;i++)            if(Mx[i]==-1&&dfs(i))ans++;    }    return ans;}int main(){int n,m,i,j,k;while(~scanf("%d",&n)){for(i=0;i<n;i++){scanf("%d: (%d)",&j,&m);adj[j].clear();while(m--){scanf("%d",&k);adj[j].push_back(k-n);}}Nx=Ny=n;printf("%d\n",MaxMatch());}}

Dinic code:

# Include <cstdio> # include <cstring> # define n 20005 # define M 200005 # define INF 999999999 # define min (a, B) (a) <(B )? (A) :( B) int n, m, S, T, num, adj [N], DIS [N], Q [N]; struct edge {int V, w, pre;} e [m]; void insert (int u, int V, int W) {e [num] = (edge) {V, W, adj [u]}; adj [u] = num ++; E [num] = (edge) {u, 0, adj [v]}; // directed graph adj [v] = num ++;} int BFS () {int I, X, V, head = 0, tail = 0; memset (DIS, 0, sizeof (DIS); DIS [s] = 1; Q [++ tail] = s; while (Head! = Tail) {x = Q [head = (Head + 1) % N]; for (I = adj [X]; ~ I; I = E [I]. PRE) if (E [I]. W &&! Dis [V = E [I]. v]) {dis [v] = dis [x] + 1; if (V = T) return 1; Q [tail = (tail + 1) % N] = V ;}} return 0;} int DFS (int x, int limit) {If (x = T) return limit; int I, V, TMP, cost = 0; for (I = adj [X]; ~ I & Cost <limit; I = E [I]. PRE) if (E [I]. W & dis [x] = dis [V = E [I]. v]-1) {TMP = DFS (v, min (Limit-cost, E [I]. w); If (TMP) {e [I]. w-= TMP; E [I ^ 1]. W + = TMP; Cost + = TMP;} elsedis [v] =-1;} return cost;} int dinic () {int ans = 0; while (BFS ()) ans + = DFS (S, INF); Return ans;} int main () {While (~ Scanf ("% d", & N) {int I, j, k; memset (adj,-1, sizeof (adj); num = 0; S = 0; t = n + 1; for (I = 0; I <n; I ++) {scanf ("% d: (% d)", & J, & M); While (M --) {scanf ("% d", & K); insert (J + 1, k + 1, 1) ;}} for (I = 1; I <= N; I ++) {insert (S, I, 1); insert (I + N, T, 1);} printf ("% d \ n ", dinic ());}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.