zoukankan      html  css  js  c++  java
  • poj 1904 King's Quest

    King's Quest
    Time Limit: 15000MS   Memory Limit: 65536K
    Total Submissions: 10052   Accepted: 3704
    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个王子和n个妹子,每个王子有好几个喜欢的妹子,巫师给出了一个完美匹配,每个王子都可以和自己喜欢的妹子结婚。但是国王对这个不满意,国王想知道每个王子最多可以和几个妹子结婚,在每个王子都可以找到自己喜欢的妹子的前提下。
    思路:二话不说,先建图,每个王子向每个喜欢的妹子连一条边,因为能结婚的第一个条件,王子喜欢她。再把给出的完美匹配,妹子向王子连一条边。强连通分量内部的王子和公主匹配,为什么呢?
    首先,强连通分量内部的王子和妹子是可以相互联通的,所以可以结婚,
    其次,王子和妹子个数一定相等,不可能王子比妹子少,否则与完美匹配只有一个矛盾,也不可能王子比妹子多,一来王子不会喜欢王子,二来还是完美匹配矛盾问题。
    为什么王子不会和强连通分量外部的妹子结婚?
    如果出现这种情况,比如王子x,y,妹子a,b,x,a是匹配的,y,b是匹配的,且不在一个联通分量里。若x与b匹配,则y与a匹配,与不在一个强连通分量矛盾。
    被卡了10次的地方!
    数组开小了!
    读题读题读题!
      1 #include<cstdio>
      2 #include<cstring>
      3 #include<iostream>
      4 #include<cstdlib>
      5 #include<algorithm>
      6 #include<cmath>
      7 #include<vector>
      8 #include<stack>
      9 #include<map>
     10 using namespace std;
     11 #define mem(a,b) memset(a,b,sizeof(a))
     12 #define ll long long
     13 #define inf 1000000000
     14 #define maxn 5000
     15 #define maxm 200000+2500
     16 inline int read()
     17 {
     18     int x=0,f=1;char ch=getchar();
     19     while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
     20     while(ch>='0'&&ch<='9') {x=10*x+ch-'0';ch=getchar();}
     21     return x*f;
     22 }
     23 struct node
     24 {
     25     int to,next;
     26 } edge[maxm];
     27 int n,m,head[maxn],vis[maxn],dfn[maxn],low[maxn],cdu[maxn],num[maxn],cnt,timi,stack1[maxn],top,cut,rdu[maxn];
     28 void init()
     29 {
     30     memset(dfn,0,sizeof(dfn));
     31     memset(low,0,sizeof(low));
     32     memset(head,-1,sizeof(head));
     33     memset(vis,0,sizeof(vis));
     34     memset(num,0,sizeof(num));
     35     memset(cdu,0,sizeof(cdu));
     36     memset(rdu,0,sizeof(rdu));
     37     cnt=0;
     38     timi=1;
     39     top=0;
     40     cut=0;
     41 }
     42 void addedge(int u,int v)
     43 {
     44     edge[cnt].to=v;
     45     edge[cnt].next=head[u];
     46     head[u]=cnt;
     47     cnt++;
     48 }
     49 int Stack[maxm];
     50 void Tarjan(int u)    //求强连通分量
     51 {
     52     int i,v;
     53     low[u]=dfn[u]=timi++;
     54     Stack[++top]=u;
     55     vis[u]=1;
     56     for(i=head[u];i!=-1;i=edge[i].next)
     57     {
     58         v=edge[i].to;
     59         if(!dfn[v])
     60         {
     61             Tarjan(v);
     62             low[u]=min(low[u],low[v]);
     63         }
     64         else if(vis[v])
     65             low[u]=min(low[u],dfn[v]);
     66     }
     67     if(low[u]==dfn[u])
     68     {
     69         cut++;
     70         while(1)
     71         {
     72             v=Stack[top--];
     73             vis[v]=false;
     74             num[v]=cut;    //缩点
     75             if(u==v)
     76                 break;
     77         }
     78     }
     79 }
     80 int main()
     81 {
     82     int a;
     83     while(~scanf("%d",&n))
     84     {
     85         init();
     86         for(int i=1; i<=n; ++i)
     87         {
     88             int x=read();
     89             while(x--)
     90             {
     91                 a=read();addedge(i,a+n);
     92             }
     93         }
     94         for(int i=1;i<=n;++i)
     95         {
     96             a=read();addedge(a+n,i);
     97         }
     98         for(int i=1; i<=2*n; ++i)
     99         {
    100             if(!dfn[i]) Tarjan(i);
    101         }
    102         for(int i=1;i<=n;++i)
    103         {
    104             int ans[4005],tot=0;
    105             tot=0;
    106             for(int j=head[i];j!=-1;j=edge[j].next)
    107             {
    108                 int v=edge[j].to;
    109                 if(num[i]==num[v]) ans[++tot]=v-n;
    110             }
    111             sort(ans+1,ans+tot+1);
    112             printf("%d",tot);
    113             for(int j=1;j<=tot;++j) printf(" %d",ans[j]);
    114             printf("
    ");
    115             //printf("%d
    ",ans[tot]);
    116         }
    117     }
    118     return 0;
    119 }
    View Code
     
  • 相关阅读:
    下载视频
    009 逻辑 + MFC CString
    008 浅拷贝与深拷贝
    007 operator
    006 this指针原理
    005 逻辑 + STL list
    004 list::sort
    003 逻辑 + mfc CList
    001 C++历史和思想
    并查集模板
  • 原文地址:https://www.cnblogs.com/TYH-TYH/p/9396504.html
Copyright © 2011-2022 走看看