POJ 1904 King's Quest
2014-08-29 13:44
453 查看
King's Quest
Time Limit: 15000msCase 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
Northeastern Europe 2003解题:。。。。。。。。。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <vector> #include <queue> #include <cstdlib> #include <string> #include <set> #include <stack> #define LL long long #define pii pair<int,int> #define INF 0x3f3f3f3f using namespace std; const int maxn = 2000000; struct arc{ int to,next; }; arc e[maxn]; int head[maxn],dfn[maxn],low[maxn],tot; bool instack[maxn]; int belong[maxn],cnt,scc,n; stack<int>stk; void add(int u,int v){ e[tot].to = v; e[tot].next = head[u]; head[u] = tot++; } void tarjan(int u){ dfn[u] = low[u] = ++cnt; stk.push(u); instack[u] = true; for(int i = head[u]; i != -1; i = e[i].next){ if(!dfn[e[i].to]){ tarjan(e[i].to); low[u] = min(low[u],low[e[i].to]); }else if(instack[e[i].to]) low[u] = min(low[u],dfn[e[i].to]); } if(dfn[u] == low[u]){ int v; scc++; do{ v = stk.top(); belong[v] = scc; instack[v] = false; stk.pop(); }while(v != u); } } vector<int>ans; int main() { int i,j,k,u,v; scanf("%d",&n); for(i = 1; i <= n<<1; i++){ dfn[i] = low[i] = belong[i] = 0; instack[i] = false; head[i] = -1; } cnt = scc = tot = 0; for(i = 1; i <= n; i++){ scanf("%d",&k); while(k--){ scanf("%d",&j); add(i,j+n); } } for(i = 1; i <= n; i++){ scanf("%d",&j); add(j+n,i); } for(i = 1; i <= n; i++) if(!dfn[i]) tarjan(i); for(i = 1; i <= n; i++){ ans.clear(); for(j = head[i]; j != -1; j = e[j].next){ if(e[j].to > n && belong[i] == belong[e[j].to]){ ans.push_back(e[j].to-n); } } sort(ans.begin(),ans.end());//要求递增 printf("%d",ans.size()); for(j = 0; j < ans.size(); j++) printf(" %d",ans[j]); puts(""); } return 0; }
View Code
相关文章推荐
- poj 1904 King's Quest
- POJ-1904-King's Quest(强连通图)
- poj--1904--King's Quest(scc建图)
- poj 1904 King's Quest 强连通
- Poj 1904 King's Quest 强连通分量
- poj 1904 King's Quest tarjan求二分图的所有可选最大匹配边
- POJ 1904 King's Quest【强连通分量】
- poj 1904 King's Quest (SCC缩点)
- POJ 1904 King's Quest 强连通分量+二分图增广判定
- POJ 1904 King's Quest - from lanshui_Yang
- POJ - 1904 King's Quest(强连通分量+二分图匹配)
- [poj 1904]King's Quest[Tarjan强连通分量]
- poj 1904 King's Quest 强连通
- pku 1904 King's Quest tarjan求强连通分量
- POJ1904(King's Quest)
- poj 1904 King's Quest(强连通分量)
- POJ 1904 King's Quest(强连通分量+匹配)
- King's Quest - poj 1904(强连通分量+外挂输入输出)
- poj 1904 King's Quest 强连通分量
- poj 1904 King's Quest(强连通)