URAL 10,991-like picture with Flower tree matching

Source: Internet
Author: User

Click to open link

Test instructions: Give n individuals and many relationships, ask to be able to match the number of people and match the person

Thought: Is said to be with the flower tree template problem, to practice a practice, the principle of what is not the weak can understand, right when it is a template directly with forget, like the maximum flow, and according to the weak to do some of the weak problems found this problem test is the ability to build the map, that is, all the template on the top almost won't And then directly build the map on the line, plainly is too weak to understand the principle of direct when the template made

#include <queue> #include <vector> #include <stdio.h> #include <string.h> #include <stdlib.h > #include <iostream> #include <algorithm>using namespace std;typedef long long ll;typedef unsigned long Long Ull;const int inf=0x3f3f3f3f;const ll inf=0x3f3f3f3f3f3f3fll;const int Maxn=230;int f[maxn],spouse[maxn],next[ Maxn],q[maxn],mark[maxn],vis[maxn];int n,bot;vector<int>g[maxn];int find1 (int x) {if (x!=f[x]) f[x]=find1 (F[x]    ); return f[x];}    void Unite (int a,int b) {int aa=find1 (a);    int Bb=find1 (b);    if (AA==BB) return; F[AA]=BB;}    int Findlca (int x,int y) {static int t=0;    t++;            while (1) {if (x!=-1) {x=find1 (x);            if (vis[x]==t) return x;            vis[x]=t;            if (spouse[x]!=-1) x=next[spouse[x]];        else X=-1;    } swap (x, y);        }}void goup (int a,int p) {while (a!=p) {int b=spouse[a],c=next[b];        if (Find1 (c)!=p) next[c]=b; if (mark[b]==2) mark[q[bot++]=b]=1;        if (mark[c]==2) mark[q[bot++]=c]=1;    Unite (A, b); Unite (b,c); a=c;    }}void findaugment (int s) {for (int i=0;i<n;i++) next[i]=-1,f[i]=i,mark[i]=0,vis[i]=-1;    Q[0]=s;bot=1;mark[s]=1;        for (int head=0;spouse[s]==-1&&head<bot;head++) {int x=q[head];            for (unsigned int i=0;i<g[x].size (); i++) {int y=g[x][i]; if (Spouse[x]!=y&&find1 (x)!=find1 (y) &&mark[y]!=2) {if (mark[y]==1) {int p=                    Findlca (x, y);                    if (Find1 (x)!=p) next[x]=y;                    if (Find1 (y)!=p) next[y]=x;                Goup (x,p); Goup (y,p);                    }else if (spouse[y]==-1) {next[y]=x;                        for (int j=y;j!=-1;) {int k=next[j];                        int l=spouse[k];                    Spouse[j]=k;spouse[k]=j;j=l;                } break;                    }else{next[y]=x; MaRk[q[bot++]=spouse[y]]=1;                mark[y]=2;    }}}}}int Num[maxn][maxn];int main () {scanf ("%d", &n);    int x, y;    for (int i=0;i<n;i++) {for (int j=0;j<n;j++) num[i][j]=0;        } while (scanf ("%d%d", &x,&y)!=eof) {x--;y--;            if (x!=y&&num[x][y]==0) {num[x][y]=num[y][x]=1;            G[x].push_back (y);        G[y].push_back (x);    }} for (int i=0;i<n;i++) spouse[i]=-1;    for (int i=0;i<n;i++) {if (spouse[i]==-1) findaugment (i);    } int ans=0;    for (int i=0;i<n;i++) if (spouse[i]!=-1) ans++;    printf ("%d\n", ans);    for (int i=0;i<n;i++) {if (spouse[i]!=-1&&spouse[i]>i) printf ("%d%d\n", i+1,spouse[i]+1); } return 0;}

URAL 10,991-like picture with Flower tree matching

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.