强连通 UVA 1327 King's Quest

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

Input consists of several datasets. The first line of each dataset contains N - the number of king's sons (1$ \le$N$ \le$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 200 000.

The last line of the input file 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 arbitrary 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


题意:有n个新郎,n个新娘,他们要配对,题目给出新郎愿意跟哪位新娘配对,并且给出了一组完美匹配,问每个新郎在不改变最大匹配数的前提下,他能跟哪位新娘配对。


思路:我们来看看,当一个新郎要换对象的时候会发生什么事情,我们先把新郎连边到新的对象,那么新对象肯定有一个原配嘛?那么那个原配又要招新对象了,然后原配的新对象的原配又要找新对象,知道有一个原配能跟一开始这个新郎的对象配对。一个很直接的想法就是,破坏原图,然后在找最大匹配,看能不能满足,其实或许能过吧? 但是其实你再思考一下这个问题的本质是什么?我们来看一个简单的例子:

以下(x,y)表示x有一条边指向y,   (1',1) , (1,2') , (2,1') , (2',2) 其中x表示新郎,x'表示新娘,如果a'是b原配,那么我们有一条边(a',b) ,如果c能与d(d不是原配)结婚,那么有(c,d) , 回到上面的例子,这个例子存在了一个环,

1 - > 2' - > 2 - > 1' - > 1

这样,1的对象如果变为了2’,2的对象也能变为1'。最大匹配数没有任何变化,但是连接的状态发生了变化。

到更加一般的情况,如果a能连到b', 同时b'能连到c' , c能连到a' ,也是可以的,这里面同样有一个环。所以现在我们的目标就是要找到所有的环,其实因为这里我们的图(a,a') (a',a) 不会同时存在,所以只要我们找到各个强连通分量,就能确定某两个点是否在一个环里面,也就是在一个强连通分量里面的点的状态可以随意改变其中一个,其余一定能通过某种方式匹配过来。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define clr(a,x) memset(a,x,sizeof(a))
const int maxn=5000+5;
int pre[maxn],sccno[maxn],dfn,scc_cnt;
int low[maxn],S[maxn],c;

inline int min(int a,int b) { return a<b?a:b; }
int n;

vector<int> adj[maxn];
vector<int> ans[maxn];

void dfs(int u)
{
    pre[u]=low[u]=++dfn;
    S[c++]=u;
    for(int i=0;i<adj[u].size();++i) {
        int v=adj[u][i];
        if(!pre[v]) {
            dfs(v);
            low[u]=min(low[u],low[v]);
        } else if(!sccno[v])
            low[u]=min(low[u],pre[v]);
    }
    if(pre[u]==low[u]) {
        ++scc_cnt;
        for(;;) {
            int x=S[--c];
            sccno[x]=scc_cnt;
            if(x==u) break;
        }
    }
}

void find_scc()
{
    c=0;
    clr(pre,0); clr(sccno,0); clr(low,0);
    dfn=scc_cnt=0;
    for(int i=1;i<=2*n;++i)
    if(!pre[i]) dfs(i);
}

void input()
{
    for(int i=0;i<maxn;++i) adj[i].clear(), ans[i].clear();
    for(int i=1;i<=n;++i) {
        int K; scanf("%d",&K);
        while(K--) {
            int x; scanf("%d",&x);
            x+=n;
            adj[i].push_back(x);
        }
    }
    for(int i=1;i<=n;++i) {
        int x; scanf("%d",&x); x+=n;
        adj[x].push_back(i);
        ans[i].push_back(x-n);
        for(int j=0;j<adj[i].size();++j) {
            int v=adj[i][j];
            if(v==x) { adj[i].erase(adj[i].begin()+j); break; }
        }
    }
}

void solve()
{
    find_scc();
    for(int i=1;i<=n;++i) {
        for(int j=0;j<adj[i].size();++j) {
            int k=adj[i][j];
            if(sccno[i]==sccno[k]) ans[i].push_back(k-n);
        }
    }
    for(int i=1;i<=n;++i) {
        int sz=ans[i].size(); printf("%d",sz);
        for(int j=0;j<sz;++j) printf(" %d",ans[i][j]);
        puts("");
    }
}

int main()
{
    while(scanf("%d",&n)==1) {
        input();
        solve();
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值