zoukankan      html  css  js  c++  java
  • (tarjan) poj 1904

    King's Quest
    Time Limit: 15000MS   Memory Limit: 65536K
    Total Submissions: 7838   Accepted: 2841
    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
    

    Hint

    This problem has huge input and output data,use scanf() and printf() instead of cin and cout to read data to avoid time limit exceed.

    Source

     

    题意:有n个王子,有n个美女,每个王子可能同时喜欢多个美女,数据已经给出一组完全匹配的方案。问在满足所有王子都能完全匹配的情况下,每个王子能选择的对象分别有谁,按升序输出。

    主要是建图的问题啊

     

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<cstdlib>
    #include<cmath>
    #include<queue>
    #include<vector>
    #include<set>
    #include<stack>
    using namespace std;
    stack<int> s;
    vector<int> e[4010];
    int n,top,newflag;
    int Dfs[4010],low[4010],use[4010],isstack[4010];
    void tarjan(int u)
    {
        Dfs[u]=low[u]=++top;
        isstack[u]=1;
        s.push(u);
        for(int i=0;i<e[u].size();i++)
        {
            int v=e[u][i];
            if(!Dfs[v])
            {
                tarjan(v);
                low[u]=min(low[u],low[v]);
            }
            else if(isstack[v])
                low[u]=min(low[u],Dfs[v]);
        }
        if(Dfs[u]==low[u])
        {
            newflag++;
            int x;
            do
            {
                x=s.top();
                s.pop();
                isstack[x]=0;
                use[x]=newflag;
            }while(x!=u);
        }
    }
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            int k,x;
            scanf("%d",&k);
            while(k--)
            {
                scanf("%d",&x);
                e[i].push_back(x+n);
            }
        }
        for(int i=1;i<=n;i++)
        {
            int x;
            scanf("%d",&x);
            e[x+n].push_back(i);
        }
        for(int i=1;i<=n;i++)
        {
            if(!Dfs[i])
                tarjan(i);
        }
        for(int i=1;i<=n;i++)
        {
            priority_queue<int ,vector<int> ,greater<int> > q;
            for(int j=0;j<e[i].size();j++)
            {
                int v=e[i][j];
                if(use[v]==use[i])
                    q.push(v-n);
            }
            printf("%d",q.size());
            while(!q.empty())
            {
                printf(" %d",q.top());
                q.pop();
            }
            printf("
    ");
        }
        return 0;
    }
    

      

  • 相关阅读:
    面试题汇总
    Chromium中多线程及并发技术要点(C/C++)
    关于《Swift开发指南》背后的那些事
    HDU 3080 The plan of city rebuild(除点最小生成树)
    2.1.3策略模式(5.9)
    Shell脚本之监视指定进程的执行状态
    敏捷开发中的10大错误认识
    mysql插入中文数据报错:incorrect string value
    JapserReport导出PDF Could not load the following font错误
    冒泡排序
  • 原文地址:https://www.cnblogs.com/water-full/p/4527520.html
Copyright © 2011-2022 走看看