1. 程式人生 > >POJ 1904 King's Quest(SCC的巧妙應用,思維題!!!,經典題)

POJ 1904 King's Quest(SCC的巧妙應用,思維題!!!,經典題)

King's Quest
Time Limit: 15000MS   Memory Limit: 65536K
Total Submissions: 10305   Accepted: 3798
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.   題目意思:
國王有n個兒子,每個兒子喜歡ki個女孩,國王想讓王子和他喜歡的人結婚,
就讓巫師做一個列表,就是國王想知道每個王子可以和哪些女生結合且不影響其他王子也能和自己喜歡的人結婚
其實就是求王子和哪些女生結婚不影響最大匹配!!! 做法:
對每個王子喜歡的女生,王子到女孩建邊
對完備匹配,女孩到王子建邊
然後和王子x屬於同一個SCC的且王子喜歡的女生就是王子x在不影響最大匹配的情況下,可以結婚的女生 1.為什麼對完備匹配反向建邊?
保證完備匹配的女生y和王子x是屬於同一個SCC 2.為什麼該圖是二分圖?
題目說n個王子,n個女生,保證每個王子都可以匹配到喜歡的女生
所以該圖是二分圖
且同一個SCC中王子和女生的數目是相同的!! 3.為什麼王子不能選擇其他SCC的女生?
反證法:如果強連通分量1中的王子選擇了強連通分量2中的妹子,
那麼勢必強連通分量2中的一個王子無法在自己的強連通分量中找到妹子,
那麼他就會去別的強連通分量找妹子,這樣一直迴圈下去,
我們知道最終一定是經過了強連通分量1,2,x1,x2,xn,……,1,
王子們才能都找到自己的妹子,這樣這些強連通分量1,2,x1,x2,xn,……,1會構成一個強連通分量,
與題設在不同強連通分量中找妹子不符 和王子的同一個SCC裡面的女生,該王子都可以選擇與其結婚(前提是該王子喜歡) 坑點:
1.對在同一個SCC裡面的女生y,王子x不一定喜歡
2.輸出的編號要升序排序  
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
#define INF 0x7fffffff
#define me(a,x) memset(a,x,sizeof(a))
int mon1[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
int mon2[13]= {0,31,29,31,30,31,30,31,31,30,31,30,31};
int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};

int getval()
{
    int ret(0);
    char c;
    while((c=getchar())==' '||c=='\n'||c=='\r');
    ret=c-'0';
    while((c=getchar())!=' '&&c!='\n'&&c!='\r')
        ret=ret*10+c-'0';
    return ret;
}
void out(int a)
{
    if(a>9)
        out(a/10);
    putchar(a%10+'0');
}

#define max_v 20005
int vis[max_v];
int dfn[max_v];
int low[max_v];
int color[max_v];
int stk[max_v];
vector<int> G[max_v];
int n,m;
int sig,cnt,sp;

priority_queue<int,vector<int>,greater<int> > q;//優先佇列 升序
void init()
{
    me(vis,0);
    me(dfn,0);
    me(color,0);
    me(low,0);
    for(int i=1;i<=n*2;i++)
    {
        G[i].clear();
    }
    sig=0;
    cnt=1;
    sp=-1;
}

void tarjan(int u)
{
    vis[u]=1;
    dfn[u]=low[u]=cnt++;
    stk[++sp]=u;
    for(int j=0;j<G[u].size();j++)
    {
        int v=G[u][j];
        if(vis[v]==0)
            tarjan(v);
        if(vis[v]==1)
            low[u]=min(low[v],low[u]);
    }
    if(low[u]==dfn[u])
    {
        sig++;
        do
        {
            color[stk[sp]]=sig;
            vis[stk[sp]]=-1;
        }while(stk[sp--]!=u);
    }
}
int main()
{
    int x,y;
    while(~scanf("%d",&n))
    {
        init();
        for(int i=1;i<=n;i++)
        {
            m=getval();//輸入外掛
            for(int j=1;j<=m;j++)
            {
                y=getval();
                y+=n;
                if(count(G[i].begin(),G[i].end(),y)==0)
                    G[i].push_back(y);
            }
        }
        for(int i=1;i<=n;i++)
        {
            x=getval();
            x+=n;
            if(count(G[x].begin(),G[x].end(),i)==0)
                G[x].push_back(i);
        }
        for(int i=1;i<=n+n;i++)
        {
            if(vis[i]==0)
                tarjan(i);
        }
        int sum=0;
        for(int i=1;i<=n;i++)
        {
            sum=0;
            for(int j=0;j<G[i].size();j++)//只考慮王子i喜歡的女生
            {
                if(color[i]==color[G[i][j]])//顏色相同代表屬於同一個SCC
                {
                    sum++;
                    q.push(G[i][j]-n);
                }
            }
            out(sum);
            while(!q.empty())
            {
                printf(" ");
                out(q.top());//輸出外掛
                q.pop();
            }
            printf("\n");
        }
    }
    return 0;
}

/*
題目意思:
國王有n個兒子,每個兒子喜歡ki個女孩,國王想讓王子和他喜歡的人結婚,
就讓巫師做一個列表,就是國王想知道每個王子可以和哪些女生結合且不影響其他王子也能和自己喜歡的人結婚
其實就是求王子和哪些女生結婚不影響最大匹配!!!

做法:
對每個王子喜歡的女生,王子到女孩建邊
對完備匹配,女孩到王子建邊
然後和王子x屬於同一個SCC的且王子喜歡的女生就是王子x在不影響最大匹配的情況下,可以結婚的女生

1.為什麼對完備匹配反向建邊?
保證完備匹配的女生y和王子x是屬於同一個SCC

2.為什麼該圖是二分圖?
題目說n個王子,n個女生,保證每個王子都可以匹配到喜歡的女生
所以該圖是二分圖
且同一個SCC中王子和女生的數目是相同的!!

3.為什麼王子不能選擇其他SCC的女生?
反證法:如果強連通分量1中的王子選擇了強連通分量2中的妹子,
那麼勢必強連通分量2中的一個王子無法在自己的強連通分量中找到妹子,
那麼他就會去別的強連通分量找妹子,這樣一直迴圈下去,
我們知道最終一定是經過了強連通分量1,2,x1,x2,xn,……,1,
王子們才能都找到自己的妹子,這樣這些強連通分量1,2,x1,x2,xn,……,1會構成一個強連通分量,
與題設在不同強連通分量中找妹子不符

和王子的同一個SCC裡面的女生,該王子都可以選擇與其結婚(前提是該王子喜歡)

坑點:
1.對在同一個SCC裡面的女生y,王子x不一定喜歡
2.輸出的編號要升序排序

*/