King's Quest —— POJ1904(ZOJ2470)Tarjan縮點

King’s Quest


  • Time Limit: 15000MS Memory Limit: 65536K
    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.markdown

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.dom

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.」ui

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.this

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.spa

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.code

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.ip

Sample Input

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

Sample Output

2 1 2
2 1 2
1 3
1 4get

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.input

Source

Northeastern Europe 2003

題意 :國王有n個兒子,同時在他的王國裏有n個漂亮的女孩,國王知道他的兒子喜歡那些女孩(不止一個哦),國王要求謀士爲他的每個兒子挑一個他喜歡的女孩,讓他的兒子娶這個女孩,每個女孩只能嫁給一個國王的兒子,當國王看到謀士給他的選擇名單後,不是很滿意,他須要知道他的兒子能夠和哪些女孩結婚,固然只要他和選擇女孩結婚,其餘的兄弟就能選到他喜歡的其餘女孩結婚。

思路 : 能夠將兒子與女孩之間的關係看作邊,人看作點,創建有向圖兒子[1,n],女孩[n+1,2n]。因爲名單的緣由,因此每個兒子在能夠結婚的女孩中必有名單上的女孩,因此能夠在名單上的女孩和對應的兒子之間創建一條邊,使得兒子和女孩處於同一個強連通份量中,創建圖之後能夠發現強連通份量中的元素的女孩是均可以娶的,再找出他喜歡的就是對應兒子能夠娶的。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

const int MaxM = 210000;

const int MaxN = 4100;

vector<int>Map[MaxN];

int dfn[MaxN],low[MaxN],vis[MaxN],dep;

int pre[MaxN],num;


vector<int>Gnum[MaxN];

stack <int> S;

int n;

void Init()
{
    memset(vis,0,sizeof(vis));

    for(int i=0;i<=2*n;i++)
    {
        Gnum[i].clear();
        Map[i].clear();
    }

    dep = 0; num = 0 ;
}

void Tarjan(int u) //Tarjan縮點
{
    dfn[u] = low[u] = dep++;

    vis[u] = 1;

    S.push(u);

    for(int i = 0 ;i<Map[u].size();i++)
    {
        int v = Map[u][i];

        if(vis[v]==1)
        {
            low[u] = min(low[u],dfn[v]);
        }
        else if(vis[v]==0)
        {
            Tarjan(v);

            low[u] = min(low[u],low[v]);
        }
    }

    if(dfn[u] == low[u])
    {
        while(!S.empty())
        {
            int  v = S.top();

            S.pop();

            pre[v] = num;

            vis[v] = 2;

            if(v == u)
            {
                break;
            }
        }

        num++;
    }
}
int Scan()     //輸入外掛
{
    int res=0,ch,flag=0;
    if((ch=getchar())=='-')
        flag=1;
    else if(ch>='0'&&ch<='9')
        res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')
        res=res*10+ch-'0';
    return flag?-res:res;
}
void Out(int a)  //輸出外掛
{
    if(a>9)
         Out(a/10);
    putchar(a%10+'0');
}

int main()
{
    while(~scanf("%d",&n))
    {
        Init();

        int v,Ki;

        for(int i=1;i<=n;i++)
        {
            Ki = Scan();

            for(int j=1;j<=Ki;j++)
            {

                v = Scan();

                Map[i].push_back(v+n);
            }
        }

        for(int i=1;i<=n;i++)
        {
            v = Scan();

            Map[v+n].push_back(i);
        }

        for(int i=1;i<=n;i++) //使每個兒子喜歡的女孩編號有序
        {
            sort(Map[i].begin(),Map[i].end());
        }

        for(int i=1;i<=n;i++)
        {
            if(vis[i]==0)
            {
                Tarjan(i);

            }
        }

        for(int i=1;i<=n;i++)
        {

            int ans = 0;

            for(int j = 0;j<Map[i].size();j++)
            {
                if(pre[i]==pre[Map[i][j]])//處於同一個強連通份量爲可娶的。
                {
                    ans++;
                }
            }

            Out(ans);

            for(int j=0;j<Map[i].size();j++)
            {
                if(pre[i]==pre[Map[i][j]])
                {
                    putchar(' ');
                    Out(Map[i][j]-n);
                }
            }

            puts("");
        }
        // puts(""); ZOJ多一個換行


    }
    return 0;
}
相關文章
相關標籤/搜索