青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

oyjpArt ACM/ICPC算法程序設計空間

// I am new in programming, welcome to my blog
I am oyjpart(alpc12, 四城)
posts - 224, comments - 694, trackbacks - 0, articles - 6

PKU1904 king's quest

Posted on 2007-08-07 23:58 oyjpart 閱讀(2269) 評論(3)  編輯 收藏 引用 所屬分類: ACM/ICPC或其他比賽

King's Quest
Time Limit:15000MS  Memory Limit:65536K
Total Submit:938 Accepted:278
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.

Source
Northeastern Europe 2003


題目給我們一個2*N個頂點的2分圖,并且給了一個完美匹配(Perfect Matching)以及每個頂點可以連接的其他的頂點。
題目要求是否可以確定某2個頂點連邊后,其他2*(N - 1)個頂點的2分圖是否可以構成完美匹配。
分析:
題目給了你一個初始的最大匹配 怎樣用好這個匹配是非常關鍵的
首先把圖轉化成有向圖(因為后面我們要用到連通性判定)
現在令王子為A集合,公主為B集合
原圖中所有的邊轉化成A->B的邊 然后對原來的每一個匹配Ai->Bj 添加一條從Bj到Ai的邊
結論: 如果Ai,Bj能夠互訪 我們則認為Ai, Bj作為一條匹配邊仍然不會影響其他王子和自己心愛的人匹配
證明如下:
現在有一個匹配Ai->Bj 我們知道Ai和Bj可以互訪
我們要驗證其是否可以成立時對其他王子找MM沒有影響
1。如果題目中給的初始匹配包含這條邊 則題目給出的初始匹配就證明了這位王子的公德心
2。如果題目沒有給出這條邊 給出的是Ai -> Bk 由于Ai與Bk也可互訪 所以存在Bj->Ai->Bk的增廣路 也就是說可以建立另外一個匹配A?->Bk
所以同一個連通分量內部是可以換匹配的 呵呵

關于求極大連通子圖的方法 具體如下:
求有向圖的極大強連通分支
1.對圖進行DFS遍歷 遍歷中記下所有的結束時間A[i].遍歷的結果是構建的一座森林W1
  我們對W1中的每棵樹G進行步驟2到步驟3的操作
2.改變圖G中每一條邊的方向 構造出新的有向圖Gr
3.按照A[i]由小到大的順序對Gr進行DFS遍歷.遍歷的結果是構建了新的樹林W2.
  W2中每棵樹上的頂點構成了有向圖的極大強連通分支
如果對DFS的相關算法不熟悉 請參考我的另一篇文章
圖的DFS信息構建+割點,橋,極大連通子圖三大法寶
http://m.shnenglu.com/sicheng/archive/2007/01/19/17767.html

如果覺得上面的方法太麻煩 還有一種簡單的方法(呵呵), 具體步驟如下:
我們定義定點U的標值函數 LOW(U) = min { dfn(U), dfn(W) };
其中W是U或者U的后代點通過反向邊或者橫叉邊能夠到達的同一個連通分量的定點
dfn()是指一個點第一次被訪問的時間
由此可以看出 LOW(U)正是U所處的強連通分支中從U出發先通過樹枝邊(組成DFS樹的邊),前向邊,最后用后向邊或者橫叉邊到達的dfn的最小的定點的標值.而對于強分支的根Ri,顯然LOW(Ri) = dfn(Ri). 因此當深度有限搜索從一個使dfn = LOW的定點U返回時,從樹中移去根為U的所有頂點.每一個這種的集合是一個強分支.
算法的主要思路是逐步迭代算出LOW值:
當第一次訪問定點U時:
LOW(U) = Min(LOW(U), dfn(U))
后向邊(U,W)被檢查時:
LOW(U) = Min(LOW(U), dfn(W))
處于同一強分支的橫叉邊被檢查時:
LOW(U) = MIN(LOW(U), dfn(W))
檢查了U的兒子S的所有關聯邊后返回頂點U時:
LOW(U) = Min(LOW(U), LOW(S))

這樣就可以用一個DFS解決啦~ 

foj的一個"信與信封"的題目也可以用強連通來做 不過那個題目數據弱多了 枚舉+2分圖最大匹配也能過

下面這個程序是用第二種求法解決的:
Solution:
//by oyjpArt
 1#include <vector>
 2#include <algorithm>
 3using namespace std;
 4
 5const int N = 2010;
 6int nv, times, sccId;
 7int go[N], back[N]; //匹配的正向和反向
 8int low[N], dfn[N]; //low數組, dfn是第一次訪問某節點的時間
 9int love[N][N];  // love[i][j]代表i王子愛上j公主 即i向j連接一條邊
10int scc[N];   //scc代表強連通分量id (Strongly Connected Component ID)
11bool inS[N]; //inS代表是否在棧中(已被壓入且為被彈出) 注意 表示一個節點沒有被訪問過應該是dfn[i] == 0
12vector<int> S; //Stack
13
14#define Min(a, b) ((a) < (b) ? (a) : (b))
15
16void DFS(int x) {
17    int i;
18    dfn[x] = ++times; //第一次訪問
19    S.push_back(x); //壓入棧中
20    inS[x] = 1//標記入棧
21    int y = back[x], z; //找到相應王子
22    low[x] = times; //定義low[x]
23
24    for(i = 1; i <= love[y][0]; ++i) {
25        int j = love[y][i];
26        if(j != x) {
27            if(dfn[j] == 0) { //樹枝邊
28                DFS(j); 
29                low[x] = Min(low[x], low[j]); //檢查了x的兒子j的所有關聯邊后返回頂點x
30            }
31            else if(dfn[j] < dfn[x] && inS[j]) //處于同一強分支的后向邊或者橫叉邊被檢查
32                low[x] = Min(low[x], dfn[j]);
33        }
34    }
35
36    if(low[x] == dfn[x]) { //找到一個新的強連通分支
37        do {
38            z = S.back();
39            scc[z] = sccId; //標記連通分值 
40            inS[z] = false//出棧
41            S.pop_back();
42        }while(z != x); 
43        sccId++;
44    }
45}
46
47int main() {
48
49    scanf("%d"&nv);
50    int i, t, j;
51    for(i = 0; i < nv; ++i) {
52        scanf("%d"&love[i][0]);
53        for(j = 1; j <= love[i][0]; ++j) {
54            scanf("%d"&love[i][j]);
55            --love[i][j];
56        }
57    }
58    for(i = 0; i < nv; ++i) {
59        scanf("%d"&t);
60        go[i] = t-1;
61        back[t-1= i;
62    }
63
64    times = sccId = 0;
65    memset(inS, 0, sizeof(inS));
66    memset(dfn, 0, sizeof(dfn));
67    for(i = 0; i < nv; ++i) if(dfn[i] == 0) {
68        DFS(i); //對每個公主作DFS
69    }
70
71    for(i = 0; i < nv; ++i) {
72        vector<int> ans;
73        for(j = 1; j <= love[i][0]; ++j) {
74            if(scc[love[i][j]] == scc[go[i]]) //如果在同一個連通分量內
75                ans.push_back(love[i][j]);
76        }
77        sort(ans.begin(), ans.end());
78        printf("%d", ans.size());
79        for(j = 0; j < ans.size(); ++j)
80            printf(" %d", ans[j]+1);
81        printf("\n");
82    }
83
84    return 0;
85}


下面這種求法是用第一種方法解決的
Solution:
//by oyjpArt
 1#include <vector>
 2#include <algorithm>
 3using namespace std;
 4
 5const int N = 2010;
 6int nv;
 7vector<int> head[N], head2[N], S;
 8int go[N], back[N]; 
 9int scc[N];
10bool chk[N];
11bool love[N][N];
12
13void DFS(int x) {
14    int i;
15    chk[x] = 1;
16    for(i = 0; i < head[x].size(); ++i) {
17        int j = back[head[x][i]];
18        if(!chk[j]) 
19            DFS(j);    
20    }
21    S.push_back(x); //入棧
22}
23
24void DFS2(int x, int id) {
25    int y = go[x], i;
26    chk[y] = 1;
27    scc[x] = id; //標記連通分支
28    for(i = 0; i < head2[y].size(); ++i) {
29        int j = go[head2[y][i]]; //找到對應的公主
30        if(!chk[j])
31            DFS2(head2[y][i], id);
32    }
33}
34
35int main() {
36    scanf("%d"&nv);
37    int i, t, u, j;
38    for(i = 0; i < nv; ++i) {
39        scanf("%d"&t);
40        while(t--) {
41            scanf("%d"&u);
42            love[i][u-1= 1;
43            head[i].push_back(u-1); //有向邊
44            head2[u-1].push_back(i); //逆轉的有向邊
45        }
46    }
47    for(i = 0; i < nv; ++i) {
48        scanf("%d"&t);
49        go[i] = t-1;
50        back[t-1= i;
51    }
52
53    memset(chk, 0, sizeof(chk));
54    for(i = 0; i < nv; ++i) if(!chk[i]) {
55        DFS(i); //對王子作DFS 確定i到達的點
56    }
57
58    memset(chk, 0, sizeof(chk));
59    int sccId = 0;
60    for(i = S.size()-1; i >= 0--i) {
61        int j = S[i];
62        if(!chk[go[j]]) {
63            DFS2(j, sccId); //再對公主做DFS 確定連通分支(對王子和對公主其實是一樣的 寫法有點不同而已:)
64            sccId++;
65        }
66    }
67
68    for(i = 0; i < nv; ++i) {
69        vector<int> ans;
70        for(j = 0; j < nv; ++j) if(love[i][j]) {
71            if(scc[i] == scc[back[j]])
72                ans.push_back(j);
73        }
74        sort(ans.begin(), ans.end());
75        printf("%d", ans.size());
76        for(j = 0; j < ans.size(); ++j)
77            printf(" %d", ans[j]+1);
78        printf("\n");
79    }
80
81    return 0;
82}

突然發現簡單的方法寫出來的程序還長一點點。。暈

Feedback

# re: PKU1904 king's quest  回復  更多評論   

2008-08-17 13:34 by sdfond
第二種思路很不錯,學習下。。。十分感謝!~

# re: PKU1904 king's quest  回復  更多評論   

2009-07-01 12:52 by WinterLegend
寫得不錯,贊一個。

# re: PKU1904 king's quest  回復  更多評論   

2009-07-02 20:54 by oyjpart
謝謝
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            一本色道久久精品| 欧美一级午夜免费电影| 夜夜嗨av一区二区三区四区| 欧美aⅴ一区二区三区视频| 亚洲一区二区在线免费观看视频 | 欧美福利精品| 免费在线亚洲| 国产精品久久久久av| 欧美成人国产| 欧美人与禽性xxxxx杂性| 免费视频一区| 亚洲一区欧美二区| 一区二区三区免费在线观看| 欧美日韩精选| 亚洲免费视频网站| 国产人成精品一区二区三| 久久久久久久91| 另类综合日韩欧美亚洲| 久久se精品一区二区| 欧美~级网站不卡| 欧美激情在线免费观看| 国产主播精品在线| 亚洲影院在线| 日韩午夜av电影| 欧美福利视频在线| 中文在线不卡视频| 亚洲女性喷水在线观看一区| 欧美+日本+国产+在线a∨观看| 欧美大片18| 亚洲精品小视频在线观看| 久久亚洲二区| 午夜久久美女| 亚洲无亚洲人成网站77777 | 国产一区二区三区在线观看视频 | 亚洲人体影院| 一本色道久久综合亚洲二区三区| 在线视频欧美日韩精品| 好看不卡的中文字幕| 久久精品日产第一区二区三区 | 亚洲一区二区免费在线| 狼狼综合久久久久综合网| 精品99一区二区三区| 亚洲国产欧美日韩精品| 激情偷拍久久| 国产精品国产福利国产秒拍 | 久久人91精品久久久久久不卡| 亚洲欧洲另类国产综合| 国产精品久久久久久影院8一贰佰 国产精品久久久久久影视 | 99在线精品视频| 欧美精品尤物在线| 亚洲欧洲一级| 美国成人直播| 亚洲男人的天堂在线| 亚洲第一成人在线| 最新亚洲激情| 亚洲欧美精品中文字幕在线| 欧美日本国产| 欧美激情第9页| 国产精品成人免费| 欧美日韩第一区| 久久久亚洲高清| 午夜视频在线观看一区二区| 亚洲午夜在线观看| 一区二区三区|亚洲午夜| 午夜日韩在线观看| 在线一区二区视频| 亚洲国产精品99久久久久久久久| 久久久国产精品一区| 久久天堂国产精品| 亚洲第一狼人社区| 亚洲九九爱视频| 久久久久国产精品麻豆ai换脸| 欧美久久99| 国产精品盗摄久久久| 亚洲专区一区二区三区| 亚洲伦伦在线| 免费在线欧美视频| 91久久国产精品91久久性色| 久久久精品动漫| 亚洲欧美日韩精品| 亚洲中无吗在线| 欧美成人午夜激情在线| 欧美日本韩国一区二区三区| 久久久久九九九| 欧美极品在线观看| 欧美三区美女| 欧美不卡视频一区| 久久久久久97三级| 国产精品国产成人国产三级| 久久久精品一区| 免费不卡欧美自拍视频| 99re热精品| 在线视频精品一| 国产婷婷一区二区| 欧美一级久久久| 欧美日韩在线不卡| 欧美在线视频免费观看| 亚洲神马久久| 午夜日韩福利| 久久久久.com| 久久久久久9| 国产欧美日本在线| 中文亚洲欧美| 亚洲黄色视屏| 久久综合一区| 欧美国产日韩一区二区| 国产精品青草综合久久久久99| 久久久久久久一区二区| 欧美午夜www高清视频| 久久亚洲精品一区二区| 国产亚洲精品久| 午夜在线精品偷拍| 亚洲永久视频| 亚洲国产另类久久久精品极度| 亚洲性线免费观看视频成熟| 一区二区三区成人| 久久精品亚洲一区| 亚洲欧美成人一区二区三区| 欧美风情在线观看| 亚洲视频一区二区| 可以看av的网站久久看| 日韩视频在线免费观看| 亚洲高清在线播放| 免费成人毛片| 一本久久综合亚洲鲁鲁| 91久久国产综合久久91精品网站| 你懂的网址国产 欧美| 亚洲人成人一区二区三区| 在线观看欧美| 国产精品久久久久永久免费观看 | 亚洲永久在线观看| 国产一区二区精品丝袜| 欧美/亚洲一区| 美日韩精品免费观看视频| 亚洲国产成人精品女人久久久 | 亚洲国产欧美不卡在线观看| 国产亚洲欧美日韩日本| 男人插女人欧美| 久久精品一区二区三区不卡牛牛| 欧美aa国产视频| 午夜精品一区二区三区四区| 激情久久五月| 亚洲国产精品成人综合| 国产精品亚洲片夜色在线| 国产精品自拍在线| 欧美精品一区二区在线播放| 欧美日韩国产综合视频在线观看中文 | 亚洲一区影音先锋| 久久激情五月丁香伊人| 欧美一区二区黄色| 久久精品99无色码中文字幕| 日韩一级视频免费观看在线| 午夜精品久久| 性视频1819p久久| 另类激情亚洲| 久久99在线观看| 欧美一区二区三区在线播放| 男人的天堂亚洲在线| 欧美www视频| 久久婷婷国产综合尤物精品| 亚洲精品影院| 欧美成人小视频| 国产精品国产三级国产| 亚洲日本精品国产第一区| 在线国产亚洲欧美| 久久精选视频| 欧美有码在线观看视频| 国产精品一区二区三区四区五区| 精品av久久707| 亚洲一区二区三区乱码aⅴ| 午夜一级久久| 亚洲一级二级| 欧美日韩精品在线| 亚洲国产第一| 久久在线精品| 免费日韩成人| 亚洲国产另类精品专区| 免费久久99精品国产自在现线| 中日韩高清电影网| 欧美日韩免费观看一区=区三区| 国产精品草草| 久久久91精品| 麻豆成人在线观看| 亚洲国产成人午夜在线一区 | 国产一区二区日韩精品欧美精品| 伊人男人综合视频网| 蜜臀av一级做a爰片久久| 亚洲另类黄色| 欧美国产视频一区二区| 亚洲一区网站| 久久一本综合频道| 亚洲国产你懂的| 亚洲人人精品| 欧美视频在线观看| 香港成人在线视频| 亚洲欧美国产视频| 亚洲国产va精品久久久不卡综合| 欧美在线视频一区二区| 久久久久九九视频| 99re66热这里只有精品4|