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

    King's Quest

    Time Limit: 15000ms
    Case Time Limit: 2000ms
    Memory Limit: 65536KB
    This problem will be judged on PKU. Original ID: 1904
    64-bit integer IO format: %lld      Java class name: Main
     
    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.
     

    Source

     
    解题:。。。。。。。。。
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 const int maxn = 2000000;
    18 struct arc{
    19     int to,next;
    20 };
    21 arc e[maxn];
    22 int head[maxn],dfn[maxn],low[maxn],tot;
    23 bool instack[maxn];
    24 int belong[maxn],cnt,scc,n;
    25 stack<int>stk;
    26 void add(int u,int v){
    27     e[tot].to = v;
    28     e[tot].next = head[u];
    29     head[u] = tot++;
    30 }
    31 void tarjan(int u){
    32     dfn[u] = low[u] = ++cnt;
    33     stk.push(u);
    34     instack[u] = true;
    35     for(int i = head[u]; i != -1; i = e[i].next){
    36         if(!dfn[e[i].to]){
    37             tarjan(e[i].to);
    38             low[u] = min(low[u],low[e[i].to]);
    39         }else if(instack[e[i].to])
    40             low[u] = min(low[u],dfn[e[i].to]);
    41     }
    42     if(dfn[u] == low[u]){
    43         int v;
    44         scc++;
    45         do{
    46             v = stk.top();
    47             belong[v] = scc;
    48             instack[v] = false;
    49             stk.pop();
    50         }while(v != u);
    51     }
    52 }
    53 vector<int>ans;
    54 int main() {
    55     int i,j,k,u,v;
    56     scanf("%d",&n);
    57     for(i = 1; i <= n<<1; i++){
    58         dfn[i] = low[i] = belong[i] = 0;
    59         instack[i] = false;
    60         head[i] = -1;
    61     }
    62     cnt  = scc = tot = 0;
    63     for(i = 1; i <= n; i++){
    64         scanf("%d",&k);
    65         while(k--){
    66             scanf("%d",&j);
    67             add(i,j+n);
    68         }
    69     }
    70     for(i = 1; i <= n; i++){
    71         scanf("%d",&j);
    72         add(j+n,i);
    73     }
    74     for(i = 1; i <= n; i++)
    75         if(!dfn[i]) tarjan(i);
    76     for(i = 1; i <= n; i++){
    77         ans.clear();
    78         for(j = head[i]; j != -1; j = e[j].next){
    79             if(e[j].to > n && belong[i] == belong[e[j].to]){
    80                 ans.push_back(e[j].to-n);
    81             }
    82         }
    83         sort(ans.begin(),ans.end());//要求递增
    84         printf("%d",ans.size());
    85         for(j = 0; j < ans.size(); j++)
    86             printf(" %d",ans[j]);
    87         puts("");
    88     }
    89     return 0;
    90 }
    View Code
  • 相关阅读:
    笑话(真人真事)一则
    Object Builder中的Locator究竟是不是采用Composite的模式之我见
    C++AndC#我的程序员之路
    C#中各种十进制数的转换
    使用GotDotnet workSpace手记
    检索 COM 类工厂中 CLSID 为 {0002450000000000C000000000000046} 的组件失败
    CSS如何让同一行的图片和文字垂直居中对齐(FF,Safari,IE都通过)
    怎样练习一万小时成为顶级高手?
    CSS控制大小写
    做SEO权重计算公式
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3944770.html
Copyright © 2011-2022 走看看