日韩性视频-久久久蜜桃-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(强连通分量)的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 亚洲精品综合精品自拍 | 哪个网站可以看毛片 | 亚洲精品天堂在线 | 成人手机在线观看 | 国产成人在线影院 | 久久亚洲热 | 在线免费日本 | 亚洲欧洲在线视频 | 中文字幕第一页亚洲 | 你懂的网站在线 | 一级全黄男女免费大片 | 亚洲熟妇色自偷自拍另类 | 久久久精品人妻一区二区三区四 | 欧美在线观看网站 | 妺妺窝人体色777777 | 国产成人综合精品 | 亚洲欧美一区二区三 | 亚洲激情免费 | 99欧美精品 | 重囗另类bbwseⅹhd | 九九黄色| 久草福利在线视频 | www.久久av.com| 少妇一级片 | 亚洲图片欧美在线 | 人人干视频 | 亚洲午夜福利一区二区三区 | 人人插人人爽 | 国产自偷| 四级毛片| av色区| 午夜影视剧场 | 人人射av| 日韩美女在线 | 国产老头和老头xxxx× | 东京热无码av一区二区 | 一区www | 在线播放不卡 | 国产在线观看免费视频软件 | 少妇人妻真实偷人精品视频 | 国偷自产视频一区二区久 | 萌白酱一区二区 | av高清不卡| 亚洲小视频在线 | 久久久999精品 | 第四色视频 | 老司机精品视频在线 | 国产黄色免费观看 | 国产传媒一区二区三区 | 毛片aaaa| 污导航在线观看 | 三上悠亚一区二区三区 | 北条麻妃一区二区三区在线观看 | 国产视频导航 | 欧美视频在线观看一区二区 | 国产精品tv | 久久久久久久久国产 | 尤物视频在线观看国产性感 | 老太脱裤让老头玩ⅹxxxx | 操操日日 | 韩国三级hd两男一女 | 久久久久亚洲精品系列色欲 | 日韩三级一区二区三区 | 视频一区二区在线 | 伊人99| 久久久久久久久久免费 | 色妞av | 精品视频久久 | 五月婷婷激情在线 | 亚洲精品一区二区三区婷婷月 | 国产精品一区二区av白丝下载 | 亚洲人人插 | 久草影视网 | 上海贵妇尝试黑人洋吊 | 亚洲天堂色图 | 99国产精品国产精品九九 | 好吊色综合 | 成人欧美一区二区三区黑人动态图 | 黄色国产在线观看 | 夜夜天天干| 欧美a在线视频 | 麻豆国产视频 | 快色在线观看 | 亚洲无码精品免费 | 欧美在线色视频 | 免费视频福利 | 亚洲春色一区二区三区 | 日韩电影网址 | 麻豆精品视频免费观看 | 成人乱码一区二区三区 | 中国毛片在线观看 | 亚洲熟妇无码另类久久久 | 免费黄色小网站 | 国模私拍在线 | 色欧洲 | 欧美性猛交xxx乱久交 | 粉嫩aⅴ一区二区三区四区五区 | 日韩精品视频久久 | 给我看免费高清在线观看 |