1904 King's Quest

原创 2007年10月15日 08:38:00
King's Quest
Time Limit: 15000MS   Memory Limit: 65536K
Total Submissions: 1045   Accepted: 329
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

 

Sample Output

 

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.

Source
Northeastern Europe  

***********************************************************************************

***********************************************************************************

Source Code
/*
题目给我们一个2*N个顶点的2分图,并且给了一个完美匹配(Perfect Matching)以及每个顶点可以连接的其他的顶点。
题目要求是否可以确定某2个顶点连边后,其他2*(N - 1)个顶点的2分图是否可以构成完美匹配。
分析:
题目给了你一个初始的最大匹配 怎样用好这个匹配是非常关键的
首先把图转化成有向图(因为后面我们要用到连通性判定)
现在令王子为A集合,公主为B集合
原图中所有的边转化成A->B的边 然后对原来的每一个匹配Ai->Bj 添加一条从Bj到Ai的边
结论: 如果Ai,Bj能够互访 我们则认为Ai, Bj作为一条匹配边仍然不会影响其他王子和自己心爱的人匹配
证明如下:
现在有一个匹配Ai->Bj 我们知道Ai和Bj可以互访
我们要验证其是否可以成立时对其他王子找MM没有影响
1。如果题目中给的初始匹配包含这条边 则题目给出的初始匹配就证明了这位王子的公德心
2。如果题目没有给出这条边 给出的是Ai -> Bk 由于Ai与Bk也可互访 所以存在Bj->Ai->Bk的增广路 也就是说可以建立另外一个匹配A?->Bk 
所以同一个连通分量内部是可以换匹配的 呵呵

关于求极大连通子图的方法 具体如下:
求有向图的极大强连通分支 
1.对图进行DFS遍历 遍历中记下所有的结束时间A[i].遍历的结果是构建的一座森林W1
  我们对W1中的每棵树G进行步骤2到步骤3的操作
2.改变图G中每一条边的方向 构造出新的有向图Gr
3.按照A[i]由小到大的顺序对Gr进行DFS遍历.遍历的结果是构建了新的树林W2.
  W2中每棵树上的顶点构成了有向图的极大强连通分支
如果对DFS的相关算法不熟悉 请参考我的另一篇文章
图的DFS信息构建+割点,桥,极大连通子图三大法宝

*/
#include <vector>
#include <algorithm>
using namespace std;

const int N = 2010;
int nv;
vector<int> head[N], head2[N], S;
int go[N], back[N]; 
int scc[N];
bool chk[N];
bool love[N][N];

void DFS(int x) {
    int i;
    chk[x] = 1;
    for(i = 0; i < head[x].size(); ++i) {
        int j = back[head[x][i]];
        if(!chk[j]) 
            DFS(j);    
    }
    S.push_back(x); //入栈
}

 void DFS2(int x, int id) {
    int y = go[x], i;
    chk[y] = 1;
    scc[x] = id; //标记连通分支
    for(i = 0; i < head2[y].size(); ++i) {
        int j = go[head2[y][i]]; //找到对应的公主
        if(!chk[j])
            DFS2(head2[y][i], id);
    }
}

 int main() {
    scanf("%d", &nv);
    int i, t, u, j;
    for(i = 0; i < nv; ++i) {
        scanf("%d", &t);
        while(t--) {
            scanf("%d", &u);
            love[i][u-1] = 1;
            head[i].push_back(u-1); //有向边
            head2[u-1].push_back(i); //逆转的有向边
        }
    }
    for(i = 0; i < nv; ++i) {
        scanf("%d", &t);
        go[i] = t-1;
       back[t-1] = i;
   }

    memset(chk, 0, sizeof(chk));
    for(i = 0; i < nv; ++i) if(!chk[i]) {
        DFS(i); //对王子作DFS 确定i到达的点
    }

    memset(chk, 0, sizeof(chk));
    int sccId = 0;
    for(i = S.size()-1; i >= 0; --i) {
        int j = S[i];
        if(!chk[go[j]]) {
            DFS2(j, sccId); 
 //再对公主做DFS 确定连通分支(对王子和对公主其实是一样的 写法有点不同而已:)
            sccId++;
        }
    }

    for(i = 0; i < nv; ++i) {
        vector<int> ans;
        for(j = 0; j < nv; ++j) if(love[i][j]) {
            if(scc[i] == scc[back[j]])
                ans.push_back(j);
        }
        sort(ans.begin(), ans.end());
        printf("%d", ans.size());
       for(j = 0; j < ans.size(); ++j)
            printf(" %d", ans[j]+1);
        printf("/n");
   }

    return 0;
}
2 1 2
2 1 2
1 3
1 4
4
2 1 2
2 1 2
2 2 3
2 3 4
1 2 3 4

【poj】1904 King's Quest【强连通】

题意:国王有n个王子,王国内有n个妹子,每个王子喜欢一个(些)妹子(们),给出了一个王子与妹子之间的完美匹配.问一个王子可以匹配的妹子有哪些,要求改王子匹配改妹子之后,其他王子仍有妹子可以匹配,仍能够...
  • a709743744
  • a709743744
  • 2016-08-06 02:14:16
  • 261

poj1904 - King's Quest

想看更多的解题报告: http://blog.csdn.net/wangjian8006/article/details/7870410                               ...
  • wangjian8006
  • wangjian8006
  • 2012-09-15 16:44:10
  • 2989

POJ - 1904 King's Quest(强连通分量+二分图匹配)

题目大意:有N个帅哥和N个美女,现在给出每个帅哥所喜欢的美女的编号,和一个帅哥和美女的完美匹配 问每个帅哥可以娶多少个美女,且当他娶完这个美女后,剩下的人还可以完美匹配解题思路:神题啊,给一个大神的...
  • L123012013048
  • L123012013048
  • 2015-08-16 00:19:46
  • 642

PKU 1904 King's Quest

  • 2009年10月20日 21:29
  • 1018B
  • 下载

POJ 1904 King's Quest

 Kings QuestTime Limit: 15000MS Memory Limit: 65536KTotal Submissions: 2957 Accepted: 993Case Time L...
  • bobten2008
  • bobten2008
  • 2009-11-28 15:57:00
  • 883

Poj 1904 King's Quest

Poj 1904 Tarjan判环,非缩点
  • CuSO45H20
  • CuSO45H20
  • 2017-02-13 22:05:26
  • 508

poj 1904 King's Quest

题目链接:http://poj.org/problem?id=1904 题目大意:列出一张单子,在有一个完美匹配的基础上得到一个匹配表保证任一王子选择了一个钟意女子作为妻子,其他王子还有钟意的女...
  • gotoac
  • gotoac
  • 2012-07-08 13:55:00
  • 1643

POJ 1904:King's Quest

题目链接:http://poj.org/problem?id=1904 /* 题目来源:POJ 1904:King's Quest 题目大意:国王有N个儿子,国家有N个漂亮女孩,每个儿子有一...
  • wyxeainn
  • wyxeainn
  • 2017-09-06 11:40:15
  • 125

pku 1904 king's Quest

题意:有n个男的,n个女的,每个男的都有自己喜欢的女孩(可以是多个),给你个初始匹配,问你每个男的可以和哪些女的结婚。可以的意思是:这个男的和这个女的结婚后对于其他的人都存在一个女孩能和他结婚分析:匹...
  • ywhorizen
  • ywhorizen
  • 2011-07-21 19:35:27
  • 455

pku 1904 King's Quest

这个题目是看解题报告做的,最大感觉 是模型转化的奇妙。如果prince  i matches princess i,j matches j oringinally, now i matches j i...
  • sanpin_2007
  • sanpin_2007
  • 2007-10-08 21:22:00
  • 589
收藏助手
不良信息举报
您举报文章:1904 King's Quest
举报原因:
原因补充:

(最多只允许输入30个字)