日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

POJ 1904 King's Quest(强连通分量)

發布時間:2024/4/18 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ 1904 King's Quest(强连通分量) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接

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個公主,巫師已經為每個王子分配一個公主,但是皇帝想知道每個王子在保證每個王子都能夠匹配的前提下能娶的公主有多少?

思路

  • 剛開始用二分圖暴力刪邊 -> TLE
  • 強連通分量:王子向公主建邊,公主向和她匹配的王子建邊。這樣保證每一對王子和公主都在一個強聯通分量中,這是如果求得一個強聯通分量,在分量中的王子和公主都可以相互結婚而之前已經匹配的也能找到新的關系,這樣就符合題意

有兩個坑點:

  • 輸出公主編號要升序(in ascending order)
  • 同一個強連通分量其實也不能互相結婚,還有個前提是王子要喜歡才行,最后輸出的時候要判斷一下

scanf + printf + vector :12s
讀寫掛 + 鏈式向前星:500ms

#include <iostream> #include <stdio.h> #include <map> #include <vector> #include <set> #include <queue> #include <stack> #include <cstring> #include <cmath> #include <algorithm> #define lowbit(x) (x & (-x)) #define mem(a, b) memset(a, b, sizeof(a)) #define rep(i, a, n) for (int i = a; i < n; ++i) #define mid ((l + r)>>1) #define lc rt<<1 #define rc rt<<1|1 typedef long long LL; #define maxn 4004 using namespace std; // __int128 read() { __int128 x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } return x * f;} // void print(__int128 x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) print(x / 10); putchar(x % 10 + '0');} int read() {int num = 0, flag = 1;char c = getchar();while (c < '0' || c > '9') flag = (c == '-') ? -1 : 1, c = getchar();while (c >= '0' && c <= '9') num = (num << 3) + (num << 1) + c - '0', c = getchar();return num * flag; } void out(int x) {if (x > 9) out(x / 10);putchar(x % 10 + '0'); }vector<int> g[maxn];int Stack[maxn], low[maxn], dfn[maxn], inStack[maxn], belong[maxn]; int now = 0, len = 0, cnt= 0, n; int ans[maxn];int head[300020], pre[300020], edge[300020], tot = 1; void add (int u, int v) {edge[tot] = v;pre[tot] = head[u];head[u] = tot++; }void tarjan(int x) {low[x] = dfn[x] = ++now;Stack[++len] = x;inStack[x] = 1;for (int i = head[x]; i; i = pre[i]) {int y = edge[i];if (!dfn[y]) {tarjan(y);low[x] = min(low[x], low[y]);}else if (inStack[y]) low[x] = min(low[x], low[y]);}// for (int i = 0; i < (int)g[x].size(); ++i) {// int y = g[x][i];// if (!dfn[y]) tarjan(y), low[x] = min(low[x], low[y]);// else if (inStack[y]) low[x] = min(low[x], low[y]);// }if (dfn[x] == low[x]) {++cnt;int top;while (Stack[len] != x) {top = Stack[len--];belong[top] = cnt;inStack[top] = 0; }top = Stack[len--];belong[top] = cnt;inStack[top] = 0;} }int main() { #ifndef ONLINE_JUDGE// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout); #endifios::sync_with_stdio(false);cin.tie(0); cout.tie(0);n = read();for (int i = 1, m, d; i <= n; ++i) {m = read();while (m--) {d = read();// g[i].push_back(d+n);add(i, d+n);}}for (int i = 1, d; i <= n; ++i) {d = read();// g[d+n].push_back(i);add(d+n, i);}for (int i = 1; i <= n; ++i) {if (!dfn[i]) {tarjan(i);}}for (int i = 1; i <= n; ++i) {int sum = 0;for (int j = head[i], y; j; j = pre[j]) {y = edge[j];if (belong[y] == belong[i]) ans[++sum] = y;}// for (int j = 0, y; j < (int)g[i].size(); ++j) {// y = g[i][j];// if (belong[y] == belong[i]) ans[++sum] = y; // }out(sum);sort(ans+1, ans+1+sum);for (int i = 1; i <= sum; ++i) {putchar(32); out(ans[i]-n);}putchar(10);}return 0; } 與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的POJ 1904 King's Quest(强连通分量)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。