POJ 1904--King's Quest 【经典建模 && SCC】

King's Quest
Time Limit: 15000MS Memory Limit: 65536K
Total Submissions: 8163 Accepted: 2959
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个王子,每个王子都有k个喜欢的妹子,每个王子只能和喜欢的妹子结婚,大臣给出一个匹配表,每个王子都和一个妹子结婚,但是国王不满意,他要求大臣给他另一个表,每个王子可以和几个妹子结婚,按序号升序输出妹子的编号,这个表应满足所有的王子最终都有妹子和他结婚。

解析:

好经典的图论题,还以是为二分匹配问题,但是枚举+二分匹配肯定会超时,回来看大牛的解析发现竟然用强联通,瞬间膜拜了。

首先建图,如果王子u喜欢妹子v,则建一条边u指向v(u,v),对于大臣给出的初始完美匹配,如果王子u和妹子v结婚,则建一条边v指向u(v,u),然后求强连通分量。这里我们把王子虚拟成节点i,王子是1~n,公主虚拟成节点i + n,公主是n + 1~ 2 * n。

具体为啥这样可以,我也说不太清楚,自己模拟一下画画图就差不多明白了。

想看为啥这样的点这里  解析1 ,解析2。讲的都不错。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define maxn 5500
#define maxm 2200000
#define INF 0x3f3f3f3f
using namespace std;
int n;

struct node {
    int u, v, next;
};
node edge[maxm];
int head[maxn], cnt;
int low[maxn], dfn[maxn];
int dfs_clock;
int Stack[maxn], top;
bool Instack[maxn];
int Belong[maxn];
int scc_clock;
vector<int>scc[maxn];
int map[2010][2010];//map[i][j]表示王子i喜欢公主j
int num[maxn];//记录可以结婚的女孩的编号

void init(){
    cnt = 0;
    memset(head, -1, sizeof(head));
    memset(map, 0, sizeof(map));
}

void add(int u, int v){
    edge[cnt] = {u, v, head[u]};
    head[u] = cnt++;
}

void getmap(){
    init();
    int a, b;
    for(int i = 1; i <= n; ++i){
        scanf("%d", &a);
        while(a--){
            scanf("%d", &b);
            add(i, b + n);
            map[i][b + n] = 1;
        }
    }
    for(int i = 1; i <= n; ++i){
        scanf("%d", &b);
        add(b + n, i);
    }
}

void Tarjan(int u){
    int v;
    low[u] = dfn[u] = ++dfs_clock;
    Stack[top++] = u;
    Instack[u] = true;
    for(int i = head[u]; i != -1; i = edge[i].next){
        v = edge[i].v;
        if(!dfn[v]){
            Tarjan(v);
            low[u] = min(low[u], low[v]);
        }
        else if(Instack[v])
            low[u] = min(low[u], dfn[v]);
    }
    if(dfn[u] == low[u]){
        scc_clock++;
        scc[scc_clock].clear();
        do{
            v = Stack[--top];
            Instack[v] = false;
            Belong[v] = scc_clock;
            scc[scc_clock].push_back(v);
        }
        while(v != u);
    }
}

void find(){
    memset(low, 0, sizeof(low));
    memset(dfn, 0, sizeof(dfn));
    memset(Belong, 0, sizeof(Belong));
    memset(Stack, 0, sizeof(Stack));
    memset(Instack, false, sizeof(Instack));
    dfs_clock = scc_clock = top = 0;
    for(int i = 1; i <= 2 * n; ++i){
        if(!dfn[i])
            Tarjan(i);
    }
}

void solve(){
    find();
    int ans;
    for(int i = 1; i <= n; ++i){
        int u = Belong[i];
        ans = 0;
        for(int j = 0; j < scc[u].size(); ++j){
            //在一个强连通分量 且有边相连
            if(scc[u][j] > n && map[i][scc[u][j]])
                num[ans++] = scc[u][j] - n;//记录编号
        }
        printf("%d", ans);
        sort(num, num + ans);
        for(int j = 0; j < ans; ++j)
            printf(" %d", num[j]);
        printf("\n");
    }
}

int main (){
    while(scanf("%d", &n) != EOF){
        getmap();
        solve();
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值