POJ 1904 King's Quest【Tanjar】

传送门

 

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.

 

题意:给你N个王子和N个公主,告诉你王子喜欢那些公主,然后告诉你大臣的方案(最大匹配N),要求你输出,每个王子可以选择那些公主(条件:公主必须是王子喜欢的,王子选择这个公主后,任可以达到最大匹配数N)。

思路:将王子喜欢的公主建单向边(王子一>公主),将大臣给的方案建单向边(公主一>王子),王子编号1—N,公主编号N+1—2N。然后用Tanjar,最后判断王子喜欢的公主是否和王子在同一个强连通分量里。

坑点:王子可能和他不喜欢的女生在同一个强连通分量,所以我们要在王子喜欢的公主里面判断符合条件。

(王子1和公主3在同一个强连通分量里面,但是王子1并不喜欢公主3)

附上代码:

///#include<bits/stdc++.h>
///#include<unordered_map>
///#include<unordered_set>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<bitset>
#include<set>
#include<stack>
#include<map>
#include<list>
#include<new>
#include<vector>
#define MT(a,b) memset(a,b,sizeof(a));
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double pai=acos(-1.0);
const double E=2.718281828459;
const ll mod=998424353;
const int INF=0x3f3f3f3f;

struct node
{
    int e;
    int p;
} load[250000];
int head[4005],sign;

void add_edge(int s,int e)
{
    load[++sign]=node{e,head[s]};
    head[s]=sign;
}

int dfn[4005],low[4005],time;
int belong[4005],cnt;
int stack_[4005],instack[4005],top;

void tanjar(int s)
{
    dfn[s]=low[s]=++time;
    instack[s]=1;
    stack_[++top]=s;
    for(int i=head[s]; i!=-1; i=load[i].p)
    {
        int e=load[i].e;
        if(!dfn[e])
        {
            tanjar(e);
            low[s]=min(low[s],low[e]);
        }
        else
        {
            if(instack[e])
                low[s]=min(low[s],dfn[e]);
        }
    }
    int t;
    if(dfn[s]==low[s])
    {
        cnt++;
        do
        {
            t=stack_[top--];
            instack[t]=0;
            belong[t]=cnt;
        }
        while(s!=t);
    }
}

int n,k,e;
void init()
{
    sign=time=cnt=top=0;
    for(int i=1; i<=2*n; i++)
    {
        head[i]=-1;
        dfn[i]=low[i]=0;
    }
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        init();
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&k);
            while(k--)
            {
                scanf("%d",&e);
                add_edge(i,e+n);
            }
        }
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&e);
            add_edge(e+n,i);
        }
        for(int i=1; i<=2*n; i++)
        {
            if(!dfn[i])
                tanjar(i);
        }
        vector<int>peo;
        for(int i=1; i<=n; i++)
        {
            for(int j=head[i];j!=-1;j=load[j].p)
            {
                e=load[j].e;
                if(belong[e]==belong[i])
                    peo.push_back(e-n);
            }
            int d=peo.size();
            printf("%d",d);
            sort(peo.begin(),peo.end());
            for(int j=0; j<d; j++)
                printf(" %d",peo[j]);
            printf("\n");
            peo.clear();
        }
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值