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("");
            }
        }
    }
  • 相关阅读:
    JS设计模式的坑
    nth-child()和nth-of-type()的区别,以及如何在nth中添加变量和表达式
    for循环中,使用闭包和不使用闭包的区别以及原因
    JS闭包的基础知识,闭包的本质,闭包的作用,闭包的间谍属性和闭包的遗憾
    前端和后端数据交互的基本知识和常见方式
    dedecms手机PC同步更新插件的bug修复和前后端调试的经验
    065 女神颜值打分系统
    029 令牌桶算法限流
    04-01 集成学习基础
    028 【博弈论】关于三姬分金(五海盗分赃)的博弈论问题分析
  • 原文地址:https://www.cnblogs.com/mfys/p/7612159.html
Copyright © 2011-2022 走看看