zoukankan      html  css  js  c++  java
  • poj1904 完美匹配+Tarjan

     
    King's Quest
    Time Limit: 15000MS   Memory Limit: 65536K
    Total Submissions: 9460   Accepted: 3497
    Case Time Limit: 2000MS

    Description

    Once upon a time there lived a king and he had N sons. And there were N beautiful girls in the kingdom and the king knew about each of his sons which of those girls he did like. The sons of the king were young and light-headed, so it was possible for one son to like several girls.

    So the king asked his wizard to find for each of his sons the girl he liked, so that he could marry her. And the king's wizard did it -- for each son the girl that he could marry was chosen, so that he liked this girl and, of course, each beautiful girl had to marry only one of the king's sons.

    However, the king looked at the list and said: "I like the list you have made, but I am not completely satisfied. For each son I would like to know all the girls that he can marry. Of course, after he marries any of those girls, for each other son you must still be able to choose the girl he likes to marry."

    The problem the king wanted the wizard to solve had become too hard for him. You must save wizard's head by solving this problem.

    Input

    The first line of the input contains N -- the number of king's sons (1 <= N <= 2000). Next N lines for each of king's sons contain the list of the girls he likes: first Ki -- the number of those girls, and then Ki different integer numbers, ranging from 1 to N denoting the girls. The sum of all Ki does not exceed 200000.

    The last line of the case contains the original list the wizard had made -- N different integer numbers: for each son the number of the girl he would marry in compliance with this list. It is guaranteed that the list is correct, that is, each son likes the girl he must marry according to this list.

    Output

    Output N lines.For each king's son first print Li -- the number of different girls he likes and can marry so that after his marriage it is possible to marry each of the other king's sons. After that print Li different integer numbers denoting those girls, in ascending order.

    Sample Input

    4
    2 1 2
    2 1 2
    2 2 3
    2 3 4
    1 2 3 4
    

    Sample Output

    2 1 2
    2 1 2
    1 3
    1 4
    题解这里

    #include<vector>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int N=4080;
    const int M=4e6+88;
    vector<int>sc[N];
    vector<int>G[N];
    int head[N],dfn[N],low[N],q[N];
    bool instack[N],mp[2008][2008];
    int tot,scnt,l,cnt;
    struct node
    {
        int to,next;
    } e[M];
    void add(int u,int v)
    {
        e[tot].to=v;
        e[tot].next=head[u];
        head[u]=tot++;
    }
    void Tarjan(int u)
    {
        dfn[u]=low[u]=++cnt;
        q[l++]=u;
        instack[u]=1;
        for(int i=head[u]; ~i; i=e[i].next)
        {
            int v=e[i].to;
            if(!dfn[v])
            {
                Tarjan(v);
                low[u]=min(low[u],low[v]);
            }
            else if(instack[v]&&dfn[v]<low[u]) low[u]=dfn[v];
        }
        if(low[u]==dfn[u])
        {
            ++scnt;
            int t;
            do
            {
                t=q[--l];
                sc[scnt].push_back(t);
                instack[t]=0;
            }
            while(t!=u);
        }
    }
    int main()
    {
        int n,x,y;
        while(scanf("%d",&n)!=EOF)
        {
            tot=scnt=cnt=l=0;
            memset(head,-1,sizeof(head));
            memset(dfn,0,sizeof(dfn));
            memset(mp,0,sizeof(mp));
            for(int i=1; i<=n; ++i)
            {
                scanf("%d",&x);
                while(x--)
                {
                    scanf("%d",&y);
                    add(i,y+n);
                    G[i].clear();
                    sc[i].clear();
                    mp[i][y]=1;
                }
            }
            for(int i=1; i<=n; ++i)
            {
                scanf("%d",&x);
                add(x+n,i);
            }
            for(int i=1; i<=n; ++i) if(!dfn[i]) Tarjan(i);
            for(int i=1; i<=scnt; ++i) sort(sc[i].begin(),sc[i].end());
            for(int i=1; i<=scnt; ++i)
            {
                int tc=upper_bound(sc[i].begin(),sc[i].end(),n)-sc[i].begin();
                for(int j=0; j<tc; ++j) for(int k=tc; k<(int)sc[i].size(); ++k) if(mp[sc[i][j]][sc[i][k]-n] ) G[sc[i][j]].push_back(sc[i][k]-n);
            }
            for(int i=1; i<=n; ++i)
            {
                printf("%d",(int)G[i].size());
                for(int j=0; j<(int)G[i].size(); ++j) printf(" %d",G[i][j]);
                puts("");
            }
        }
    }
  • 相关阅读:
    微服务化之无状态化与容器化
    微服务化的不同阶段 Kubernetes 的不同玩法
    网易大数据技术沙龙
    十年•杭研技术秀 | “网易云存储服务”从0到1发展之路
    传统业务上云:跨AZ容灾架构解析
    kubernetes1.9管中窥豹-CRD概念、使用场景及实例
    浅谈 kubernetes service 那些事 (下篇)
    浅谈 kubernetes service 那些事(上篇)
    Docker容器的原理与实践(上)
    Docker容器的原理与实践 (下)
  • 原文地址:https://www.cnblogs.com/mfys/p/7612159.html
Copyright © 2011-2022 走看看